blob: 58d547e55d3a63d538126039c2cb4acf1a2be437 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman6a648612005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000056
57 /// NeedsAnotherIteration - This is set when we expand a large integer
58 /// operation into smaller integer operations, but the smaller operations are
59 /// not set. This occurs only rarely in practice, for targets that don't have
60 /// 32-bit or larger integer registers.
61 bool NeedsAnotherIteration;
62
63 /// LegalizedNodes - For nodes that are of legal width, and that have more
64 /// than one use, this map indicates what regularized operand to use. This
65 /// allows us to avoid legalizing the same thing more than once.
66 std::map<SDOperand, SDOperand> LegalizedNodes;
67
Chris Lattner03c85462005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattner8afc48e2005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000080 LegalizedNodes.insert(std::make_pair(From, To));
81 // If someone requests legalization of the new node, return itself.
82 if (From != To)
83 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000084 }
Chris Lattner03c85462005-01-15 05:21:40 +000085 void AddPromotedOperand(SDOperand From, SDOperand To) {
86 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
87 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +000088 // If someone requests legalization of the new node, return itself.
89 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +000090 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000091
Chris Lattner3e928bb2005-01-07 07:47:09 +000092public:
93
Chris Lattner9c32d3b2005-01-23 04:42:50 +000094 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000095
96 /// Run - While there is still lowering to do, perform a pass over the DAG.
97 /// Most regularization can be done in a single pass, but targets that require
98 /// large values to be split into registers multiple times (e.g. i64 -> 4x
99 /// i16) require iteration for these values (the first iteration will demote
100 /// to i32, the second will demote to i16).
101 void Run() {
102 do {
103 NeedsAnotherIteration = false;
104 LegalizeDAG();
105 } while (NeedsAnotherIteration);
106 }
107
108 /// getTypeAction - Return how we should legalize values of this type, either
109 /// it is already legal or we need to expand it into multiple registers of
110 /// smaller integer type, or we need to promote it to a larger type.
111 LegalizeAction getTypeAction(MVT::ValueType VT) const {
112 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
113 }
114
115 /// isTypeLegal - Return true if this type is legal on this target.
116 ///
117 bool isTypeLegal(MVT::ValueType VT) const {
118 return getTypeAction(VT) == Legal;
119 }
120
121private:
122 void LegalizeDAG();
123
124 SDOperand LegalizeOp(SDOperand O);
125 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000126 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000127
Chris Lattner77e77a62005-01-21 06:05:23 +0000128 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
129 SDOperand &Hi);
130 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
131 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000132
Jim Laskey6269ed12005-08-17 00:39:29 +0000133 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
134 SDOperand LegalOp,
135 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000136 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
137 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000138 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
139 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000140
Chris Lattnere34b3962005-01-19 04:19:40 +0000141 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
142 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000143 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
144 SDOperand &Lo, SDOperand &Hi);
145 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000146 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000147
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000148 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
149
Chris Lattner3e928bb2005-01-07 07:47:09 +0000150 SDOperand getIntPtrConstant(uint64_t Val) {
151 return DAG.getConstant(Val, TLI.getPointerTy());
152 }
153};
154}
155
Chris Lattnerb89175f2005-11-19 05:51:46 +0000156static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000157 switch (VecOp) {
158 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000159 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
160 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
161 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
162 }
163}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000164
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000165SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
166 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
167 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000168 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000169 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000170}
171
Jim Laskey6269ed12005-08-17 00:39:29 +0000172/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
173/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000174/// we expand it. At this point, we know that the result and operand types are
175/// legal for the target.
Jim Laskey6269ed12005-08-17 00:39:29 +0000176SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
177 SDOperand Op0,
178 MVT::ValueType DestVT) {
179 if (Op0.getValueType() == MVT::i32) {
180 // simple 32-bit [signed|unsigned] integer to float/double expansion
181
182 // get the stack frame index of a 8 byte buffer
183 MachineFunction &MF = DAG.getMachineFunction();
184 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
185 // get address of 8 byte buffer
186 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
187 // word offset constant for Hi/Lo address computation
188 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
189 // set up Hi and Lo (into buffer) address based on endian
190 SDOperand Hi, Lo;
191 if (TLI.isLittleEndian()) {
192 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
193 Lo = StackSlot;
194 } else {
195 Hi = StackSlot;
196 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
197 }
198 // if signed map to unsigned space
199 SDOperand Op0Mapped;
200 if (isSigned) {
201 // constant used to invert sign bit (signed to unsigned mapping)
202 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
203 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
204 } else {
205 Op0Mapped = Op0;
206 }
207 // store the lo of the constructed double - based on integer input
208 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
209 Op0Mapped, Lo, DAG.getSrcValue(NULL));
210 // initial hi portion of constructed double
211 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
212 // store the hi of the constructed double - biased exponent
213 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
214 InitialHi, Hi, DAG.getSrcValue(NULL));
215 // load the constructed double
216 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
217 DAG.getSrcValue(NULL));
218 // FP constant to bias correct the final result
Jim Laskey02659d22005-08-17 17:42:52 +0000219 SDOperand Bias = DAG.getConstantFP(isSigned ?
220 BitsToDouble(0x4330000080000000ULL)
221 : BitsToDouble(0x4330000000000000ULL),
Jim Laskey6269ed12005-08-17 00:39:29 +0000222 MVT::f64);
223 // subtract the bias
Chris Lattner01b3d732005-09-28 22:28:18 +0000224 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskey6269ed12005-08-17 00:39:29 +0000225 // final result
226 SDOperand Result;
227 // handle final rounding
228 if (DestVT == MVT::f64) {
229 // do nothing
230 Result = Sub;
231 } else {
232 // if f32 then cast to f32
233 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
234 }
Chris Lattner69a889e2005-12-20 00:53:54 +0000235 return LegalizeOp(Result);
Jim Laskey6269ed12005-08-17 00:39:29 +0000236 }
237 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnercad063f2005-07-16 00:19:57 +0000238 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000239
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000240 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
241 DAG.getConstant(0, Op0.getValueType()),
242 ISD::SETLT);
Chris Lattnercad063f2005-07-16 00:19:57 +0000243 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
244 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
245 SignSet, Four, Zero);
Jeff Cohen00b168892005-07-27 06:12:32 +0000246
Jim Laskey6269ed12005-08-17 00:39:29 +0000247 // If the sign bit of the integer is set, the large number will be treated
248 // as a negative number. To counteract this, the dynamic code adds an
249 // offset depending on the data type.
Chris Lattnerf4d32722005-07-18 04:31:14 +0000250 uint64_t FF;
251 switch (Op0.getValueType()) {
252 default: assert(0 && "Unsupported integer type!");
253 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
254 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
255 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
256 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
257 }
Chris Lattnercad063f2005-07-16 00:19:57 +0000258 if (TLI.isLittleEndian()) FF <<= 32;
259 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000260
Chris Lattner5839bf22005-08-26 17:15:30 +0000261 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnercad063f2005-07-16 00:19:57 +0000262 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
263 SDOperand FudgeInReg;
264 if (DestVT == MVT::f32)
265 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
266 DAG.getSrcValue(NULL));
267 else {
268 assert(DestVT == MVT::f64 && "Unexpected conversion");
269 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
270 DAG.getEntryNode(), CPIdx,
271 DAG.getSrcValue(NULL), MVT::f32));
272 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000273
Chris Lattner69a889e2005-12-20 00:53:54 +0000274 return LegalizeOp(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
Chris Lattnercad063f2005-07-16 00:19:57 +0000275}
276
Chris Lattner149c58c2005-08-16 18:17:10 +0000277/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner1618beb2005-07-29 00:11:56 +0000278/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000279/// we promote it. At this point, we know that the result and operand types are
280/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
281/// operation that takes a larger input.
Nate Begeman5a8441e2005-07-16 02:02:34 +0000282SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
283 MVT::ValueType DestVT,
284 bool isSigned) {
Chris Lattnercad063f2005-07-16 00:19:57 +0000285 // First step, figure out the appropriate *INT_TO_FP operation to use.
286 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen00b168892005-07-27 06:12:32 +0000287
Chris Lattnercad063f2005-07-16 00:19:57 +0000288 unsigned OpToUse = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000289
Chris Lattnercad063f2005-07-16 00:19:57 +0000290 // Scan for the appropriate larger type to use.
291 while (1) {
292 NewInTy = (MVT::ValueType)(NewInTy+1);
293 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000294
Chris Lattnercad063f2005-07-16 00:19:57 +0000295 // If the target supports SINT_TO_FP of this type, use it.
296 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
297 default: break;
298 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000299 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000300 break; // Can't use this datatype.
301 // FALL THROUGH.
302 case TargetLowering::Custom:
303 OpToUse = ISD::SINT_TO_FP;
304 break;
305 }
306 if (OpToUse) break;
Nate Begeman5a8441e2005-07-16 02:02:34 +0000307 if (isSigned) continue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000308
Chris Lattnercad063f2005-07-16 00:19:57 +0000309 // If the target supports UINT_TO_FP of this type, use it.
310 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
311 default: break;
312 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000313 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000314 break; // Can't use this datatype.
315 // FALL THROUGH.
316 case TargetLowering::Custom:
317 OpToUse = ISD::UINT_TO_FP;
318 break;
319 }
320 if (OpToUse) break;
Jeff Cohen00b168892005-07-27 06:12:32 +0000321
Chris Lattnercad063f2005-07-16 00:19:57 +0000322 // Otherwise, try a larger type.
323 }
324
Chris Lattnercad063f2005-07-16 00:19:57 +0000325 // Okay, we found the operation and type to use. Zero extend our input to the
326 // desired type then run the operation on it.
Chris Lattner69a889e2005-12-20 00:53:54 +0000327 SDOperand N = DAG.getNode(OpToUse, DestVT,
Nate Begeman5a8441e2005-07-16 02:02:34 +0000328 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
329 NewInTy, LegalOp));
Chris Lattner69a889e2005-12-20 00:53:54 +0000330 // Make sure to legalize any nodes we create here.
331 return LegalizeOp(N);
Chris Lattnercad063f2005-07-16 00:19:57 +0000332}
333
Chris Lattner1618beb2005-07-29 00:11:56 +0000334/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
335/// FP_TO_*INT operation of the specified operand when the target requests that
336/// we promote it. At this point, we know that the result and operand types are
337/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
338/// operation that returns a larger result.
339SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
340 MVT::ValueType DestVT,
341 bool isSigned) {
342 // First step, figure out the appropriate FP_TO*INT operation to use.
343 MVT::ValueType NewOutTy = DestVT;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000344
Chris Lattner1618beb2005-07-29 00:11:56 +0000345 unsigned OpToUse = 0;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000346
Chris Lattner1618beb2005-07-29 00:11:56 +0000347 // Scan for the appropriate larger type to use.
348 while (1) {
349 NewOutTy = (MVT::ValueType)(NewOutTy+1);
350 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000351
Chris Lattner1618beb2005-07-29 00:11:56 +0000352 // If the target supports FP_TO_SINT returning this type, use it.
353 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
354 default: break;
355 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000356 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000357 break; // Can't use this datatype.
358 // FALL THROUGH.
359 case TargetLowering::Custom:
360 OpToUse = ISD::FP_TO_SINT;
361 break;
362 }
363 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000364
Chris Lattner1618beb2005-07-29 00:11:56 +0000365 // If the target supports FP_TO_UINT of this type, use it.
366 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
367 default: break;
368 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000369 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000370 break; // Can't use this datatype.
371 // FALL THROUGH.
372 case TargetLowering::Custom:
373 OpToUse = ISD::FP_TO_UINT;
374 break;
375 }
376 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000377
Chris Lattner1618beb2005-07-29 00:11:56 +0000378 // Otherwise, try a larger type.
379 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000380
Chris Lattner1618beb2005-07-29 00:11:56 +0000381 // Okay, we found the operation and type to use. Truncate the result of the
382 // extended FP_TO_*INT operation to the desired size.
Chris Lattner69a889e2005-12-20 00:53:54 +0000383 SDOperand N = DAG.getNode(ISD::TRUNCATE, DestVT,
384 DAG.getNode(OpToUse, NewOutTy, LegalOp));
385 // Make sure to legalize any nodes we create here in the next pass.
386 return LegalizeOp(N);
Chris Lattner1618beb2005-07-29 00:11:56 +0000387}
388
Chris Lattner32fca002005-10-06 01:20:27 +0000389/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
390/// not been visited yet and if all of its operands have already been visited.
391static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
392 std::map<SDNode*, unsigned> &Visited) {
393 if (++Visited[N] != N->getNumOperands())
394 return; // Haven't visited all operands yet
395
396 Order.push_back(N);
397
398 if (N->hasOneUse()) { // Tail recurse in common case.
399 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
400 return;
401 }
402
403 // Now that we have N in, add anything that uses it if all of their operands
404 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000405 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
406 ComputeTopDownOrdering(*UI, Order, Visited);
407}
408
Chris Lattner1618beb2005-07-29 00:11:56 +0000409
Chris Lattner3e928bb2005-01-07 07:47:09 +0000410void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattnerab510a72005-10-02 17:49:46 +0000411 // The legalize process is inherently a bottom-up recursive process (users
412 // legalize their uses before themselves). Given infinite stack space, we
413 // could just start legalizing on the root and traverse the whole graph. In
414 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000415 // blocks. To avoid this problem, compute an ordering of the nodes where each
416 // node is only legalized after all of its operands are legalized.
417 std::map<SDNode*, unsigned> Visited;
418 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000419
Chris Lattner32fca002005-10-06 01:20:27 +0000420 // Compute ordering from all of the leaves in the graphs, those (like the
421 // entry node) that have no operands.
422 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
423 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000424 if (I->getNumOperands() == 0) {
425 Visited[I] = 0 - 1U;
426 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000427 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000428 }
429
Chris Lattnerde202b32005-11-09 23:47:37 +0000430 assert(Order.size() == Visited.size() &&
431 Order.size() ==
432 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000433 "Error: DAG is cyclic!");
434 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000435
Chris Lattner32fca002005-10-06 01:20:27 +0000436 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
437 SDNode *N = Order[i];
438 switch (getTypeAction(N->getValueType(0))) {
439 default: assert(0 && "Bad type action!");
440 case Legal:
441 LegalizeOp(SDOperand(N, 0));
442 break;
443 case Promote:
444 PromoteOp(SDOperand(N, 0));
445 break;
446 case Expand: {
447 SDOperand X, Y;
448 ExpandOp(SDOperand(N, 0), X, Y);
449 break;
450 }
451 }
452 }
453
454 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000455 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000456 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
457 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000458
459 ExpandedNodes.clear();
460 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000461 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000462
463 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000464 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000465}
466
467SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000468 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000469 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000470 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000471
Chris Lattner3e928bb2005-01-07 07:47:09 +0000472 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000473 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000474 if (Node->getNumValues() > 1) {
475 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
476 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000477 case Legal: break; // Nothing to do.
478 case Expand: {
479 SDOperand T1, T2;
480 ExpandOp(Op.getValue(i), T1, T2);
481 assert(LegalizedNodes.count(Op) &&
482 "Expansion didn't add legal operands!");
483 return LegalizedNodes[Op];
484 }
485 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000486 PromoteOp(Op.getValue(i));
487 assert(LegalizedNodes.count(Op) &&
488 "Expansion didn't add legal operands!");
489 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000490 }
491 }
492
Chris Lattner45982da2005-05-12 16:53:42 +0000493 // Note that LegalizeOp may be reentered even from single-use nodes, which
494 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000495 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
496 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000497
Nate Begeman9373a812005-08-10 20:51:12 +0000498 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000499
500 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000501
502 switch (Node->getOpcode()) {
503 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000504 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
505 // If this is a target node, legalize it by legalizing the operands then
506 // passing it through.
507 std::vector<SDOperand> Ops;
508 bool Changed = false;
509 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
510 Ops.push_back(LegalizeOp(Node->getOperand(i)));
511 Changed = Changed || Node->getOperand(i) != Ops.back();
512 }
513 if (Changed)
514 if (Node->getNumValues() == 1)
515 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
516 else {
517 std::vector<MVT::ValueType> VTs(Node->value_begin(),
518 Node->value_end());
519 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
520 }
521
522 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
523 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
524 return Result.getValue(Op.ResNo);
525 }
526 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000527 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
528 assert(0 && "Do not know how to legalize this operator!");
529 abort();
530 case ISD::EntryToken:
531 case ISD::FrameIndex:
Chris Lattner32fca002005-10-06 01:20:27 +0000532 case ISD::TargetFrameIndex:
533 case ISD::Register:
534 case ISD::TargetConstant:
Nate Begeman28a6b022005-12-10 02:36:00 +0000535 case ISD::TargetConstantPool:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000536 case ISD::GlobalAddress:
Chris Lattnerb9debbf2005-11-17 05:52:24 +0000537 case ISD::TargetGlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000538 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000539 case ISD::ConstantPool: // Nothing to do.
Chris Lattner32fca002005-10-06 01:20:27 +0000540 case ISD::BasicBlock:
541 case ISD::CONDCODE:
542 case ISD::VALUETYPE:
543 case ISD::SRCVALUE:
Chris Lattner36ce6912005-11-29 06:21:05 +0000544 case ISD::STRING:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000545 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
546 default: assert(0 && "This action is not supported yet!");
547 case TargetLowering::Custom: {
548 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
549 if (Tmp.Val) {
550 Result = LegalizeOp(Tmp);
551 break;
552 }
553 } // FALLTHROUGH if the target doesn't want to lower this op after all.
554 case TargetLowering::Legal:
555 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
556 break;
557 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000558 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000559 case ISD::AssertSext:
560 case ISD::AssertZext:
561 Tmp1 = LegalizeOp(Node->getOperand(0));
562 if (Tmp1 != Node->getOperand(0))
563 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
564 Node->getOperand(1));
565 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000566 case ISD::MERGE_VALUES:
567 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner69a52152005-01-14 22:38:01 +0000568 case ISD::CopyFromReg:
569 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000570 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000571 if (Node->getNumValues() == 2) {
Chris Lattner7310fb12005-12-18 15:27:43 +0000572 if (Tmp1 != Node->getOperand(0))
573 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000574 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattner7310fb12005-12-18 15:27:43 +0000575 Node->getValueType(0));
576 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000577 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
578 if (Node->getNumOperands() == 3)
579 Tmp2 = LegalizeOp(Node->getOperand(2));
580 if (Tmp1 != Node->getOperand(0) ||
581 (Node->getNumOperands() == 3 && Tmp2 != Node->getOperand(2)))
Chris Lattner7310fb12005-12-18 15:27:43 +0000582 Result = DAG.getCopyFromReg(Tmp1,
583 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
584 Node->getValueType(0), Tmp2);
585 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
586 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000587 // Since CopyFromReg produces two values, make sure to remember that we
588 // legalized both of them.
589 AddLegalizedOperand(Op.getValue(0), Result);
590 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
591 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000592 case ISD::UNDEF: {
593 MVT::ValueType VT = Op.getValueType();
594 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000595 default: assert(0 && "This action is not supported yet!");
596 case TargetLowering::Expand:
597 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000598 if (MVT::isInteger(VT))
599 Result = DAG.getConstant(0, VT);
600 else if (MVT::isFloatingPoint(VT))
601 Result = DAG.getConstantFP(0, VT);
602 else
603 assert(0 && "Unknown value type!");
604 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000605 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000606 break;
607 }
608 break;
609 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000610
611 case ISD::LOCATION:
612 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
613 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
614
615 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
616 case TargetLowering::Promote:
617 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000618 case TargetLowering::Expand: {
Jim Laskeye81aecb2005-12-21 20:51:37 +0000619 if (TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other)) {
620 MachineDebugInfo &DebugInfo = DAG.getMachineFunction().getDebugInfo();
621 std::vector<SDOperand> Ops;
622 Ops.push_back(Tmp1); // chain
623 Ops.push_back(Node->getOperand(1)); // line #
624 Ops.push_back(Node->getOperand(2)); // col #
625 const std::string &fname =
626 cast<StringSDNode>(Node->getOperand(3))->getValue();
627 const std::string &dirname =
628 cast<StringSDNode>(Node->getOperand(4))->getValue();
629 unsigned id = DebugInfo.RecordSource(fname, dirname);
630 Ops.push_back(DAG.getConstant(id, MVT::i32)); // source file id
631 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
632 } else {
633 Result = Tmp1; // chain
634 }
Chris Lattnere7736732005-12-18 23:54:29 +0000635 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner36ce6912005-11-29 06:21:05 +0000636 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000637 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000638 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000639 if (Tmp1 != Node->getOperand(0) ||
640 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000641 std::vector<SDOperand> Ops;
642 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000643 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
644 Ops.push_back(Node->getOperand(1)); // line # must be legal.
645 Ops.push_back(Node->getOperand(2)); // col # must be legal.
646 } else {
647 // Otherwise promote them.
648 Ops.push_back(PromoteOp(Node->getOperand(1)));
649 Ops.push_back(PromoteOp(Node->getOperand(2)));
650 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000651 Ops.push_back(Node->getOperand(3)); // filename must be legal.
652 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
653 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
654 }
655 break;
656 }
657 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000658
659 case ISD::DEBUG_LOC:
660 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
661 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
662 case TargetLowering::Promote:
663 case TargetLowering::Expand:
664 default: assert(0 && "This action is not supported yet!");
665 case TargetLowering::Legal:
666 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
667 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
668 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
669 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
670
671 if (Tmp1 != Node->getOperand(0) ||
672 Tmp2 != Node->getOperand(1) ||
673 Tmp3 != Node->getOperand(2) ||
674 Tmp4 != Node->getOperand(3)) {
675 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
676 }
677 break;
678 }
679 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000680
Chris Lattner3e928bb2005-01-07 07:47:09 +0000681 case ISD::Constant:
682 // We know we don't need to expand constants here, constants only have one
683 // value and we check that it is fine above.
684
685 // FIXME: Maybe we should handle things like targets that don't support full
686 // 32-bit immediates?
687 break;
688 case ISD::ConstantFP: {
689 // Spill FP immediates to the constant pool if the target cannot directly
690 // codegen them. Targets often have some immediate values that can be
691 // efficiently generated into an FP register without a load. We explicitly
692 // leave these constants as ConstantFP nodes for the target to deal with.
693
694 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
695
696 // Check to see if this FP immediate is already legal.
697 bool isLegal = false;
698 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
699 E = TLI.legal_fpimm_end(); I != E; ++I)
700 if (CFP->isExactlyValue(*I)) {
701 isLegal = true;
702 break;
703 }
704
705 if (!isLegal) {
706 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000707 bool Extend = false;
708
709 // If a FP immediate is precise when represented as a float, we put it
710 // into the constant pool as a float, even if it's is statically typed
711 // as a double.
712 MVT::ValueType VT = CFP->getValueType(0);
713 bool isDouble = VT == MVT::f64;
714 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
715 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000716 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
717 // Only do this if the target has a native EXTLOAD instruction from
718 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000719 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000720 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
721 VT = MVT::f32;
722 Extend = true;
723 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000724
Nate Begeman28a6b022005-12-10 02:36:00 +0000725 SDOperand CPIdx =
726 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000727 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000728 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
729 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000730 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000731 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
732 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000733 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000734 }
735 break;
736 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000737 case ISD::ConstantVec: {
738 // We assume that vector constants are not legal, and will be immediately
739 // spilled to the constant pool.
740 //
741 // FIXME: revisit this when we have some kind of mechanism by which targets
742 // can decided legality of vector constants, of which there may be very
743 // many.
744 //
745 // Create a ConstantPacked, and put it in the constant pool.
746 std::vector<Constant*> CV;
747 MVT::ValueType VT = Node->getValueType(0);
748 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
749 SDOperand OpN = Node->getOperand(I);
750 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
751 if (MVT::isFloatingPoint(VT))
752 CV.push_back(ConstantFP::get(OpNTy,
753 cast<ConstantFPSDNode>(OpN)->getValue()));
754 else
755 CV.push_back(ConstantUInt::get(OpNTy,
756 cast<ConstantSDNode>(OpN)->getValue()));
757 }
758 Constant *CP = ConstantPacked::get(CV);
Nate Begemand7d746f2005-12-13 03:03:23 +0000759 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000760 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
761 break;
762 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000763 case ISD::TokenFactor:
764 if (Node->getNumOperands() == 2) {
765 bool Changed = false;
766 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
767 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
768 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
769 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
770 } else {
771 std::vector<SDOperand> Ops;
772 bool Changed = false;
773 // Legalize the operands.
774 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
775 SDOperand Op = Node->getOperand(i);
776 Ops.push_back(LegalizeOp(Op));
777 Changed |= Ops[i] != Op;
778 }
779 if (Changed)
780 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000781 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000782 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000783
Chris Lattner16cd04d2005-05-12 23:24:06 +0000784 case ISD::CALLSEQ_START:
785 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000786 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000787 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000788 Tmp2 = Node->getOperand(0);
Nate Begeman1aa19722005-10-04 02:10:55 +0000789 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000790 Node->setAdjCallChain(Tmp1);
Nate Begeman27d404c2005-10-04 00:37:37 +0000791
Chris Lattner16cd04d2005-05-12 23:24:06 +0000792 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000793 // nodes are treated specially and are mutated in place. This makes the dag
794 // legalization process more efficient and also makes libcall insertion
795 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000796 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000797 case ISD::DYNAMIC_STACKALLOC:
798 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
799 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
800 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
801 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000802 Tmp3 != Node->getOperand(2)) {
803 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
804 std::vector<SDOperand> Ops;
805 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
806 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
807 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000808 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000809
810 // Since this op produces two values, make sure to remember that we
811 // legalized both of them.
812 AddLegalizedOperand(SDOperand(Node, 0), Result);
813 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
814 return Result.getValue(Op.ResNo);
815
Chris Lattnerd71c0412005-05-13 18:43:43 +0000816 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000817 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000818 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
819 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000820
821 bool Changed = false;
822 std::vector<SDOperand> Ops;
823 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
824 Ops.push_back(LegalizeOp(Node->getOperand(i)));
825 Changed |= Ops.back() != Node->getOperand(i);
826 }
827
828 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000829 std::vector<MVT::ValueType> RetTyVTs;
830 RetTyVTs.reserve(Node->getNumValues());
831 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000832 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000833 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
834 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000835 } else {
836 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000837 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000838 // Since calls produce multiple values, make sure to remember that we
839 // legalized all of them.
840 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
841 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
842 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000843 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000844 case ISD::BR:
845 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
846 if (Tmp1 != Node->getOperand(0))
847 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
848 break;
849
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000850 case ISD::BRCOND:
851 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000852
Chris Lattner47e92232005-01-18 19:27:06 +0000853 switch (getTypeAction(Node->getOperand(1).getValueType())) {
854 case Expand: assert(0 && "It's impossible to expand bools");
855 case Legal:
856 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
857 break;
858 case Promote:
859 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
860 break;
861 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000862
863 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
864 default: assert(0 && "This action is not supported yet!");
865 case TargetLowering::Expand:
866 // Expand brcond's setcc into its constituent parts and create a BR_CC
867 // Node.
868 if (Tmp2.getOpcode() == ISD::SETCC) {
869 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
870 Tmp2.getOperand(0), Tmp2.getOperand(1),
871 Node->getOperand(2));
872 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000873 // Make sure the condition is either zero or one. It may have been
874 // promoted from something else.
875 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
876
Nate Begeman7cbd5252005-08-16 19:49:35 +0000877 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
878 DAG.getCondCode(ISD::SETNE), Tmp2,
879 DAG.getConstant(0, Tmp2.getValueType()),
880 Node->getOperand(2));
881 }
Chris Lattnere7736732005-12-18 23:54:29 +0000882 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000883 break;
Evan Cheng898101c2005-12-19 23:12:38 +0000884 case TargetLowering::Custom: {
885 SDOperand Tmp =
886 TLI.LowerOperation(DAG.getNode(ISD::BRCOND, Node->getValueType(0),
887 Tmp1, Tmp2, Node->getOperand(2)), DAG);
888 if (Tmp.Val) {
889 Result = LegalizeOp(Tmp);
890 break;
891 }
892 // FALLTHROUGH if the target thinks it is legal.
893 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000894 case TargetLowering::Legal:
895 // Basic block destination (Op#2) is always legal.
896 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
897 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
898 Node->getOperand(2));
899 break;
900 }
901 break;
902 case ISD::BR_CC:
903 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner181b7a32005-12-17 23:46:46 +0000904 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000905 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
906 Node->getOperand(2), // LHS
907 Node->getOperand(3), // RHS
908 Node->getOperand(1)));
909 // If we get a SETCC back from legalizing the SETCC node we just
910 // created, then use its LHS, RHS, and CC directly in creating a new
911 // node. Otherwise, select between the true and false value based on
912 // comparing the result of the legalized with zero.
913 if (Tmp2.getOpcode() == ISD::SETCC) {
914 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
915 Tmp2.getOperand(0), Tmp2.getOperand(1),
916 Node->getOperand(4));
917 } else {
918 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
919 DAG.getCondCode(ISD::SETNE),
920 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
921 Node->getOperand(4));
922 }
Chris Lattner181b7a32005-12-17 23:46:46 +0000923 break;
924 }
925
926 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
927 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
928
929 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
930 default: assert(0 && "Unexpected action for BR_CC!");
931 case TargetLowering::Custom: {
932 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
933 Tmp2, Tmp3, Node->getOperand(4));
934 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
935 if (Tmp4.Val) {
936 Result = LegalizeOp(Tmp4);
937 break;
938 }
939 } // FALLTHROUGH if the target doesn't want to lower this op after all.
940 case TargetLowering::Legal:
941 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
942 Tmp3 != Node->getOperand(3)) {
943 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
944 Tmp2, Tmp3, Node->getOperand(4));
945 }
946 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000947 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000948 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000949 case ISD::BRCONDTWOWAY:
950 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
951 switch (getTypeAction(Node->getOperand(1).getValueType())) {
952 case Expand: assert(0 && "It's impossible to expand bools");
953 case Legal:
954 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
955 break;
956 case Promote:
957 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
958 break;
959 }
960 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
961 // pair.
962 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
963 case TargetLowering::Promote:
964 default: assert(0 && "This action is not supported yet!");
965 case TargetLowering::Legal:
966 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
967 std::vector<SDOperand> Ops;
968 Ops.push_back(Tmp1);
969 Ops.push_back(Tmp2);
970 Ops.push_back(Node->getOperand(2));
971 Ops.push_back(Node->getOperand(3));
972 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
973 }
974 break;
975 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000976 // If BRTWOWAY_CC is legal for this target, then simply expand this node
977 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
978 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000979 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000980 if (Tmp2.getOpcode() == ISD::SETCC) {
981 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
982 Tmp2.getOperand(0), Tmp2.getOperand(1),
983 Node->getOperand(2), Node->getOperand(3));
984 } else {
985 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
986 DAG.getConstant(0, Tmp2.getValueType()),
987 Node->getOperand(2), Node->getOperand(3));
988 }
989 } else {
990 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000991 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000992 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
993 }
Chris Lattnere7736732005-12-18 23:54:29 +0000994 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner411e8882005-04-09 03:30:19 +0000995 break;
996 }
997 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000998 case ISD::BRTWOWAY_CC:
999 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001000 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001001 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1002 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1003 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1004 Tmp3 != Node->getOperand(3)) {
1005 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1006 Node->getOperand(4), Node->getOperand(5));
1007 }
1008 break;
1009 } else {
1010 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1011 Node->getOperand(2), // LHS
1012 Node->getOperand(3), // RHS
1013 Node->getOperand(1)));
1014 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1015 // pair.
1016 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1017 default: assert(0 && "This action is not supported yet!");
1018 case TargetLowering::Legal:
1019 // If we get a SETCC back from legalizing the SETCC node we just
1020 // created, then use its LHS, RHS, and CC directly in creating a new
1021 // node. Otherwise, select between the true and false value based on
1022 // comparing the result of the legalized with zero.
1023 if (Tmp2.getOpcode() == ISD::SETCC) {
1024 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1025 Tmp2.getOperand(0), Tmp2.getOperand(1),
1026 Node->getOperand(4), Node->getOperand(5));
1027 } else {
1028 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1029 DAG.getConstant(0, Tmp2.getValueType()),
1030 Node->getOperand(4), Node->getOperand(5));
1031 }
1032 break;
1033 case TargetLowering::Expand:
1034 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1035 Node->getOperand(4));
1036 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1037 break;
1038 }
Chris Lattnerf9dee6a2005-12-21 19:40:42 +00001039 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +00001040 }
1041 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001042 case ISD::LOAD:
1043 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1044 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001045
Chris Lattner3e928bb2005-01-07 07:47:09 +00001046 if (Tmp1 != Node->getOperand(0) ||
1047 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001048 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1049 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001050 else
1051 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001052
Chris Lattner8afc48e2005-01-07 22:28:47 +00001053 // Since loads produce two values, make sure to remember that we legalized
1054 // both of them.
1055 AddLegalizedOperand(SDOperand(Node, 0), Result);
1056 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1057 return Result.getValue(Op.ResNo);
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001058
Chris Lattner0f69b292005-01-15 06:18:18 +00001059 case ISD::EXTLOAD:
1060 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001061 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001062 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1063 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001064
Chris Lattner5f056bf2005-07-10 01:55:33 +00001065 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001066 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001067 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001068 case TargetLowering::Promote:
1069 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001070 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1071 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001072 // Since loads produce two values, make sure to remember that we legalized
1073 // both of them.
1074 AddLegalizedOperand(SDOperand(Node, 0), Result);
1075 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1076 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001077
Chris Lattner01ff7212005-04-10 22:54:25 +00001078 case TargetLowering::Legal:
1079 if (Tmp1 != Node->getOperand(0) ||
1080 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +00001081 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1082 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +00001083 else
1084 Result = SDOperand(Node, 0);
1085
1086 // Since loads produce two values, make sure to remember that we legalized
1087 // both of them.
1088 AddLegalizedOperand(SDOperand(Node, 0), Result);
1089 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1090 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +00001091 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001092 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001093 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1094 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001095 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnere7736732005-12-18 23:54:29 +00001096 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001097 Load = LegalizeOp(Load);
1098 AddLegalizedOperand(SDOperand(Node, 0), Result);
1099 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001100 if (Op.ResNo)
1101 return Load.getValue(1);
1102 return Result;
1103 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001104 assert(Node->getOpcode() != ISD::EXTLOAD &&
1105 "EXTLOAD should always be supported!");
1106 // Turn the unsupported load into an EXTLOAD followed by an explicit
1107 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001108 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1109 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001110 SDOperand ValRes;
1111 if (Node->getOpcode() == ISD::SEXTLOAD)
1112 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001113 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001114 else
1115 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnere7736732005-12-18 23:54:29 +00001116 Result = LegalizeOp(Result); // Relegalize new nodes.
1117 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001118 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1119 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattner01ff7212005-04-10 22:54:25 +00001120 if (Op.ResNo)
1121 return Result.getValue(1);
1122 return ValRes;
1123 }
1124 assert(0 && "Unreachable");
1125 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001126 case ISD::EXTRACT_ELEMENT: {
1127 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1128 switch (getTypeAction(OpTy)) {
1129 default:
1130 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1131 break;
1132 case Legal:
1133 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1134 // 1 -> Hi
1135 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1136 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1137 TLI.getShiftAmountTy()));
1138 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1139 } else {
1140 // 0 -> Lo
1141 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1142 Node->getOperand(0));
1143 }
1144 Result = LegalizeOp(Result);
1145 break;
1146 case Expand:
1147 // Get both the low and high parts.
1148 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1149 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1150 Result = Tmp2; // 1 -> Hi
1151 else
1152 Result = Tmp1; // 0 -> Lo
1153 break;
1154 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001155 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001156 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001157
1158 case ISD::CopyToReg:
1159 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001160
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001161 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001162 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001163 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001164 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001165 if (Node->getNumValues() == 1) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001166 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1167 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1168 Node->getOperand(1), Tmp2);
1169 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001170 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1171 if (Node->getNumOperands() == 4)
1172 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattner7310fb12005-12-18 15:27:43 +00001173 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001174 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001175 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1176 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1177 }
1178
1179 // Since this produces two values, make sure to remember that we legalized
1180 // both of them.
1181 AddLegalizedOperand(SDOperand(Node, 0), Result);
1182 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1183 return Result.getValue(Op.ResNo);
1184 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001185 break;
1186
1187 case ISD::RET:
1188 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1189 switch (Node->getNumOperands()) {
1190 case 2: // ret val
1191 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1192 case Legal:
1193 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001194 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +00001195 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1196 break;
1197 case Expand: {
1198 SDOperand Lo, Hi;
1199 ExpandOp(Node->getOperand(1), Lo, Hi);
1200 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001201 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001202 }
1203 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001204 Tmp2 = PromoteOp(Node->getOperand(1));
1205 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1206 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001207 }
1208 break;
1209 case 1: // ret void
1210 if (Tmp1 != Node->getOperand(0))
1211 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1212 break;
1213 default: { // ret <values>
1214 std::vector<SDOperand> NewValues;
1215 NewValues.push_back(Tmp1);
1216 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1217 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1218 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001219 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001220 break;
1221 case Expand: {
1222 SDOperand Lo, Hi;
1223 ExpandOp(Node->getOperand(i), Lo, Hi);
1224 NewValues.push_back(Lo);
1225 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001226 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001227 }
1228 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001229 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001230 }
1231 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1232 break;
1233 }
1234 }
1235 break;
1236 case ISD::STORE:
1237 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1238 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1239
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001240 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001241 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001242 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001243 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001244 DAG.getConstant(FloatToBits(CFP->getValue()),
1245 MVT::i32),
1246 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001247 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001248 } else {
1249 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001250 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001251 DAG.getConstant(DoubleToBits(CFP->getValue()),
1252 MVT::i64),
1253 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001254 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001255 }
Chris Lattner84734ce2005-02-22 07:23:39 +00001256 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001257 }
1258
Chris Lattner3e928bb2005-01-07 07:47:09 +00001259 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1260 case Legal: {
1261 SDOperand Val = LegalizeOp(Node->getOperand(1));
1262 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1263 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001264 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1265 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001266 break;
1267 }
1268 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001269 // Truncate the value and store the result.
1270 Tmp3 = PromoteOp(Node->getOperand(1));
1271 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001272 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001273 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001274 break;
1275
Chris Lattner3e928bb2005-01-07 07:47:09 +00001276 case Expand:
1277 SDOperand Lo, Hi;
Nate Begemanab48be32005-11-22 18:16:00 +00001278 unsigned IncrementSize;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001279 ExpandOp(Node->getOperand(1), Lo, Hi);
1280
1281 if (!TLI.isLittleEndian())
1282 std::swap(Lo, Hi);
1283
Chris Lattneredb1add2005-05-11 04:51:16 +00001284 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1285 Node->getOperand(3));
Nate Begemanab48be32005-11-22 18:16:00 +00001286 // If this is a vector type, then we have to calculate the increment as
1287 // the product of the element size in bytes, and the number of elements
1288 // in the high half of the vector.
1289 if (MVT::Vector == Hi.getValueType()) {
1290 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1291 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1292 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1293 } else {
1294 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1295 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001296 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1297 getIntPtrConstant(IncrementSize));
1298 assert(isTypeLegal(Tmp2.getValueType()) &&
1299 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001300 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001301 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1302 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001303 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1304 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001305 }
1306 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001307 case ISD::PCMARKER:
1308 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001309 if (Tmp1 != Node->getOperand(0))
1310 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001311 break;
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001312 case ISD::READCYCLECOUNTER:
1313 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthcde0f5c2005-12-02 06:08:08 +00001314 if (Tmp1 != Node->getOperand(0)) {
1315 std::vector<MVT::ValueType> rtypes;
1316 std::vector<SDOperand> rvals;
1317 rtypes.push_back(MVT::i64);
1318 rtypes.push_back(MVT::Other);
1319 rvals.push_back(Tmp1);
1320 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1321 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001322
1323 // Since rdcc produce two values, make sure to remember that we legalized
1324 // both of them.
1325 AddLegalizedOperand(SDOperand(Node, 0), Result);
1326 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1327 return Result.getValue(Op.ResNo);
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001328
Chris Lattner0f69b292005-01-15 06:18:18 +00001329 case ISD::TRUNCSTORE:
1330 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1331 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1332
1333 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1334 case Legal:
1335 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001336
1337 // The only promote case we handle is TRUNCSTORE:i1 X into
1338 // -> TRUNCSTORE:i8 (and X, 1)
1339 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1340 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1341 TargetLowering::Promote) {
1342 // Promote the bool to a mask then store.
1343 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1344 DAG.getConstant(1, Tmp2.getValueType()));
1345 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1346 Node->getOperand(3), DAG.getValueType(MVT::i8));
1347
1348 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1349 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001350 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001351 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001352 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001353 break;
1354 case Promote:
1355 case Expand:
1356 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1357 }
1358 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001359 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001360 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1361 case Expand: assert(0 && "It's impossible to expand bools");
1362 case Legal:
1363 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1364 break;
1365 case Promote:
1366 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1367 break;
1368 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001369 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001370 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001371
Nate Begemanb942a3d2005-08-23 04:29:48 +00001372 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001373 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001374 case TargetLowering::Expand:
1375 if (Tmp1.getOpcode() == ISD::SETCC) {
1376 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1377 Tmp2, Tmp3,
1378 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1379 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001380 // Make sure the condition is either zero or one. It may have been
1381 // promoted from something else.
1382 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001383 Result = DAG.getSelectCC(Tmp1,
1384 DAG.getConstant(0, Tmp1.getValueType()),
1385 Tmp2, Tmp3, ISD::SETNE);
1386 }
Chris Lattnere7736732005-12-18 23:54:29 +00001387 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman9373a812005-08-10 20:51:12 +00001388 break;
Evan Cheng7df96d62005-12-17 01:21:05 +00001389 case TargetLowering::Custom: {
1390 SDOperand Tmp =
1391 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1392 Tmp1, Tmp2, Tmp3), DAG);
1393 if (Tmp.Val) {
1394 Result = LegalizeOp(Tmp);
1395 break;
1396 }
1397 // FALLTHROUGH if the target thinks it is legal.
1398 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001399 case TargetLowering::Legal:
1400 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1401 Tmp3 != Node->getOperand(2))
1402 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1403 Tmp1, Tmp2, Tmp3);
1404 break;
1405 case TargetLowering::Promote: {
1406 MVT::ValueType NVT =
1407 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1408 unsigned ExtOp, TruncOp;
1409 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001410 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001411 TruncOp = ISD::TRUNCATE;
1412 } else {
1413 ExtOp = ISD::FP_EXTEND;
1414 TruncOp = ISD::FP_ROUND;
1415 }
1416 // Promote each of the values to the new type.
1417 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1418 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1419 // Perform the larger operation, then round down.
1420 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1421 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1422 break;
1423 }
1424 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001425 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001426 case ISD::SELECT_CC:
1427 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1428 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1429
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001430 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001431 // Everything is legal, see if we should expand this op or something.
1432 switch (TLI.getOperationAction(ISD::SELECT_CC,
1433 Node->getOperand(0).getValueType())) {
1434 default: assert(0 && "This action is not supported yet!");
1435 case TargetLowering::Custom: {
1436 SDOperand Tmp =
1437 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1438 Node->getOperand(0),
1439 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001440 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001441 if (Tmp.Val) {
1442 Result = LegalizeOp(Tmp);
1443 break;
1444 }
1445 } // FALLTHROUGH if the target can't lower this operation after all.
1446 case TargetLowering::Legal:
1447 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1448 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1449 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1450 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001451 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1,Tmp2,
Chris Lattner23004e52005-08-26 00:23:59 +00001452 Tmp3, Tmp4, Node->getOperand(4));
1453 }
1454 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001455 }
1456 break;
1457 } else {
1458 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1459 Node->getOperand(0), // LHS
1460 Node->getOperand(1), // RHS
1461 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001462 // If we get a SETCC back from legalizing the SETCC node we just
1463 // created, then use its LHS, RHS, and CC directly in creating a new
1464 // node. Otherwise, select between the true and false value based on
1465 // comparing the result of the legalized with zero.
1466 if (Tmp1.getOpcode() == ISD::SETCC) {
1467 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1468 Tmp1.getOperand(0), Tmp1.getOperand(1),
1469 Tmp3, Tmp4, Tmp1.getOperand(2));
1470 } else {
1471 Result = DAG.getSelectCC(Tmp1,
1472 DAG.getConstant(0, Tmp1.getValueType()),
1473 Tmp3, Tmp4, ISD::SETNE);
1474 }
Nate Begeman9373a812005-08-10 20:51:12 +00001475 }
1476 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001477 case ISD::SETCC:
1478 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1479 case Legal:
1480 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1481 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001482 break;
1483 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001484 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1485 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1486
1487 // If this is an FP compare, the operands have already been extended.
1488 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1489 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001490 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001491
1492 // Otherwise, we have to insert explicit sign or zero extends. Note
1493 // that we could insert sign extends for ALL conditions, but zero extend
1494 // is cheaper on many machines (an AND instead of two shifts), so prefer
1495 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001496 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001497 default: assert(0 && "Unknown integer comparison!");
1498 case ISD::SETEQ:
1499 case ISD::SETNE:
1500 case ISD::SETUGE:
1501 case ISD::SETUGT:
1502 case ISD::SETULE:
1503 case ISD::SETULT:
1504 // ALL of these operations will work if we either sign or zero extend
1505 // the operands (including the unsigned comparisons!). Zero extend is
1506 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001507 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1508 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001509 break;
1510 case ISD::SETGE:
1511 case ISD::SETGT:
1512 case ISD::SETLT:
1513 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001514 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1515 DAG.getValueType(VT));
1516 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1517 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001518 break;
1519 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001520 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001521 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001522 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001523 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1524 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1525 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001526 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001527 case ISD::SETEQ:
1528 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001529 if (RHSLo == RHSHi)
1530 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1531 if (RHSCST->isAllOnesValue()) {
1532 // Comparison to -1.
1533 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001534 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001535 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001536 }
1537
Chris Lattner3e928bb2005-01-07 07:47:09 +00001538 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1539 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1540 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001541 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001542 break;
1543 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001544 // If this is a comparison of the sign bit, just look at the top part.
1545 // X > -1, x < 0
1546 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001547 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001548 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001549 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001550 (CST->isAllOnesValue()))) { // X > -1
1551 Tmp1 = LHSHi;
1552 Tmp2 = RHSHi;
1553 break;
1554 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001555
Chris Lattner3e928bb2005-01-07 07:47:09 +00001556 // FIXME: This generated code sucks.
1557 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001558 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001559 default: assert(0 && "Unknown integer setcc!");
1560 case ISD::SETLT:
1561 case ISD::SETULT: LowCC = ISD::SETULT; break;
1562 case ISD::SETGT:
1563 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1564 case ISD::SETLE:
1565 case ISD::SETULE: LowCC = ISD::SETULE; break;
1566 case ISD::SETGE:
1567 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1568 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001569
Chris Lattner3e928bb2005-01-07 07:47:09 +00001570 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1571 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1572 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1573
1574 // NOTE: on targets without efficient SELECT of bools, we can always use
1575 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001576 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1577 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1578 Node->getOperand(2));
1579 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001580 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1581 Result, Tmp1, Tmp2));
Chris Lattner69a889e2005-12-20 00:53:54 +00001582 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001583 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001584 }
1585 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001586
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001587 switch(TLI.getOperationAction(ISD::SETCC,
1588 Node->getOperand(0).getValueType())) {
Nate Begemanb942a3d2005-08-23 04:29:48 +00001589 default:
1590 assert(0 && "Cannot handle this action for SETCC yet!");
1591 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001592 case TargetLowering::Promote: {
1593 // First step, figure out the appropriate operation to use.
1594 // Allow SETCC to not be supported for all legal data types
1595 // Mostly this targets FP
1596 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1597 MVT::ValueType OldVT = NewInTy;
1598
1599 // Scan for the appropriate larger type to use.
1600 while (1) {
1601 NewInTy = (MVT::ValueType)(NewInTy+1);
1602
1603 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1604 "Fell off of the edge of the integer world");
1605 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1606 "Fell off of the edge of the floating point world");
1607
1608 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001609 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001610 break;
1611 }
1612 if (MVT::isInteger(NewInTy))
1613 assert(0 && "Cannot promote Legal Integer SETCC yet");
1614 else {
1615 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1616 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1617 }
1618
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001619 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1620 Node->getOperand(2));
1621 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001622 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001623 case TargetLowering::Custom: {
1624 SDOperand Tmp =
1625 TLI.LowerOperation(DAG.getNode(ISD::SETCC, Node->getValueType(0),
1626 Tmp1, Tmp2, Node->getOperand(2)), DAG);
1627 if (Tmp.Val) {
1628 Result = LegalizeOp(Tmp);
1629 break;
1630 }
1631 // FALLTHROUGH if the target thinks it is legal.
1632 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001633 case TargetLowering::Legal:
1634 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1635 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1636 Node->getOperand(2));
1637 break;
1638 case TargetLowering::Expand:
1639 // Expand a setcc node into a select_cc of the same condition, lhs, and
1640 // rhs that selects between const 1 (true) and const 0 (false).
1641 MVT::ValueType VT = Node->getValueType(0);
1642 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1643 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1644 Node->getOperand(2));
1645 Result = LegalizeOp(Result);
1646 break;
1647 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001648 break;
1649
Chris Lattnere1bd8222005-01-11 05:57:22 +00001650 case ISD::MEMSET:
1651 case ISD::MEMCPY:
1652 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001653 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001654 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1655
1656 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1657 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1658 case Expand: assert(0 && "Cannot expand a byte!");
1659 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001660 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001661 break;
1662 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001663 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001664 break;
1665 }
1666 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001667 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001668 }
Chris Lattner272455b2005-02-02 03:44:41 +00001669
1670 SDOperand Tmp4;
1671 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001672 case Expand: {
1673 // Length is too big, just take the lo-part of the length.
1674 SDOperand HiPart;
1675 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1676 break;
1677 }
Chris Lattnere5605212005-01-28 22:29:18 +00001678 case Legal:
1679 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001680 break;
1681 case Promote:
1682 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001683 break;
1684 }
1685
1686 SDOperand Tmp5;
1687 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1688 case Expand: assert(0 && "Cannot expand this yet!");
1689 case Legal:
1690 Tmp5 = LegalizeOp(Node->getOperand(4));
1691 break;
1692 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001693 Tmp5 = PromoteOp(Node->getOperand(4));
1694 break;
1695 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001696
1697 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1698 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001699 case TargetLowering::Custom: {
1700 SDOperand Tmp =
1701 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1702 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1703 if (Tmp.Val) {
1704 Result = LegalizeOp(Tmp);
1705 break;
1706 }
1707 // FALLTHROUGH if the target thinks it is legal.
1708 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001709 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001710 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1711 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1712 Tmp5 != Node->getOperand(4)) {
1713 std::vector<SDOperand> Ops;
1714 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1715 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1716 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1717 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001718 break;
1719 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001720 // Otherwise, the target does not support this operation. Lower the
1721 // operation to an explicit libcall as appropriate.
1722 MVT::ValueType IntPtr = TLI.getPointerTy();
1723 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1724 std::vector<std::pair<SDOperand, const Type*> > Args;
1725
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001726 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001727 if (Node->getOpcode() == ISD::MEMSET) {
1728 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1729 // Extend the ubyte argument to be an int value for the call.
1730 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1731 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1732 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1733
1734 FnName = "memset";
1735 } else if (Node->getOpcode() == ISD::MEMCPY ||
1736 Node->getOpcode() == ISD::MEMMOVE) {
1737 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1738 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1739 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1740 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1741 } else {
1742 assert(0 && "Unknown op!");
1743 }
Chris Lattner45982da2005-05-12 16:53:42 +00001744
Chris Lattnere1bd8222005-01-11 05:57:22 +00001745 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001746 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001747 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner69a889e2005-12-20 00:53:54 +00001748 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001749 break;
1750 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001751 }
1752 break;
1753 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001754
1755 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001756 Tmp1 = LegalizeOp(Node->getOperand(0));
1757 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001758
Chris Lattner3e011362005-05-14 07:45:46 +00001759 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1760 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1761 std::vector<SDOperand> Ops;
1762 Ops.push_back(Tmp1);
1763 Ops.push_back(Tmp2);
1764 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1765 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001766 Result = SDOperand(Node, 0);
1767 // Since these produce two values, make sure to remember that we legalized
1768 // both of them.
1769 AddLegalizedOperand(SDOperand(Node, 0), Result);
1770 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1771 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001772 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001773 Tmp1 = LegalizeOp(Node->getOperand(0));
1774 Tmp2 = LegalizeOp(Node->getOperand(1));
1775 Tmp3 = LegalizeOp(Node->getOperand(2));
1776 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1777 Tmp3 != Node->getOperand(2))
1778 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1779 break;
1780
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001781 case ISD::READIO:
1782 Tmp1 = LegalizeOp(Node->getOperand(0));
1783 Tmp2 = LegalizeOp(Node->getOperand(1));
1784
1785 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1786 case TargetLowering::Custom:
1787 default: assert(0 && "This action not implemented for this operation!");
1788 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001789 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1790 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1791 std::vector<SDOperand> Ops;
1792 Ops.push_back(Tmp1);
1793 Ops.push_back(Tmp2);
1794 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1795 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001796 Result = SDOperand(Node, 0);
1797 break;
1798 case TargetLowering::Expand:
1799 // Replace this with a load from memory.
1800 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1801 Node->getOperand(1), DAG.getSrcValue(NULL));
1802 Result = LegalizeOp(Result);
1803 break;
1804 }
1805
1806 // Since these produce two values, make sure to remember that we legalized
1807 // both of them.
1808 AddLegalizedOperand(SDOperand(Node, 0), Result);
1809 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1810 return Result.getValue(Op.ResNo);
1811
1812 case ISD::WRITEIO:
1813 Tmp1 = LegalizeOp(Node->getOperand(0));
1814 Tmp2 = LegalizeOp(Node->getOperand(1));
1815 Tmp3 = LegalizeOp(Node->getOperand(2));
1816
1817 switch (TLI.getOperationAction(Node->getOpcode(),
1818 Node->getOperand(1).getValueType())) {
1819 case TargetLowering::Custom:
1820 default: assert(0 && "This action not implemented for this operation!");
1821 case TargetLowering::Legal:
1822 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1823 Tmp3 != Node->getOperand(2))
1824 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1825 break;
1826 case TargetLowering::Expand:
1827 // Replace this with a store to memory.
1828 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1829 Node->getOperand(1), Node->getOperand(2),
1830 DAG.getSrcValue(NULL));
1831 Result = LegalizeOp(Result);
1832 break;
1833 }
1834 break;
1835
Chris Lattner84f67882005-01-20 18:52:28 +00001836 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001837 case ISD::SUB_PARTS:
1838 case ISD::SHL_PARTS:
1839 case ISD::SRA_PARTS:
1840 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001841 std::vector<SDOperand> Ops;
1842 bool Changed = false;
1843 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1844 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1845 Changed |= Ops.back() != Node->getOperand(i);
1846 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001847 if (Changed) {
1848 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1849 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1850 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001851
1852 // Since these produce multiple values, make sure to remember that we
1853 // legalized all of them.
1854 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1855 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1856 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001857 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001858
1859 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001860 case ISD::ADD:
1861 case ISD::SUB:
1862 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001863 case ISD::MULHS:
1864 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001865 case ISD::UDIV:
1866 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001867 case ISD::AND:
1868 case ISD::OR:
1869 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001870 case ISD::SHL:
1871 case ISD::SRL:
1872 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001873 case ISD::FADD:
1874 case ISD::FSUB:
1875 case ISD::FMUL:
1876 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001877 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001878 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1879 case Expand: assert(0 && "Not possible");
1880 case Legal:
1881 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1882 break;
1883 case Promote:
1884 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1885 break;
1886 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001887 if (Tmp1 != Node->getOperand(0) ||
1888 Tmp2 != Node->getOperand(1))
1889 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1890 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001891
Nate Begeman419f8b62005-10-18 00:27:41 +00001892 case ISD::BUILD_PAIR: {
1893 MVT::ValueType PairTy = Node->getValueType(0);
1894 // TODO: handle the case where the Lo and Hi operands are not of legal type
1895 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1896 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1897 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1898 case TargetLowering::Legal:
1899 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1900 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1901 break;
1902 case TargetLowering::Promote:
1903 case TargetLowering::Custom:
1904 assert(0 && "Cannot promote/custom this yet!");
1905 case TargetLowering::Expand:
1906 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1907 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1908 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1909 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1910 TLI.getShiftAmountTy()));
1911 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1912 break;
1913 }
1914 break;
1915 }
1916
Nate Begemanc105e192005-04-06 00:23:54 +00001917 case ISD::UREM:
1918 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001919 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001920 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1921 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1922 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1923 case TargetLowering::Legal:
1924 if (Tmp1 != Node->getOperand(0) ||
1925 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001926 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001927 Tmp2);
1928 break;
1929 case TargetLowering::Promote:
1930 case TargetLowering::Custom:
1931 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001932 case TargetLowering::Expand:
1933 if (MVT::isInteger(Node->getValueType(0))) {
1934 MVT::ValueType VT = Node->getValueType(0);
1935 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1936 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1937 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1938 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1939 } else {
1940 // Floating point mod -> fmod libcall.
1941 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1942 SDOperand Dummy;
1943 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001944 }
1945 break;
1946 }
1947 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001948
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001949 case ISD::CTPOP:
1950 case ISD::CTTZ:
1951 case ISD::CTLZ:
1952 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1953 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1954 case TargetLowering::Legal:
1955 if (Tmp1 != Node->getOperand(0))
1956 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1957 break;
1958 case TargetLowering::Promote: {
1959 MVT::ValueType OVT = Tmp1.getValueType();
1960 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001961
1962 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001963 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1964 // Perform the larger operation, then subtract if needed.
1965 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1966 switch(Node->getOpcode())
1967 {
1968 case ISD::CTPOP:
1969 Result = Tmp1;
1970 break;
1971 case ISD::CTTZ:
1972 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001973 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1974 DAG.getConstant(getSizeInBits(NVT), NVT),
1975 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001976 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001977 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1978 break;
1979 case ISD::CTLZ:
1980 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001981 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1982 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001983 getSizeInBits(OVT), NVT));
1984 break;
1985 }
1986 break;
1987 }
1988 case TargetLowering::Custom:
1989 assert(0 && "Cannot custom handle this yet!");
1990 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001991 switch(Node->getOpcode())
1992 {
1993 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001994 static const uint64_t mask[6] = {
1995 0x5555555555555555ULL, 0x3333333333333333ULL,
1996 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1997 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1998 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001999 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002000 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2001 unsigned len = getSizeInBits(VT);
2002 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002003 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002004 Tmp2 = DAG.getConstant(mask[i], VT);
2005 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002006 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002007 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
2008 DAG.getNode(ISD::AND, VT,
2009 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
2010 Tmp2));
2011 }
2012 Result = Tmp1;
2013 break;
2014 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00002015 case ISD::CTLZ: {
2016 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002017 x = x | (x >> 1);
2018 x = x | (x >> 2);
2019 ...
2020 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00002021 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002022 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00002023
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002024 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2025 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00002026 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2027 unsigned len = getSizeInBits(VT);
2028 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2029 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002030 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00002031 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2032 }
2033 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002034 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00002035 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002036 }
2037 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00002038 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002039 // unless the target has ctlz but not ctpop, in which case we use:
2040 // { return 32 - nlz(~x & (x-1)); }
2041 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002042 MVT::ValueType VT = Tmp1.getValueType();
2043 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002044 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002045 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2046 DAG.getNode(ISD::SUB, VT, Tmp1,
2047 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002048 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002049 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2050 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00002051 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002052 DAG.getConstant(getSizeInBits(VT), VT),
2053 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2054 } else {
2055 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2056 }
Chris Lattner18aa6802005-05-11 05:27:09 +00002057 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002058 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002059 default:
2060 assert(0 && "Cannot expand this yet!");
2061 break;
2062 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002063 break;
2064 }
2065 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002066
Chris Lattner2c8086f2005-04-02 05:00:07 +00002067 // Unary operators
2068 case ISD::FABS:
2069 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002070 case ISD::FSQRT:
2071 case ISD::FSIN:
2072 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002073 Tmp1 = LegalizeOp(Node->getOperand(0));
2074 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2075 case TargetLowering::Legal:
2076 if (Tmp1 != Node->getOperand(0))
2077 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2078 break;
2079 case TargetLowering::Promote:
2080 case TargetLowering::Custom:
2081 assert(0 && "Cannot promote/custom handle this yet!");
2082 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002083 switch(Node->getOpcode()) {
2084 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00002085 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2086 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002087 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00002088 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002089 break;
2090 }
2091 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002092 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2093 MVT::ValueType VT = Node->getValueType(0);
2094 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002095 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002096 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2097 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2098 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002099 break;
2100 }
2101 case ISD::FSQRT:
2102 case ISD::FSIN:
2103 case ISD::FCOS: {
2104 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002105 const char *FnName = 0;
2106 switch(Node->getOpcode()) {
2107 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2108 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2109 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2110 default: assert(0 && "Unreachable!");
2111 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002112 SDOperand Dummy;
2113 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002114 break;
2115 }
2116 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002117 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00002118 }
2119 break;
2120 }
2121 break;
2122
2123 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002124 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002125 case ISD::UINT_TO_FP: {
2126 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002127 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2128 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002129 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002130 Node->getOperand(0).getValueType())) {
2131 default: assert(0 && "Unknown operation action!");
2132 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002133 Result = ExpandLegalINT_TO_FP(isSigned,
2134 LegalizeOp(Node->getOperand(0)),
2135 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002136 AddLegalizedOperand(Op, Result);
2137 return Result;
2138 case TargetLowering::Promote:
2139 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2140 Node->getValueType(0),
2141 isSigned);
2142 AddLegalizedOperand(Op, Result);
2143 return Result;
2144 case TargetLowering::Legal:
2145 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002146 case TargetLowering::Custom: {
2147 Tmp1 = LegalizeOp(Node->getOperand(0));
2148 SDOperand Tmp =
2149 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2150 Tmp = TLI.LowerOperation(Tmp, DAG);
2151 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002152 Tmp = LegalizeOp(Tmp); // Relegalize input.
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002153 AddLegalizedOperand(Op, Tmp);
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002154 return Tmp;
2155 } else {
2156 assert(0 && "Target Must Lower this");
2157 }
2158 }
Andrew Lenharthf4b32782005-06-27 23:28:32 +00002159 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002160
Chris Lattner3e928bb2005-01-07 07:47:09 +00002161 Tmp1 = LegalizeOp(Node->getOperand(0));
2162 if (Tmp1 != Node->getOperand(0))
2163 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2164 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002165 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002166 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2167 Node->getValueType(0), Node->getOperand(0));
2168 break;
2169 case Promote:
2170 if (isSigned) {
2171 Result = PromoteOp(Node->getOperand(0));
2172 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2173 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2174 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2175 } else {
2176 Result = PromoteOp(Node->getOperand(0));
2177 Result = DAG.getZeroExtendInReg(Result,
2178 Node->getOperand(0).getValueType());
2179 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00002180 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002181 break;
2182 }
2183 break;
2184 }
2185 case ISD::TRUNCATE:
2186 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2187 case Legal:
2188 Tmp1 = LegalizeOp(Node->getOperand(0));
2189 if (Tmp1 != Node->getOperand(0))
2190 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2191 break;
2192 case Expand:
2193 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2194
2195 // Since the result is legal, we should just be able to truncate the low
2196 // part of the source.
2197 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2198 break;
2199 case Promote:
2200 Result = PromoteOp(Node->getOperand(0));
2201 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2202 break;
2203 }
2204 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002205
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002206 case ISD::FP_TO_SINT:
2207 case ISD::FP_TO_UINT:
2208 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2209 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002210 Tmp1 = LegalizeOp(Node->getOperand(0));
2211
Chris Lattner1618beb2005-07-29 00:11:56 +00002212 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2213 default: assert(0 && "Unknown operation action!");
2214 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002215 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2216 SDOperand True, False;
2217 MVT::ValueType VT = Node->getOperand(0).getValueType();
2218 MVT::ValueType NVT = Node->getValueType(0);
2219 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2220 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2221 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2222 Node->getOperand(0), Tmp2, ISD::SETLT);
2223 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2224 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002225 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002226 Tmp2));
2227 False = DAG.getNode(ISD::XOR, NVT, False,
2228 DAG.getConstant(1ULL << ShiftAmt, NVT));
2229 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Chris Lattner69a889e2005-12-20 00:53:54 +00002230 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman2d56e722005-08-14 18:38:32 +00002231 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00002232 } else {
2233 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2234 }
2235 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002236 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002237 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00002238 Node->getOpcode() == ISD::FP_TO_SINT);
2239 AddLegalizedOperand(Op, Result);
2240 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00002241 case TargetLowering::Custom: {
2242 SDOperand Tmp =
2243 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2244 Tmp = TLI.LowerOperation(Tmp, DAG);
2245 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002246 Tmp = LegalizeOp(Tmp);
Chris Lattner07dffd62005-08-26 00:14:16 +00002247 AddLegalizedOperand(Op, Tmp);
Chris Lattner507f7522005-08-29 17:30:00 +00002248 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00002249 } else {
2250 // The target thinks this is legal afterall.
2251 break;
2252 }
2253 }
Chris Lattner1618beb2005-07-29 00:11:56 +00002254 case TargetLowering::Legal:
2255 break;
2256 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002257
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002258 if (Tmp1 != Node->getOperand(0))
2259 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2260 break;
2261 case Expand:
2262 assert(0 && "Shouldn't need to expand other operators here!");
2263 case Promote:
2264 Result = PromoteOp(Node->getOperand(0));
2265 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2266 break;
2267 }
2268 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002269
Chris Lattner13c78e22005-09-02 00:18:10 +00002270 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002271 case ISD::ZERO_EXTEND:
2272 case ISD::SIGN_EXTEND:
2273 case ISD::FP_EXTEND:
2274 case ISD::FP_ROUND:
2275 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2276 case Legal:
2277 Tmp1 = LegalizeOp(Node->getOperand(0));
2278 if (Tmp1 != Node->getOperand(0))
2279 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2280 break;
2281 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002282 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00002283
Chris Lattner03c85462005-01-15 05:21:40 +00002284 case Promote:
2285 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002286 case ISD::ANY_EXTEND:
2287 Result = PromoteOp(Node->getOperand(0));
2288 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2289 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002290 case ISD::ZERO_EXTEND:
2291 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002292 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002293 Result = DAG.getZeroExtendInReg(Result,
2294 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002295 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002296 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002297 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002298 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002299 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002300 Result,
2301 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002302 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002303 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002304 Result = PromoteOp(Node->getOperand(0));
2305 if (Result.getValueType() != Op.getValueType())
2306 // Dynamically dead while we have only 2 FP types.
2307 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2308 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002309 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002310 Result = PromoteOp(Node->getOperand(0));
2311 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2312 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002313 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002314 }
2315 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002316 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002317 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002318 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002319 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002320
2321 // If this operation is not supported, convert it to a shl/shr or load/store
2322 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002323 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2324 default: assert(0 && "This action not supported for this op yet!");
2325 case TargetLowering::Legal:
2326 if (Tmp1 != Node->getOperand(0))
2327 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00002328 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002329 break;
2330 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002331 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002332 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002333 // NOTE: we could fall back on load/store here too for targets without
2334 // SAR. However, it is doubtful that any exist.
2335 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2336 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002337 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002338 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2339 Node->getOperand(0), ShiftCst);
2340 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2341 Result, ShiftCst);
2342 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2343 // The only way we can lower this is to turn it into a STORETRUNC,
2344 // EXTLOAD pair, targetting a temporary location (a stack slot).
2345
2346 // NOTE: there is a choice here between constantly creating new stack
2347 // slots and always reusing the same one. We currently always create
2348 // new ones, as reuse may inhibit scheduling.
2349 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2350 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2351 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2352 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002353 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002354 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2355 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2356 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002357 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002358 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002359 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2360 Result, StackSlot, DAG.getSrcValue(NULL),
2361 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002362 } else {
2363 assert(0 && "Unknown op");
2364 }
2365 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002366 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002367 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002368 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002369 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002370 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002371
Chris Lattner45982da2005-05-12 16:53:42 +00002372 // Note that LegalizeOp may be reentered even from single-use nodes, which
2373 // means that we always must cache transformed nodes.
2374 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002375 return Result;
2376}
2377
Chris Lattner8b6fa222005-01-15 22:16:26 +00002378/// PromoteOp - Given an operation that produces a value in an invalid type,
2379/// promote it to compute the value into a larger type. The produced value will
2380/// have the correct bits for the low portion of the register, but no guarantee
2381/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002382SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2383 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002384 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002385 assert(getTypeAction(VT) == Promote &&
2386 "Caller should expand or legalize operands that are not promotable!");
2387 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2388 "Cannot promote to smaller type!");
2389
Chris Lattner03c85462005-01-15 05:21:40 +00002390 SDOperand Tmp1, Tmp2, Tmp3;
2391
2392 SDOperand Result;
2393 SDNode *Node = Op.Val;
2394
Chris Lattner6fdcb252005-09-02 20:32:45 +00002395 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2396 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002397
Chris Lattner0f69b292005-01-15 06:18:18 +00002398 // Promotion needs an optimization step to clean up after it, and is not
2399 // careful to avoid operations the target does not support. Make sure that
2400 // all generated operations are legalized in the next iteration.
2401 NeedsAnotherIteration = true;
2402
Chris Lattner03c85462005-01-15 05:21:40 +00002403 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002404 case ISD::CopyFromReg:
2405 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002406 default:
2407 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2408 assert(0 && "Do not know how to promote this operator!");
2409 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002410 case ISD::UNDEF:
2411 Result = DAG.getNode(ISD::UNDEF, NVT);
2412 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002413 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002414 if (VT != MVT::i1)
2415 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2416 else
2417 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002418 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2419 break;
2420 case ISD::ConstantFP:
2421 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2422 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2423 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002424
Chris Lattner82fbfb62005-01-18 02:59:52 +00002425 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002426 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002427 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2428 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002429 Result = LegalizeOp(Result);
2430 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002431
2432 case ISD::TRUNCATE:
2433 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2434 case Legal:
2435 Result = LegalizeOp(Node->getOperand(0));
2436 assert(Result.getValueType() >= NVT &&
2437 "This truncation doesn't make sense!");
2438 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2439 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2440 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002441 case Promote:
2442 // The truncation is not required, because we don't guarantee anything
2443 // about high bits anyway.
2444 Result = PromoteOp(Node->getOperand(0));
2445 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002446 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002447 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2448 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002449 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002450 }
2451 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002452 case ISD::SIGN_EXTEND:
2453 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002454 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002455 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2456 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2457 case Legal:
2458 // Input is legal? Just do extend all the way to the larger type.
2459 Result = LegalizeOp(Node->getOperand(0));
2460 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2461 break;
2462 case Promote:
2463 // Promote the reg if it's smaller.
2464 Result = PromoteOp(Node->getOperand(0));
2465 // The high bits are not guaranteed to be anything. Insert an extend.
2466 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002467 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002468 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002469 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002470 Result = DAG.getZeroExtendInReg(Result,
2471 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002472 break;
2473 }
2474 break;
2475
2476 case ISD::FP_EXTEND:
2477 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2478 case ISD::FP_ROUND:
2479 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2480 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2481 case Promote: assert(0 && "Unreachable with 2 FP types!");
2482 case Legal:
2483 // Input is legal? Do an FP_ROUND_INREG.
2484 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002485 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2486 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002487 break;
2488 }
2489 break;
2490
2491 case ISD::SINT_TO_FP:
2492 case ISD::UINT_TO_FP:
2493 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2494 case Legal:
2495 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002496 // No extra round required here.
2497 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002498 break;
2499
2500 case Promote:
2501 Result = PromoteOp(Node->getOperand(0));
2502 if (Node->getOpcode() == ISD::SINT_TO_FP)
2503 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002504 Result,
2505 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002506 else
Chris Lattner23993562005-04-13 02:38:47 +00002507 Result = DAG.getZeroExtendInReg(Result,
2508 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002509 // No extra round required here.
2510 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002511 break;
2512 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002513 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2514 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002515 // Round if we cannot tolerate excess precision.
2516 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002517 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2518 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002519 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002520 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002521 break;
2522
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002523 case ISD::SIGN_EXTEND_INREG:
2524 Result = PromoteOp(Node->getOperand(0));
2525 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2526 Node->getOperand(1));
2527 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002528 case ISD::FP_TO_SINT:
2529 case ISD::FP_TO_UINT:
2530 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2531 case Legal:
2532 Tmp1 = LegalizeOp(Node->getOperand(0));
2533 break;
2534 case Promote:
2535 // The input result is prerounded, so we don't have to do anything
2536 // special.
2537 Tmp1 = PromoteOp(Node->getOperand(0));
2538 break;
2539 case Expand:
2540 assert(0 && "not implemented");
2541 }
Nate Begemand2558e32005-08-14 01:20:53 +00002542 // If we're promoting a UINT to a larger size, check to see if the new node
2543 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2544 // we can use that instead. This allows us to generate better code for
2545 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2546 // legal, such as PowerPC.
2547 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002548 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002549 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2550 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002551 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2552 } else {
2553 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2554 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002555 break;
2556
Chris Lattner2c8086f2005-04-02 05:00:07 +00002557 case ISD::FABS:
2558 case ISD::FNEG:
2559 Tmp1 = PromoteOp(Node->getOperand(0));
2560 assert(Tmp1.getValueType() == NVT);
2561 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2562 // NOTE: we do not have to do any extra rounding here for
2563 // NoExcessFPPrecision, because we know the input will have the appropriate
2564 // precision, and these operations don't modify precision at all.
2565 break;
2566
Chris Lattnerda6ba872005-04-28 21:44:33 +00002567 case ISD::FSQRT:
2568 case ISD::FSIN:
2569 case ISD::FCOS:
2570 Tmp1 = PromoteOp(Node->getOperand(0));
2571 assert(Tmp1.getValueType() == NVT);
2572 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2573 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002574 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2575 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002576 break;
2577
Chris Lattner03c85462005-01-15 05:21:40 +00002578 case ISD::AND:
2579 case ISD::OR:
2580 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002581 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002582 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002583 case ISD::MUL:
2584 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002585 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002586 // that too is okay if they are integer operations.
2587 Tmp1 = PromoteOp(Node->getOperand(0));
2588 Tmp2 = PromoteOp(Node->getOperand(1));
2589 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2590 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002591 break;
2592 case ISD::FADD:
2593 case ISD::FSUB:
2594 case ISD::FMUL:
2595 // The input may have strange things in the top bits of the registers, but
2596 // these operations don't care.
2597 Tmp1 = PromoteOp(Node->getOperand(0));
2598 Tmp2 = PromoteOp(Node->getOperand(1));
2599 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2600 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2601
2602 // Floating point operations will give excess precision that we may not be
2603 // able to tolerate. If we DO allow excess precision, just leave it,
2604 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002605 // FIXME: Why would we need to round FP ops more than integer ones?
2606 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002607 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002608 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2609 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002610 break;
2611
Chris Lattner8b6fa222005-01-15 22:16:26 +00002612 case ISD::SDIV:
2613 case ISD::SREM:
2614 // These operators require that their input be sign extended.
2615 Tmp1 = PromoteOp(Node->getOperand(0));
2616 Tmp2 = PromoteOp(Node->getOperand(1));
2617 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002618 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2619 DAG.getValueType(VT));
2620 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2621 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002622 }
2623 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2624
2625 // Perform FP_ROUND: this is probably overly pessimistic.
2626 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002627 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2628 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002629 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002630 case ISD::FDIV:
2631 case ISD::FREM:
2632 // These operators require that their input be fp extended.
2633 Tmp1 = PromoteOp(Node->getOperand(0));
2634 Tmp2 = PromoteOp(Node->getOperand(1));
2635 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2636
2637 // Perform FP_ROUND: this is probably overly pessimistic.
2638 if (NoExcessFPPrecision)
2639 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2640 DAG.getValueType(VT));
2641 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002642
2643 case ISD::UDIV:
2644 case ISD::UREM:
2645 // These operators require that their input be zero extended.
2646 Tmp1 = PromoteOp(Node->getOperand(0));
2647 Tmp2 = PromoteOp(Node->getOperand(1));
2648 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002649 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2650 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002651 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2652 break;
2653
2654 case ISD::SHL:
2655 Tmp1 = PromoteOp(Node->getOperand(0));
2656 Tmp2 = LegalizeOp(Node->getOperand(1));
2657 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2658 break;
2659 case ISD::SRA:
2660 // The input value must be properly sign extended.
2661 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002662 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2663 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002664 Tmp2 = LegalizeOp(Node->getOperand(1));
2665 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2666 break;
2667 case ISD::SRL:
2668 // The input value must be properly zero extended.
2669 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002670 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002671 Tmp2 = LegalizeOp(Node->getOperand(1));
2672 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2673 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002674 case ISD::LOAD:
2675 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2676 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begemanded49632005-10-13 03:11:28 +00002677 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2678 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002679 // Remember that we legalized the chain.
2680 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2681 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002682 case ISD::SEXTLOAD:
2683 case ISD::ZEXTLOAD:
2684 case ISD::EXTLOAD:
2685 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2686 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner8136cda2005-10-15 20:24:07 +00002687 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2688 Node->getOperand(2),
2689 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002690 // Remember that we legalized the chain.
2691 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2692 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002693 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002694 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2695 case Expand: assert(0 && "It's impossible to expand bools");
2696 case Legal:
2697 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2698 break;
2699 case Promote:
2700 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2701 break;
2702 }
Chris Lattner03c85462005-01-15 05:21:40 +00002703 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2704 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2705 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2706 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002707 case ISD::SELECT_CC:
2708 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2709 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2710 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2711 Node->getOperand(1), Tmp2, Tmp3,
2712 Node->getOperand(4));
2713 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002714 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002715 case ISD::CALL: {
2716 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2717 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2718
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002719 std::vector<SDOperand> Ops;
2720 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2721 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2722
Chris Lattner8ac532c2005-01-16 19:46:48 +00002723 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2724 "Can only promote single result calls");
2725 std::vector<MVT::ValueType> RetTyVTs;
2726 RetTyVTs.reserve(2);
2727 RetTyVTs.push_back(NVT);
2728 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002729 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2730 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002731 Result = SDOperand(NC, 0);
2732
2733 // Insert the new chain mapping.
2734 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2735 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002736 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002737 case ISD::CTPOP:
2738 case ISD::CTTZ:
2739 case ISD::CTLZ:
2740 Tmp1 = Node->getOperand(0);
2741 //Zero extend the argument
2742 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2743 // Perform the larger operation, then subtract if needed.
2744 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2745 switch(Node->getOpcode())
2746 {
2747 case ISD::CTPOP:
2748 Result = Tmp1;
2749 break;
2750 case ISD::CTTZ:
2751 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002752 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002753 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002754 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002755 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2756 break;
2757 case ISD::CTLZ:
2758 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002759 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2760 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002761 getSizeInBits(VT), NVT));
2762 break;
2763 }
2764 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002765 }
2766
2767 assert(Result.Val && "Didn't set a result!");
2768 AddPromotedOperand(Op, Result);
2769 return Result;
2770}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002771
Chris Lattner84f67882005-01-20 18:52:28 +00002772/// ExpandAddSub - Find a clever way to expand this add operation into
2773/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002774void SelectionDAGLegalize::
2775ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2776 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002777 // Expand the subcomponents.
2778 SDOperand LHSL, LHSH, RHSL, RHSH;
2779 ExpandOp(LHS, LHSL, LHSH);
2780 ExpandOp(RHS, RHSL, RHSH);
2781
Chris Lattner84f67882005-01-20 18:52:28 +00002782 std::vector<SDOperand> Ops;
2783 Ops.push_back(LHSL);
2784 Ops.push_back(LHSH);
2785 Ops.push_back(RHSL);
2786 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002787 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2788 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002789 Hi = Lo.getValue(1);
2790}
2791
Chris Lattner5b359c62005-04-02 04:00:59 +00002792void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2793 SDOperand Op, SDOperand Amt,
2794 SDOperand &Lo, SDOperand &Hi) {
2795 // Expand the subcomponents.
2796 SDOperand LHSL, LHSH;
2797 ExpandOp(Op, LHSL, LHSH);
2798
2799 std::vector<SDOperand> Ops;
2800 Ops.push_back(LHSL);
2801 Ops.push_back(LHSH);
2802 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002803 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002804 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002805 Hi = Lo.getValue(1);
2806}
2807
2808
Chris Lattnere34b3962005-01-19 04:19:40 +00002809/// ExpandShift - Try to find a clever way to expand this shift operation out to
2810/// smaller elements. If we can't find a way that is more efficient than a
2811/// libcall on this target, return false. Otherwise, return true with the
2812/// low-parts expanded into Lo and Hi.
2813bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2814 SDOperand &Lo, SDOperand &Hi) {
2815 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2816 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002817
Chris Lattnere34b3962005-01-19 04:19:40 +00002818 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002819 SDOperand ShAmt = LegalizeOp(Amt);
2820 MVT::ValueType ShTy = ShAmt.getValueType();
2821 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2822 unsigned NVTBits = MVT::getSizeInBits(NVT);
2823
2824 // Handle the case when Amt is an immediate. Other cases are currently broken
2825 // and are disabled.
2826 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2827 unsigned Cst = CN->getValue();
2828 // Expand the incoming operand to be shifted, so that we have its parts
2829 SDOperand InL, InH;
2830 ExpandOp(Op, InL, InH);
2831 switch(Opc) {
2832 case ISD::SHL:
2833 if (Cst > VTBits) {
2834 Lo = DAG.getConstant(0, NVT);
2835 Hi = DAG.getConstant(0, NVT);
2836 } else if (Cst > NVTBits) {
2837 Lo = DAG.getConstant(0, NVT);
2838 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002839 } else if (Cst == NVTBits) {
2840 Lo = DAG.getConstant(0, NVT);
2841 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002842 } else {
2843 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2844 Hi = DAG.getNode(ISD::OR, NVT,
2845 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2846 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2847 }
2848 return true;
2849 case ISD::SRL:
2850 if (Cst > VTBits) {
2851 Lo = DAG.getConstant(0, NVT);
2852 Hi = DAG.getConstant(0, NVT);
2853 } else if (Cst > NVTBits) {
2854 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2855 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002856 } else if (Cst == NVTBits) {
2857 Lo = InH;
2858 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002859 } else {
2860 Lo = DAG.getNode(ISD::OR, NVT,
2861 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2862 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2863 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2864 }
2865 return true;
2866 case ISD::SRA:
2867 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002868 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002869 DAG.getConstant(NVTBits-1, ShTy));
2870 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002871 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002872 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002873 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002874 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002875 } else if (Cst == NVTBits) {
2876 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002877 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002878 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002879 } else {
2880 Lo = DAG.getNode(ISD::OR, NVT,
2881 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2882 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2883 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2884 }
2885 return true;
2886 }
2887 }
2888 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2889 // so disable it for now. Currently targets are handling this via SHL_PARTS
2890 // and friends.
2891 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002892
2893 // If we have an efficient select operation (or if the selects will all fold
2894 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002895 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00002896 return false;
2897
2898 SDOperand InL, InH;
2899 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002900 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2901 DAG.getConstant(NVTBits, ShTy), ShAmt);
2902
Chris Lattnere5544f82005-01-20 20:29:23 +00002903 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002904 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2905 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002906
Chris Lattnere34b3962005-01-19 04:19:40 +00002907 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2908 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2909 DAG.getConstant(NVTBits-1, ShTy));
2910 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2911 DAG.getConstant(NVTBits-1, ShTy));
2912 }
2913
2914 if (Opc == ISD::SHL) {
2915 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2916 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2917 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002918 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002919
Chris Lattnere34b3962005-01-19 04:19:40 +00002920 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2921 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2922 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002923 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002924 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2925 DAG.getConstant(32, ShTy),
2926 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002927 DAG.getConstant(0, NVT),
2928 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002929 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002930 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002931 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002932 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002933
2934 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002935 if (Opc == ISD::SRA)
2936 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2937 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002938 else
2939 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002940 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002941 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002942 }
2943 return true;
2944}
Chris Lattner77e77a62005-01-21 06:05:23 +00002945
Chris Lattner9530ddc2005-05-13 05:09:11 +00002946/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2947/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002948/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002949static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002950 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002951
Chris Lattner16cd04d2005-05-12 23:24:06 +00002952 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002953 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002954 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002955 Found = Node;
2956 return;
2957 }
2958
2959 // Otherwise, scan the operands of Node to see if any of them is a call.
2960 assert(Node->getNumOperands() != 0 &&
2961 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00002962 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002963 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002964
2965 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002966 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002967 Found);
2968}
2969
2970
Chris Lattner9530ddc2005-05-13 05:09:11 +00002971/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2972/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002973/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002974static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2975 std::set<SDNode*> &Visited) {
2976 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2977 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002978
Chris Lattner16cd04d2005-05-12 23:24:06 +00002979 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002980 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002981 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002982 Found = Node;
2983 return;
2984 }
2985
2986 // Otherwise, scan the operands of Node to see if any of them is a call.
2987 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2988 if (UI == E) return;
2989 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002990 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002991
2992 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002993 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002994}
2995
Chris Lattner9530ddc2005-05-13 05:09:11 +00002996/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002997/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002998static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002999 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003000 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00003001 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00003002 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003003
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003004 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00003005 if (TheChain.getValueType() != MVT::Other)
3006 TheChain = SDOperand(Node, 0);
Nate Begeman1aa19722005-10-04 02:10:55 +00003007 if (TheChain.getValueType() != MVT::Other)
3008 return 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003009
3010 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00003011 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003012
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003013 // Make sure to only follow users of our token chain.
3014 SDNode *User = *UI;
3015 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3016 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00003017 if (SDNode *Result = FindCallSeqEnd(User))
3018 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003019 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00003020 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003021}
3022
Chris Lattner9530ddc2005-05-13 05:09:11 +00003023/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003024/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003025static SDNode *FindCallSeqStart(SDNode *Node) {
3026 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00003027 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003028
3029 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3030 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00003031 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003032}
3033
3034
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003035/// FindInputOutputChains - If we are replacing an operation with a call we need
3036/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003037/// properly serialize the calls in the block. The returned operand is the
3038/// input chain value for the new call (e.g. the entry node or the previous
3039/// call), and OutChain is set to be the chain node to update to point to the
3040/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003041static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3042 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003043 SDNode *LatestCallSeqStart = Entry.Val;
3044 SDNode *LatestCallSeqEnd = 0;
3045 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
3046 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003047
Chris Lattner16cd04d2005-05-12 23:24:06 +00003048 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00003049 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00003050 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3051 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00003052 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003053 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00003054 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3055 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003056 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00003057 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00003058 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00003059
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003060 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00003061 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003062 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00003063 std::set<SDNode*> Visited;
3064 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003065
Chris Lattner9530ddc2005-05-13 05:09:11 +00003066 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003067 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00003068 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003069
Chris Lattner9530ddc2005-05-13 05:09:11 +00003070 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003071}
3072
Jeff Cohen00b168892005-07-27 06:12:32 +00003073/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003074void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3075 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003076 // Nothing to splice it into?
3077 if (OutChain == 0) return;
3078
3079 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3080 //OutChain->dump();
3081
3082 // Form a token factor node merging the old inval and the new inval.
3083 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3084 OutChain->getOperand(0));
3085 // Change the node to refer to the new token.
3086 OutChain->setAdjCallChain(InToken);
3087}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003088
3089
Chris Lattner77e77a62005-01-21 06:05:23 +00003090// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3091// does not fit into a register, return the lo part and set the hi part to the
3092// by-reg argument. If it does fit into a single register, return the result
3093// and leave the Hi part unset.
3094SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3095 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003096 SDNode *OutChain;
3097 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3098 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00003099 if (InChain.Val == 0)
3100 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003101
Chris Lattner77e77a62005-01-21 06:05:23 +00003102 TargetLowering::ArgListTy Args;
3103 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3104 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3105 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3106 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3107 }
3108 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003109
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003110 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003111 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003112 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003113 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3114 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003115
Chris Lattner99c25b82005-09-02 20:26:58 +00003116 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003117 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003118 default: assert(0 && "Unknown thing");
3119 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00003120 Result = CallInfo.first;
3121 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003122 case Promote:
3123 assert(0 && "Cannot promote this yet!");
3124 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003125 ExpandOp(CallInfo.first, Result, Hi);
3126 CallInfo.second = LegalizeOp(CallInfo.second);
3127 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003128 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003129
3130 SpliceCallInto(CallInfo.second, OutChain);
3131 NeedsAnotherIteration = true;
3132 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003133}
3134
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003135
Chris Lattner77e77a62005-01-21 06:05:23 +00003136/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3137/// destination type is legal.
3138SDOperand SelectionDAGLegalize::
3139ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003140 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003141 assert(getTypeAction(Source.getValueType()) == Expand &&
3142 "This is not an expansion!");
3143 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3144
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003145 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003146 assert(Source.getValueType() == MVT::i64 &&
3147 "This only works for 64-bit -> FP");
3148 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3149 // incoming integer is set. To handle this, we dynamically test to see if
3150 // it is set, and, if so, add a fudge factor.
3151 SDOperand Lo, Hi;
3152 ExpandOp(Source, Lo, Hi);
3153
Chris Lattner66de05b2005-05-13 04:45:13 +00003154 // If this is unsigned, and not supported, first perform the conversion to
3155 // signed, then adjust the result if the sign bit is set.
3156 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3157 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3158
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003159 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3160 DAG.getConstant(0, Hi.getValueType()),
3161 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003162 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3163 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3164 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003165 uint64_t FF = 0x5f800000ULL;
3166 if (TLI.isLittleEndian()) FF <<= 32;
3167 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003168
Chris Lattner5839bf22005-08-26 17:15:30 +00003169 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003170 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3171 SDOperand FudgeInReg;
3172 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003173 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3174 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003175 else {
3176 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003177 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3178 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003179 }
Chris Lattner473a9902005-09-29 06:44:39 +00003180 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003181 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003182
Chris Lattnera88a2602005-05-14 05:33:54 +00003183 // Check to see if the target has a custom way to lower this. If so, use it.
3184 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3185 default: assert(0 && "This action not implemented for this operation!");
3186 case TargetLowering::Legal:
3187 case TargetLowering::Expand:
3188 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003189 case TargetLowering::Custom: {
3190 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3191 Source), DAG);
3192 if (NV.Val)
3193 return LegalizeOp(NV);
3194 break; // The target decided this was legal after all
3195 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003196 }
3197
Chris Lattner13689e22005-05-12 07:00:44 +00003198 // Expand the source, then glue it back together for the call. We must expand
3199 // the source in case it is shared (this pass of legalize must traverse it).
3200 SDOperand SrcLo, SrcHi;
3201 ExpandOp(Source, SrcLo, SrcHi);
3202 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3203
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003204 SDNode *OutChain = 0;
3205 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3206 DAG.getEntryNode());
3207 const char *FnName = 0;
3208 if (DestTy == MVT::f32)
3209 FnName = "__floatdisf";
3210 else {
3211 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3212 FnName = "__floatdidf";
3213 }
3214
Chris Lattner77e77a62005-01-21 06:05:23 +00003215 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3216
3217 TargetLowering::ArgListTy Args;
3218 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00003219
Chris Lattner77e77a62005-01-21 06:05:23 +00003220 Args.push_back(std::make_pair(Source, ArgTy));
3221
3222 // We don't care about token chains for libcalls. We just use the entry
3223 // node as our input and ignore the output chain. This allows us to place
3224 // calls wherever we need them to satisfy data dependences.
3225 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003226
3227 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00003228 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3229 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003230
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003231 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003232 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003233}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003234
Chris Lattnere34b3962005-01-19 04:19:40 +00003235
3236
Chris Lattner3e928bb2005-01-07 07:47:09 +00003237/// ExpandOp - Expand the specified SDOperand into its two component pieces
3238/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3239/// LegalizeNodes map is filled in for any results that are not expanded, the
3240/// ExpandedNodes map is filled in for any results that are expanded, and the
3241/// Lo/Hi values are returned.
3242void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3243 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003244 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003245 SDNode *Node = Op.Val;
3246 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003247 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3248 "Cannot expand FP values!");
3249 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003250 "Cannot expand to FP value or to larger int value!");
3251
Chris Lattner6fdcb252005-09-02 20:32:45 +00003252 // See if we already expanded it.
3253 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3254 = ExpandedNodes.find(Op);
3255 if (I != ExpandedNodes.end()) {
3256 Lo = I->second.first;
3257 Hi = I->second.second;
3258 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003259 }
3260
Chris Lattner4e6c7462005-01-08 19:27:05 +00003261 // Expanding to multiple registers needs to perform an optimization step, and
3262 // is not careful to avoid operations the target does not support. Make sure
3263 // that all generated operations are legalized in the next iteration.
3264 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003265
Chris Lattner3e928bb2005-01-07 07:47:09 +00003266 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003267 case ISD::CopyFromReg:
3268 assert(0 && "CopyFromReg must be legal!");
3269 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003270 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3271 assert(0 && "Do not know how to expand this operator!");
3272 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003273 case ISD::UNDEF:
3274 Lo = DAG.getNode(ISD::UNDEF, NVT);
3275 Hi = DAG.getNode(ISD::UNDEF, NVT);
3276 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003277 case ISD::Constant: {
3278 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3279 Lo = DAG.getConstant(Cst, NVT);
3280 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3281 break;
3282 }
Nate Begemancc827e62005-12-07 19:48:11 +00003283 case ISD::ConstantVec: {
3284 unsigned NumElements = Node->getNumOperands();
3285 // If we only have two elements left in the constant vector, just break it
3286 // apart into the two scalar constants it contains. Otherwise, bisect the
3287 // ConstantVec, and return each half as a new ConstantVec.
3288 // FIXME: this is hard coded as big endian, it may have to change to support
3289 // SSE and Alpha MVI
3290 if (NumElements == 2) {
3291 Hi = Node->getOperand(0);
3292 Lo = Node->getOperand(1);
3293 } else {
3294 NumElements /= 2;
3295 std::vector<SDOperand> LoOps, HiOps;
3296 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3297 HiOps.push_back(Node->getOperand(I));
3298 LoOps.push_back(Node->getOperand(I+NumElements));
3299 }
3300 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3301 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3302 }
3303 break;
3304 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003305
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003306 case ISD::BUILD_PAIR:
3307 // Legalize both operands. FIXME: in the future we should handle the case
3308 // where the two elements are not legal.
3309 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3310 Lo = LegalizeOp(Node->getOperand(0));
3311 Hi = LegalizeOp(Node->getOperand(1));
3312 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003313
3314 case ISD::SIGN_EXTEND_INREG:
3315 ExpandOp(Node->getOperand(0), Lo, Hi);
3316 // Sign extend the lo-part.
3317 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3318 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3319 TLI.getShiftAmountTy()));
3320 // sext_inreg the low part if needed.
3321 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3322 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003323
Chris Lattneredb1add2005-05-11 04:51:16 +00003324 case ISD::CTPOP:
3325 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003326 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3327 DAG.getNode(ISD::CTPOP, NVT, Lo),
3328 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003329 Hi = DAG.getConstant(0, NVT);
3330 break;
3331
Chris Lattner39a8f332005-05-12 19:05:01 +00003332 case ISD::CTLZ: {
3333 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003334 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003335 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3336 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003337 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3338 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003339 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3340 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3341
3342 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3343 Hi = DAG.getConstant(0, NVT);
3344 break;
3345 }
3346
3347 case ISD::CTTZ: {
3348 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003349 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003350 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3351 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003352 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3353 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003354 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3355 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3356
3357 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3358 Hi = DAG.getConstant(0, NVT);
3359 break;
3360 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003361
Chris Lattner3e928bb2005-01-07 07:47:09 +00003362 case ISD::LOAD: {
3363 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3364 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003365 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003366
3367 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003368 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003369 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3370 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00003371 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003372 //are from the same instruction?
3373 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003374
3375 // Build a factor node to remember that this load is independent of the
3376 // other one.
3377 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3378 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003379
Chris Lattner3e928bb2005-01-07 07:47:09 +00003380 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00003381 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003382 if (!TLI.isLittleEndian())
3383 std::swap(Lo, Hi);
3384 break;
3385 }
Nate Begemanab48be32005-11-22 18:16:00 +00003386 case ISD::VLOAD: {
3387 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3388 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3389 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3390 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3391
3392 // If we only have two elements, turn into a pair of scalar loads.
3393 // FIXME: handle case where a vector of two elements is fine, such as
3394 // 2 x double on SSE2.
3395 if (NumElements == 2) {
3396 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3397 // Increment the pointer to the other half.
3398 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3399 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3400 getIntPtrConstant(IncrementSize));
3401 //Is this safe? declaring that the two parts of the split load
3402 //are from the same instruction?
3403 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3404 } else {
3405 NumElements /= 2; // Split the vector in half
3406 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3407 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3408 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3409 getIntPtrConstant(IncrementSize));
3410 //Is this safe? declaring that the two parts of the split load
3411 //are from the same instruction?
3412 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3413 }
3414
3415 // Build a factor node to remember that this load is independent of the
3416 // other one.
3417 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3418 Hi.getValue(1));
3419
3420 // Remember that we legalized the chain.
3421 AddLegalizedOperand(Op.getValue(1), TF);
3422 if (!TLI.isLittleEndian())
3423 std::swap(Lo, Hi);
3424 break;
3425 }
3426 case ISD::VADD:
3427 case ISD::VSUB:
3428 case ISD::VMUL: {
3429 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3430 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3431 SDOperand LL, LH, RL, RH;
3432
3433 ExpandOp(Node->getOperand(0), LL, LH);
3434 ExpandOp(Node->getOperand(1), RL, RH);
3435
3436 // If we only have two elements, turn into a pair of scalar loads.
3437 // FIXME: handle case where a vector of two elements is fine, such as
3438 // 2 x double on SSE2.
3439 if (NumElements == 2) {
3440 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3441 Lo = DAG.getNode(Opc, EVT, LL, RL);
3442 Hi = DAG.getNode(Opc, EVT, LH, RH);
3443 } else {
3444 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3445 LL.getOperand(3));
3446 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3447 LH.getOperand(3));
3448 }
3449 break;
3450 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00003451 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003452 case ISD::CALL: {
3453 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3454 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3455
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003456 bool Changed = false;
3457 std::vector<SDOperand> Ops;
3458 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3459 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3460 Changed |= Ops.back() != Node->getOperand(i);
3461 }
3462
Chris Lattner3e928bb2005-01-07 07:47:09 +00003463 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3464 "Can only expand a call once so far, not i64 -> i16!");
3465
3466 std::vector<MVT::ValueType> RetTyVTs;
3467 RetTyVTs.reserve(3);
3468 RetTyVTs.push_back(NVT);
3469 RetTyVTs.push_back(NVT);
3470 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003471 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3472 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003473 Lo = SDOperand(NC, 0);
3474 Hi = SDOperand(NC, 1);
3475
3476 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00003477 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003478 break;
3479 }
3480 case ISD::AND:
3481 case ISD::OR:
3482 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3483 SDOperand LL, LH, RL, RH;
3484 ExpandOp(Node->getOperand(0), LL, LH);
3485 ExpandOp(Node->getOperand(1), RL, RH);
3486 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3487 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3488 break;
3489 }
3490 case ISD::SELECT: {
3491 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00003492
3493 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3494 case Expand: assert(0 && "It's impossible to expand bools");
3495 case Legal:
3496 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3497 break;
3498 case Promote:
3499 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3500 break;
3501 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003502 ExpandOp(Node->getOperand(1), LL, LH);
3503 ExpandOp(Node->getOperand(2), RL, RH);
3504 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3505 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3506 break;
3507 }
Nate Begeman9373a812005-08-10 20:51:12 +00003508 case ISD::SELECT_CC: {
3509 SDOperand TL, TH, FL, FH;
3510 ExpandOp(Node->getOperand(2), TL, TH);
3511 ExpandOp(Node->getOperand(3), FL, FH);
3512 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3513 Node->getOperand(1), TL, FL, Node->getOperand(4));
3514 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3515 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003516 Lo = LegalizeOp(Lo);
3517 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003518 break;
3519 }
Nate Begeman144ff662005-10-13 17:15:37 +00003520 case ISD::SEXTLOAD: {
3521 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3522 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3523 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3524
3525 if (EVT == NVT)
3526 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3527 else
3528 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3529 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003530
3531 // Remember that we legalized the chain.
3532 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3533
Nate Begeman144ff662005-10-13 17:15:37 +00003534 // The high part is obtained by SRA'ing all but one of the bits of the lo
3535 // part.
3536 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3537 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3538 TLI.getShiftAmountTy()));
3539 Lo = LegalizeOp(Lo);
3540 Hi = LegalizeOp(Hi);
3541 break;
3542 }
3543 case ISD::ZEXTLOAD: {
3544 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3545 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3546 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3547
3548 if (EVT == NVT)
3549 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3550 else
3551 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3552 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003553
3554 // Remember that we legalized the chain.
3555 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3556
Nate Begeman144ff662005-10-13 17:15:37 +00003557 // The high part is just a zero.
Chris Lattner9ad84812005-10-13 21:44:47 +00003558 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begeman144ff662005-10-13 17:15:37 +00003559 Lo = LegalizeOp(Lo);
Chris Lattner9ad84812005-10-13 21:44:47 +00003560 break;
3561 }
3562 case ISD::EXTLOAD: {
3563 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3564 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3565 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3566
3567 if (EVT == NVT)
3568 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3569 else
3570 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3571 EVT);
3572
3573 // Remember that we legalized the chain.
3574 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3575
3576 // The high part is undefined.
3577 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3578 Lo = LegalizeOp(Lo);
Nate Begeman144ff662005-10-13 17:15:37 +00003579 break;
3580 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003581 case ISD::ANY_EXTEND: {
3582 SDOperand In;
3583 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3584 case Expand: assert(0 && "expand-expand not implemented yet!");
3585 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3586 case Promote:
3587 In = PromoteOp(Node->getOperand(0));
3588 break;
3589 }
3590
3591 // The low part is any extension of the input (which degenerates to a copy).
3592 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3593 // The high part is undefined.
3594 Hi = DAG.getNode(ISD::UNDEF, NVT);
3595 break;
3596 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003597 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003598 SDOperand In;
3599 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3600 case Expand: assert(0 && "expand-expand not implemented yet!");
3601 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3602 case Promote:
3603 In = PromoteOp(Node->getOperand(0));
3604 // Emit the appropriate sign_extend_inreg to get the value we want.
3605 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00003606 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00003607 break;
3608 }
3609
Chris Lattner3e928bb2005-01-07 07:47:09 +00003610 // The low part is just a sign extension of the input (which degenerates to
3611 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00003612 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003613
Chris Lattner3e928bb2005-01-07 07:47:09 +00003614 // The high part is obtained by SRA'ing all but one of the bits of the lo
3615 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003616 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00003617 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3618 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003619 break;
3620 }
Chris Lattner06098e02005-04-03 23:41:52 +00003621 case ISD::ZERO_EXTEND: {
3622 SDOperand In;
3623 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3624 case Expand: assert(0 && "expand-expand not implemented yet!");
3625 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3626 case Promote:
3627 In = PromoteOp(Node->getOperand(0));
3628 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003629 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003630 break;
3631 }
3632
Chris Lattner3e928bb2005-01-07 07:47:09 +00003633 // The low part is just a zero extension of the input (which degenerates to
3634 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003635 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003636
Chris Lattner3e928bb2005-01-07 07:47:09 +00003637 // The high part is just a zero.
3638 Hi = DAG.getConstant(0, NVT);
3639 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003640 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003641
Chris Lattner308575b2005-11-20 22:56:56 +00003642 case ISD::READCYCLECOUNTER: {
3643 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3644 TargetLowering::Custom &&
3645 "Must custom expand ReadCycleCounter");
3646 SDOperand T = TLI.LowerOperation(Op, DAG);
3647 assert(T.Val && "Node must be custom expanded!");
3648 Lo = LegalizeOp(T.getValue(0));
3649 Hi = LegalizeOp(T.getValue(1));
3650 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3651 LegalizeOp(T.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003652 break;
Chris Lattner308575b2005-11-20 22:56:56 +00003653 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003654
Chris Lattner4e6c7462005-01-08 19:27:05 +00003655 // These operators cannot be expanded directly, emit them as calls to
3656 // library functions.
3657 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003658 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003659 SDOperand Op;
3660 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3661 case Expand: assert(0 && "cannot expand FP!");
3662 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3663 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3664 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003665
Chris Lattnerf20d1832005-07-30 01:40:57 +00003666 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3667
Chris Lattner80a3e942005-07-29 00:33:32 +00003668 // Now that the custom expander is done, expand the result, which is still
3669 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003670 if (Op.Val) {
3671 ExpandOp(Op, Lo, Hi);
3672 break;
3673 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003674 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003675
Chris Lattner4e6c7462005-01-08 19:27:05 +00003676 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003677 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003678 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003679 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003680 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003681
Chris Lattner4e6c7462005-01-08 19:27:05 +00003682 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003683 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3684 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3685 LegalizeOp(Node->getOperand(0)));
3686 // Now that the custom expander is done, expand the result, which is still
3687 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003688 Op = TLI.LowerOperation(Op, DAG);
3689 if (Op.Val) {
3690 ExpandOp(Op, Lo, Hi);
3691 break;
3692 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003693 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003694
Chris Lattner4e6c7462005-01-08 19:27:05 +00003695 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003696 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003697 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003698 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003699 break;
3700
Chris Lattnere34b3962005-01-19 04:19:40 +00003701 case ISD::SHL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003702 // If the target wants custom lowering, do so.
3703 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3704 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3705 LegalizeOp(Node->getOperand(1)));
3706 Op = TLI.LowerOperation(Op, DAG);
3707 if (Op.Val) {
3708 // Now that the custom expander is done, expand the result, which is
3709 // still VT.
3710 ExpandOp(Op, Lo, Hi);
3711 break;
3712 }
3713 }
3714
Chris Lattnere34b3962005-01-19 04:19:40 +00003715 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003716 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003717 break;
Chris Lattner47599822005-04-02 03:38:53 +00003718
3719 // If this target supports SHL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003720 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003721 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3722 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003723 break;
3724 }
3725
Chris Lattnere34b3962005-01-19 04:19:40 +00003726 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003727 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003728 break;
3729
3730 case ISD::SRA:
Chris Lattner50ec8972005-08-31 19:01:53 +00003731 // If the target wants custom lowering, do so.
3732 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3733 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3734 LegalizeOp(Node->getOperand(1)));
3735 Op = TLI.LowerOperation(Op, DAG);
3736 if (Op.Val) {
3737 // Now that the custom expander is done, expand the result, which is
3738 // still VT.
3739 ExpandOp(Op, Lo, Hi);
3740 break;
3741 }
3742 }
3743
Chris Lattnere34b3962005-01-19 04:19:40 +00003744 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003745 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003746 break;
Chris Lattner47599822005-04-02 03:38:53 +00003747
3748 // If this target supports SRA_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003749 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003750 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3751 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003752 break;
3753 }
3754
Chris Lattnere34b3962005-01-19 04:19:40 +00003755 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003756 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003757 break;
3758 case ISD::SRL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003759 // If the target wants custom lowering, do so.
3760 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3761 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3762 LegalizeOp(Node->getOperand(1)));
3763 Op = TLI.LowerOperation(Op, DAG);
3764 if (Op.Val) {
3765 // Now that the custom expander is done, expand the result, which is
3766 // still VT.
3767 ExpandOp(Op, Lo, Hi);
3768 break;
3769 }
3770 }
3771
Chris Lattnere34b3962005-01-19 04:19:40 +00003772 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003773 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003774 break;
Chris Lattner47599822005-04-02 03:38:53 +00003775
3776 // If this target supports SRL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003777 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003778 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3779 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003780 break;
3781 }
3782
Chris Lattnere34b3962005-01-19 04:19:40 +00003783 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003784 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003785 break;
3786
Misha Brukmanedf128a2005-04-21 22:36:52 +00003787 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003788 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3789 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003790 break;
3791 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003792 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3793 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003794 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003795 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003796 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003797 SDOperand LL, LH, RL, RH;
3798 ExpandOp(Node->getOperand(0), LL, LH);
3799 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003800 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3801 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3802 // extended the sign bit of the low half through the upper half, and if so
3803 // emit a MULHS instead of the alternate sequence that is valid for any
3804 // i64 x i64 multiply.
3805 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3806 // is RH an extension of the sign bit of RL?
3807 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3808 RH.getOperand(1).getOpcode() == ISD::Constant &&
3809 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3810 // is LH an extension of the sign bit of LL?
3811 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3812 LH.getOperand(1).getOpcode() == ISD::Constant &&
3813 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3814 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3815 } else {
3816 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3817 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3818 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3819 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3820 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3821 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003822 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3823 } else {
3824 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3825 }
3826 break;
3827 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003828 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3829 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3830 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3831 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003832 }
3833
3834 // Remember in a map if the values will be reused later.
Chris Lattner6fdcb252005-09-02 20:32:45 +00003835 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3836 std::make_pair(Lo, Hi))).second;
3837 assert(isNew && "Value already expanded?!?");
Chris Lattner69a889e2005-12-20 00:53:54 +00003838
Chris Lattner83397362005-12-21 18:02:52 +00003839 // Make sure the resultant values have been legalized themselves, unless this
3840 // is a type that requires multi-step expansion.
3841 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
3842 Lo = LegalizeOp(Lo);
3843 Hi = LegalizeOp(Hi);
3844 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003845}
3846
3847
3848// SelectionDAG::Legalize - This is the entry point for the file.
3849//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003850void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003851 /// run - This is the main entry point to this class.
3852 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003853 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003854}
3855