blob: a1bd7e832f862b5b145f5c200d24d48b7ffb6014 [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);
Chris Lattner18c2f132005-01-13 20:50:02 +0000592 case ISD::ImplicitDef:
593 Tmp1 = LegalizeOp(Node->getOperand(0));
594 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000595 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
596 Tmp1, Node->getOperand(1));
Chris Lattner18c2f132005-01-13 20:50:02 +0000597 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000598 case ISD::UNDEF: {
599 MVT::ValueType VT = Op.getValueType();
600 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000601 default: assert(0 && "This action is not supported yet!");
602 case TargetLowering::Expand:
603 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000604 if (MVT::isInteger(VT))
605 Result = DAG.getConstant(0, VT);
606 else if (MVT::isFloatingPoint(VT))
607 Result = DAG.getConstantFP(0, VT);
608 else
609 assert(0 && "Unknown value type!");
610 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000611 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000612 break;
613 }
614 break;
615 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000616
617 case ISD::LOCATION:
618 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
619 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
620
621 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
622 case TargetLowering::Promote:
623 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000624 case TargetLowering::Expand: {
Chris Lattnere7736732005-12-18 23:54:29 +0000625 MachineDebugInfo &DebugInfo = DAG.getMachineFunction().getDebugInfo();
626 std::vector<SDOperand> Ops;
627 Ops.push_back(Tmp1); // chain
628 Ops.push_back(Node->getOperand(1)); // line #
629 Ops.push_back(Node->getOperand(2)); // col #
630 const std::string &fname =
631 cast<StringSDNode>(Node->getOperand(3))->getValue();
632 const std::string &dirname =
633 cast<StringSDNode>(Node->getOperand(4))->getValue();
634 unsigned id = DebugInfo.RecordSource(fname, dirname);
635 Ops.push_back(DAG.getConstant(id, MVT::i32)); // source file id
636 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
637 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner36ce6912005-11-29 06:21:05 +0000638 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000639 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000640 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000641 if (Tmp1 != Node->getOperand(0) ||
642 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000643 std::vector<SDOperand> Ops;
644 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000645 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
646 Ops.push_back(Node->getOperand(1)); // line # must be legal.
647 Ops.push_back(Node->getOperand(2)); // col # must be legal.
648 } else {
649 // Otherwise promote them.
650 Ops.push_back(PromoteOp(Node->getOperand(1)));
651 Ops.push_back(PromoteOp(Node->getOperand(2)));
652 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000653 Ops.push_back(Node->getOperand(3)); // filename must be legal.
654 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
655 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
656 }
657 break;
658 }
659 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000660
661 case ISD::DEBUG_LOC:
662 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
663 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
664 case TargetLowering::Promote:
665 case TargetLowering::Expand:
666 default: assert(0 && "This action is not supported yet!");
667 case TargetLowering::Legal:
668 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
669 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
670 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
671 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
672
673 if (Tmp1 != Node->getOperand(0) ||
674 Tmp2 != Node->getOperand(1) ||
675 Tmp3 != Node->getOperand(2) ||
676 Tmp4 != Node->getOperand(3)) {
677 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
678 }
679 break;
680 }
681 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000682
Chris Lattner3e928bb2005-01-07 07:47:09 +0000683 case ISD::Constant:
684 // We know we don't need to expand constants here, constants only have one
685 // value and we check that it is fine above.
686
687 // FIXME: Maybe we should handle things like targets that don't support full
688 // 32-bit immediates?
689 break;
690 case ISD::ConstantFP: {
691 // Spill FP immediates to the constant pool if the target cannot directly
692 // codegen them. Targets often have some immediate values that can be
693 // efficiently generated into an FP register without a load. We explicitly
694 // leave these constants as ConstantFP nodes for the target to deal with.
695
696 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
697
698 // Check to see if this FP immediate is already legal.
699 bool isLegal = false;
700 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
701 E = TLI.legal_fpimm_end(); I != E; ++I)
702 if (CFP->isExactlyValue(*I)) {
703 isLegal = true;
704 break;
705 }
706
707 if (!isLegal) {
708 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000709 bool Extend = false;
710
711 // If a FP immediate is precise when represented as a float, we put it
712 // into the constant pool as a float, even if it's is statically typed
713 // as a double.
714 MVT::ValueType VT = CFP->getValueType(0);
715 bool isDouble = VT == MVT::f64;
716 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
717 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000718 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
719 // Only do this if the target has a native EXTLOAD instruction from
720 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000721 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000722 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
723 VT = MVT::f32;
724 Extend = true;
725 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000726
Nate Begeman28a6b022005-12-10 02:36:00 +0000727 SDOperand CPIdx =
728 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000729 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000730 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
731 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000732 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000733 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
734 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000735 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000736 }
737 break;
738 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000739 case ISD::ConstantVec: {
740 // We assume that vector constants are not legal, and will be immediately
741 // spilled to the constant pool.
742 //
743 // FIXME: revisit this when we have some kind of mechanism by which targets
744 // can decided legality of vector constants, of which there may be very
745 // many.
746 //
747 // Create a ConstantPacked, and put it in the constant pool.
748 std::vector<Constant*> CV;
749 MVT::ValueType VT = Node->getValueType(0);
750 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
751 SDOperand OpN = Node->getOperand(I);
752 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
753 if (MVT::isFloatingPoint(VT))
754 CV.push_back(ConstantFP::get(OpNTy,
755 cast<ConstantFPSDNode>(OpN)->getValue()));
756 else
757 CV.push_back(ConstantUInt::get(OpNTy,
758 cast<ConstantSDNode>(OpN)->getValue()));
759 }
760 Constant *CP = ConstantPacked::get(CV);
Nate Begemand7d746f2005-12-13 03:03:23 +0000761 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000762 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
763 break;
764 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000765 case ISD::TokenFactor:
766 if (Node->getNumOperands() == 2) {
767 bool Changed = false;
768 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
769 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
770 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
771 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
772 } else {
773 std::vector<SDOperand> Ops;
774 bool Changed = false;
775 // Legalize the operands.
776 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
777 SDOperand Op = Node->getOperand(i);
778 Ops.push_back(LegalizeOp(Op));
779 Changed |= Ops[i] != Op;
780 }
781 if (Changed)
782 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000783 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000784 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000785
Chris Lattner16cd04d2005-05-12 23:24:06 +0000786 case ISD::CALLSEQ_START:
787 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000789 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000790 Tmp2 = Node->getOperand(0);
Nate Begeman1aa19722005-10-04 02:10:55 +0000791 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000792 Node->setAdjCallChain(Tmp1);
Nate Begeman27d404c2005-10-04 00:37:37 +0000793
Chris Lattner16cd04d2005-05-12 23:24:06 +0000794 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000795 // nodes are treated specially and are mutated in place. This makes the dag
796 // legalization process more efficient and also makes libcall insertion
797 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000798 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000799 case ISD::DYNAMIC_STACKALLOC:
800 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
801 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
802 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
803 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000804 Tmp3 != Node->getOperand(2)) {
805 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
806 std::vector<SDOperand> Ops;
807 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
808 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
809 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000810 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000811
812 // Since this op produces two values, make sure to remember that we
813 // legalized both of them.
814 AddLegalizedOperand(SDOperand(Node, 0), Result);
815 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
816 return Result.getValue(Op.ResNo);
817
Chris Lattnerd71c0412005-05-13 18:43:43 +0000818 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000819 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000820 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
821 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000822
823 bool Changed = false;
824 std::vector<SDOperand> Ops;
825 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
826 Ops.push_back(LegalizeOp(Node->getOperand(i)));
827 Changed |= Ops.back() != Node->getOperand(i);
828 }
829
830 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000831 std::vector<MVT::ValueType> RetTyVTs;
832 RetTyVTs.reserve(Node->getNumValues());
833 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000834 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000835 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
836 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000837 } else {
838 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000839 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000840 // Since calls produce multiple values, make sure to remember that we
841 // legalized all of them.
842 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
843 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
844 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000845 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000846 case ISD::BR:
847 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
848 if (Tmp1 != Node->getOperand(0))
849 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
850 break;
851
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000852 case ISD::BRCOND:
853 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000854
Chris Lattner47e92232005-01-18 19:27:06 +0000855 switch (getTypeAction(Node->getOperand(1).getValueType())) {
856 case Expand: assert(0 && "It's impossible to expand bools");
857 case Legal:
858 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
859 break;
860 case Promote:
861 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
862 break;
863 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000864
865 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
866 default: assert(0 && "This action is not supported yet!");
867 case TargetLowering::Expand:
868 // Expand brcond's setcc into its constituent parts and create a BR_CC
869 // Node.
870 if (Tmp2.getOpcode() == ISD::SETCC) {
871 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
872 Tmp2.getOperand(0), Tmp2.getOperand(1),
873 Node->getOperand(2));
874 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000875 // Make sure the condition is either zero or one. It may have been
876 // promoted from something else.
877 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
878
Nate Begeman7cbd5252005-08-16 19:49:35 +0000879 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
880 DAG.getCondCode(ISD::SETNE), Tmp2,
881 DAG.getConstant(0, Tmp2.getValueType()),
882 Node->getOperand(2));
883 }
Chris Lattnere7736732005-12-18 23:54:29 +0000884 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000885 break;
Evan Cheng898101c2005-12-19 23:12:38 +0000886 case TargetLowering::Custom: {
887 SDOperand Tmp =
888 TLI.LowerOperation(DAG.getNode(ISD::BRCOND, Node->getValueType(0),
889 Tmp1, Tmp2, Node->getOperand(2)), DAG);
890 if (Tmp.Val) {
891 Result = LegalizeOp(Tmp);
892 break;
893 }
894 // FALLTHROUGH if the target thinks it is legal.
895 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000896 case TargetLowering::Legal:
897 // Basic block destination (Op#2) is always legal.
898 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
899 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
900 Node->getOperand(2));
901 break;
902 }
903 break;
904 case ISD::BR_CC:
905 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner181b7a32005-12-17 23:46:46 +0000906 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000907 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
908 Node->getOperand(2), // LHS
909 Node->getOperand(3), // RHS
910 Node->getOperand(1)));
911 // If we get a SETCC back from legalizing the SETCC node we just
912 // created, then use its LHS, RHS, and CC directly in creating a new
913 // node. Otherwise, select between the true and false value based on
914 // comparing the result of the legalized with zero.
915 if (Tmp2.getOpcode() == ISD::SETCC) {
916 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
917 Tmp2.getOperand(0), Tmp2.getOperand(1),
918 Node->getOperand(4));
919 } else {
920 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
921 DAG.getCondCode(ISD::SETNE),
922 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
923 Node->getOperand(4));
924 }
Chris Lattner181b7a32005-12-17 23:46:46 +0000925 break;
926 }
927
928 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
929 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
930
931 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
932 default: assert(0 && "Unexpected action for BR_CC!");
933 case TargetLowering::Custom: {
934 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
935 Tmp2, Tmp3, Node->getOperand(4));
936 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
937 if (Tmp4.Val) {
938 Result = LegalizeOp(Tmp4);
939 break;
940 }
941 } // FALLTHROUGH if the target doesn't want to lower this op after all.
942 case TargetLowering::Legal:
943 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
944 Tmp3 != Node->getOperand(3)) {
945 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
946 Tmp2, Tmp3, Node->getOperand(4));
947 }
948 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000949 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000950 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000951 case ISD::BRCONDTWOWAY:
952 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
953 switch (getTypeAction(Node->getOperand(1).getValueType())) {
954 case Expand: assert(0 && "It's impossible to expand bools");
955 case Legal:
956 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
957 break;
958 case Promote:
959 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
960 break;
961 }
962 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
963 // pair.
964 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
965 case TargetLowering::Promote:
966 default: assert(0 && "This action is not supported yet!");
967 case TargetLowering::Legal:
968 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
969 std::vector<SDOperand> Ops;
970 Ops.push_back(Tmp1);
971 Ops.push_back(Tmp2);
972 Ops.push_back(Node->getOperand(2));
973 Ops.push_back(Node->getOperand(3));
974 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
975 }
976 break;
977 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000978 // If BRTWOWAY_CC is legal for this target, then simply expand this node
979 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
980 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000981 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000982 if (Tmp2.getOpcode() == ISD::SETCC) {
983 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
984 Tmp2.getOperand(0), Tmp2.getOperand(1),
985 Node->getOperand(2), Node->getOperand(3));
986 } else {
987 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
988 DAG.getConstant(0, Tmp2.getValueType()),
989 Node->getOperand(2), Node->getOperand(3));
990 }
991 } else {
992 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000993 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000994 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
995 }
Chris Lattnere7736732005-12-18 23:54:29 +0000996 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner411e8882005-04-09 03:30:19 +0000997 break;
998 }
999 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001000 case ISD::BRTWOWAY_CC:
1001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001002 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001003 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1004 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1005 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1006 Tmp3 != Node->getOperand(3)) {
1007 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1008 Node->getOperand(4), Node->getOperand(5));
1009 }
1010 break;
1011 } else {
1012 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1013 Node->getOperand(2), // LHS
1014 Node->getOperand(3), // RHS
1015 Node->getOperand(1)));
1016 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1017 // pair.
1018 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1019 default: assert(0 && "This action is not supported yet!");
1020 case TargetLowering::Legal:
1021 // If we get a SETCC back from legalizing the SETCC node we just
1022 // created, then use its LHS, RHS, and CC directly in creating a new
1023 // node. Otherwise, select between the true and false value based on
1024 // comparing the result of the legalized with zero.
1025 if (Tmp2.getOpcode() == ISD::SETCC) {
1026 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1027 Tmp2.getOperand(0), Tmp2.getOperand(1),
1028 Node->getOperand(4), Node->getOperand(5));
1029 } else {
1030 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1031 DAG.getConstant(0, Tmp2.getValueType()),
1032 Node->getOperand(4), Node->getOperand(5));
1033 }
1034 break;
1035 case TargetLowering::Expand:
1036 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1037 Node->getOperand(4));
1038 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
Chris Lattnere7736732005-12-18 23:54:29 +00001039 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +00001040 break;
1041 }
1042 }
1043 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001044 case ISD::LOAD:
1045 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1046 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001047
Chris Lattner3e928bb2005-01-07 07:47:09 +00001048 if (Tmp1 != Node->getOperand(0) ||
1049 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001050 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1051 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001052 else
1053 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001054
Chris Lattner8afc48e2005-01-07 22:28:47 +00001055 // Since loads produce two values, make sure to remember that we legalized
1056 // both of them.
1057 AddLegalizedOperand(SDOperand(Node, 0), Result);
1058 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1059 return Result.getValue(Op.ResNo);
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001060
Chris Lattner0f69b292005-01-15 06:18:18 +00001061 case ISD::EXTLOAD:
1062 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001063 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001064 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1065 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001066
Chris Lattner5f056bf2005-07-10 01:55:33 +00001067 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001068 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001069 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001070 case TargetLowering::Promote:
1071 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001072 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1073 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001074 // Since loads produce two values, make sure to remember that we legalized
1075 // both of them.
1076 AddLegalizedOperand(SDOperand(Node, 0), Result);
1077 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1078 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001079
Chris Lattner01ff7212005-04-10 22:54:25 +00001080 case TargetLowering::Legal:
1081 if (Tmp1 != Node->getOperand(0) ||
1082 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +00001083 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1084 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +00001085 else
1086 Result = SDOperand(Node, 0);
1087
1088 // Since loads produce two values, make sure to remember that we legalized
1089 // both of them.
1090 AddLegalizedOperand(SDOperand(Node, 0), Result);
1091 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1092 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +00001093 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001094 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001095 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1096 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001097 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnere7736732005-12-18 23:54:29 +00001098 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001099 Load = LegalizeOp(Load);
1100 AddLegalizedOperand(SDOperand(Node, 0), Result);
1101 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001102 if (Op.ResNo)
1103 return Load.getValue(1);
1104 return Result;
1105 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001106 assert(Node->getOpcode() != ISD::EXTLOAD &&
1107 "EXTLOAD should always be supported!");
1108 // Turn the unsupported load into an EXTLOAD followed by an explicit
1109 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001110 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1111 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001112 SDOperand ValRes;
1113 if (Node->getOpcode() == ISD::SEXTLOAD)
1114 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001115 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001116 else
1117 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnere7736732005-12-18 23:54:29 +00001118 Result = LegalizeOp(Result); // Relegalize new nodes.
1119 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001120 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1121 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattner01ff7212005-04-10 22:54:25 +00001122 if (Op.ResNo)
1123 return Result.getValue(1);
1124 return ValRes;
1125 }
1126 assert(0 && "Unreachable");
1127 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001128 case ISD::EXTRACT_ELEMENT: {
1129 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1130 switch (getTypeAction(OpTy)) {
1131 default:
1132 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1133 break;
1134 case Legal:
1135 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1136 // 1 -> Hi
1137 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1138 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1139 TLI.getShiftAmountTy()));
1140 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1141 } else {
1142 // 0 -> Lo
1143 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1144 Node->getOperand(0));
1145 }
1146 Result = LegalizeOp(Result);
1147 break;
1148 case Expand:
1149 // Get both the low and high parts.
1150 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1151 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1152 Result = Tmp2; // 1 -> Hi
1153 else
1154 Result = Tmp1; // 0 -> Lo
1155 break;
1156 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001157 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001158 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001159
1160 case ISD::CopyToReg:
1161 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001162
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001163 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001164 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001165 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001166 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001167 if (Node->getNumValues() == 1) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001168 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1169 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1170 Node->getOperand(1), Tmp2);
1171 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001172 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1173 if (Node->getNumOperands() == 4)
1174 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattner7310fb12005-12-18 15:27:43 +00001175 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001176 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001177 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1178 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1179 }
1180
1181 // Since this produces two values, make sure to remember that we legalized
1182 // both of them.
1183 AddLegalizedOperand(SDOperand(Node, 0), Result);
1184 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1185 return Result.getValue(Op.ResNo);
1186 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001187 break;
1188
1189 case ISD::RET:
1190 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1191 switch (Node->getNumOperands()) {
1192 case 2: // ret val
1193 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1194 case Legal:
1195 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001196 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +00001197 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1198 break;
1199 case Expand: {
1200 SDOperand Lo, Hi;
1201 ExpandOp(Node->getOperand(1), Lo, Hi);
1202 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001203 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001204 }
1205 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001206 Tmp2 = PromoteOp(Node->getOperand(1));
1207 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1208 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001209 }
1210 break;
1211 case 1: // ret void
1212 if (Tmp1 != Node->getOperand(0))
1213 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1214 break;
1215 default: { // ret <values>
1216 std::vector<SDOperand> NewValues;
1217 NewValues.push_back(Tmp1);
1218 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1219 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1220 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001221 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001222 break;
1223 case Expand: {
1224 SDOperand Lo, Hi;
1225 ExpandOp(Node->getOperand(i), Lo, Hi);
1226 NewValues.push_back(Lo);
1227 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001228 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001229 }
1230 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001231 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001232 }
1233 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1234 break;
1235 }
1236 }
1237 break;
1238 case ISD::STORE:
1239 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1240 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1241
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001242 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001243 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001244 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001245 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001246 DAG.getConstant(FloatToBits(CFP->getValue()),
1247 MVT::i32),
1248 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001249 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001250 } else {
1251 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001252 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001253 DAG.getConstant(DoubleToBits(CFP->getValue()),
1254 MVT::i64),
1255 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001256 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001257 }
Chris Lattner84734ce2005-02-22 07:23:39 +00001258 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001259 }
1260
Chris Lattner3e928bb2005-01-07 07:47:09 +00001261 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1262 case Legal: {
1263 SDOperand Val = LegalizeOp(Node->getOperand(1));
1264 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1265 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001266 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1267 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001268 break;
1269 }
1270 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001271 // Truncate the value and store the result.
1272 Tmp3 = PromoteOp(Node->getOperand(1));
1273 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001274 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001275 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001276 break;
1277
Chris Lattner3e928bb2005-01-07 07:47:09 +00001278 case Expand:
1279 SDOperand Lo, Hi;
Nate Begemanab48be32005-11-22 18:16:00 +00001280 unsigned IncrementSize;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001281 ExpandOp(Node->getOperand(1), Lo, Hi);
1282
1283 if (!TLI.isLittleEndian())
1284 std::swap(Lo, Hi);
1285
Chris Lattneredb1add2005-05-11 04:51:16 +00001286 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1287 Node->getOperand(3));
Nate Begemanab48be32005-11-22 18:16:00 +00001288 // If this is a vector type, then we have to calculate the increment as
1289 // the product of the element size in bytes, and the number of elements
1290 // in the high half of the vector.
1291 if (MVT::Vector == Hi.getValueType()) {
1292 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1293 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1294 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1295 } else {
1296 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1297 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001298 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1299 getIntPtrConstant(IncrementSize));
1300 assert(isTypeLegal(Tmp2.getValueType()) &&
1301 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001302 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001303 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1304 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001305 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1306 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001307 }
1308 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001309 case ISD::PCMARKER:
1310 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001311 if (Tmp1 != Node->getOperand(0))
1312 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001313 break;
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001314 case ISD::READCYCLECOUNTER:
1315 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthcde0f5c2005-12-02 06:08:08 +00001316 if (Tmp1 != Node->getOperand(0)) {
1317 std::vector<MVT::ValueType> rtypes;
1318 std::vector<SDOperand> rvals;
1319 rtypes.push_back(MVT::i64);
1320 rtypes.push_back(MVT::Other);
1321 rvals.push_back(Tmp1);
1322 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1323 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001324
1325 // Since rdcc produce two values, make sure to remember that we legalized
1326 // both of them.
1327 AddLegalizedOperand(SDOperand(Node, 0), Result);
1328 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1329 return Result.getValue(Op.ResNo);
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001330
Chris Lattner0f69b292005-01-15 06:18:18 +00001331 case ISD::TRUNCSTORE:
1332 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1333 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1334
1335 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1336 case Legal:
1337 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001338
1339 // The only promote case we handle is TRUNCSTORE:i1 X into
1340 // -> TRUNCSTORE:i8 (and X, 1)
1341 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1342 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1343 TargetLowering::Promote) {
1344 // Promote the bool to a mask then store.
1345 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1346 DAG.getConstant(1, Tmp2.getValueType()));
1347 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1348 Node->getOperand(3), DAG.getValueType(MVT::i8));
1349
1350 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1351 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001352 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001353 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001354 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001355 break;
1356 case Promote:
1357 case Expand:
1358 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1359 }
1360 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001361 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001362 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1363 case Expand: assert(0 && "It's impossible to expand bools");
1364 case Legal:
1365 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1366 break;
1367 case Promote:
1368 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1369 break;
1370 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001371 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001372 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001373
Nate Begemanb942a3d2005-08-23 04:29:48 +00001374 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001375 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001376 case TargetLowering::Expand:
1377 if (Tmp1.getOpcode() == ISD::SETCC) {
1378 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1379 Tmp2, Tmp3,
1380 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1381 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001382 // Make sure the condition is either zero or one. It may have been
1383 // promoted from something else.
1384 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001385 Result = DAG.getSelectCC(Tmp1,
1386 DAG.getConstant(0, Tmp1.getValueType()),
1387 Tmp2, Tmp3, ISD::SETNE);
1388 }
Chris Lattnere7736732005-12-18 23:54:29 +00001389 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman9373a812005-08-10 20:51:12 +00001390 break;
Evan Cheng7df96d62005-12-17 01:21:05 +00001391 case TargetLowering::Custom: {
1392 SDOperand Tmp =
1393 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1394 Tmp1, Tmp2, Tmp3), DAG);
1395 if (Tmp.Val) {
1396 Result = LegalizeOp(Tmp);
1397 break;
1398 }
1399 // FALLTHROUGH if the target thinks it is legal.
1400 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001401 case TargetLowering::Legal:
1402 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1403 Tmp3 != Node->getOperand(2))
1404 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1405 Tmp1, Tmp2, Tmp3);
1406 break;
1407 case TargetLowering::Promote: {
1408 MVT::ValueType NVT =
1409 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1410 unsigned ExtOp, TruncOp;
1411 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001412 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001413 TruncOp = ISD::TRUNCATE;
1414 } else {
1415 ExtOp = ISD::FP_EXTEND;
1416 TruncOp = ISD::FP_ROUND;
1417 }
1418 // Promote each of the values to the new type.
1419 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1420 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1421 // Perform the larger operation, then round down.
1422 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1423 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1424 break;
1425 }
1426 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001427 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001428 case ISD::SELECT_CC:
1429 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1430 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1431
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001432 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001433 // Everything is legal, see if we should expand this op or something.
1434 switch (TLI.getOperationAction(ISD::SELECT_CC,
1435 Node->getOperand(0).getValueType())) {
1436 default: assert(0 && "This action is not supported yet!");
1437 case TargetLowering::Custom: {
1438 SDOperand Tmp =
1439 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1440 Node->getOperand(0),
1441 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001442 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001443 if (Tmp.Val) {
1444 Result = LegalizeOp(Tmp);
1445 break;
1446 }
1447 } // FALLTHROUGH if the target can't lower this operation after all.
1448 case TargetLowering::Legal:
1449 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1450 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1451 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1452 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1453 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1454 Tmp3, Tmp4, Node->getOperand(4));
1455 }
1456 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001457 }
1458 break;
1459 } else {
1460 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1461 Node->getOperand(0), // LHS
1462 Node->getOperand(1), // RHS
1463 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001464 // If we get a SETCC back from legalizing the SETCC node we just
1465 // created, then use its LHS, RHS, and CC directly in creating a new
1466 // node. Otherwise, select between the true and false value based on
1467 // comparing the result of the legalized with zero.
1468 if (Tmp1.getOpcode() == ISD::SETCC) {
1469 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1470 Tmp1.getOperand(0), Tmp1.getOperand(1),
1471 Tmp3, Tmp4, Tmp1.getOperand(2));
1472 } else {
1473 Result = DAG.getSelectCC(Tmp1,
1474 DAG.getConstant(0, Tmp1.getValueType()),
1475 Tmp3, Tmp4, ISD::SETNE);
1476 }
Nate Begeman9373a812005-08-10 20:51:12 +00001477 }
1478 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001479 case ISD::SETCC:
1480 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1481 case Legal:
1482 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1483 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001484 break;
1485 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001486 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1487 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1488
1489 // If this is an FP compare, the operands have already been extended.
1490 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1491 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001492 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001493
1494 // Otherwise, we have to insert explicit sign or zero extends. Note
1495 // that we could insert sign extends for ALL conditions, but zero extend
1496 // is cheaper on many machines (an AND instead of two shifts), so prefer
1497 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001498 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001499 default: assert(0 && "Unknown integer comparison!");
1500 case ISD::SETEQ:
1501 case ISD::SETNE:
1502 case ISD::SETUGE:
1503 case ISD::SETUGT:
1504 case ISD::SETULE:
1505 case ISD::SETULT:
1506 // ALL of these operations will work if we either sign or zero extend
1507 // the operands (including the unsigned comparisons!). Zero extend is
1508 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001509 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1510 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001511 break;
1512 case ISD::SETGE:
1513 case ISD::SETGT:
1514 case ISD::SETLT:
1515 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001516 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1517 DAG.getValueType(VT));
1518 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1519 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001520 break;
1521 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001522 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001523 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001524 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001525 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1526 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1527 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001528 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001529 case ISD::SETEQ:
1530 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001531 if (RHSLo == RHSHi)
1532 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1533 if (RHSCST->isAllOnesValue()) {
1534 // Comparison to -1.
1535 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001536 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001537 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001538 }
1539
Chris Lattner3e928bb2005-01-07 07:47:09 +00001540 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1541 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1542 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001543 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001544 break;
1545 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001546 // If this is a comparison of the sign bit, just look at the top part.
1547 // X > -1, x < 0
1548 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001549 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001550 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001551 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001552 (CST->isAllOnesValue()))) { // X > -1
1553 Tmp1 = LHSHi;
1554 Tmp2 = RHSHi;
1555 break;
1556 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001557
Chris Lattner3e928bb2005-01-07 07:47:09 +00001558 // FIXME: This generated code sucks.
1559 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001560 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001561 default: assert(0 && "Unknown integer setcc!");
1562 case ISD::SETLT:
1563 case ISD::SETULT: LowCC = ISD::SETULT; break;
1564 case ISD::SETGT:
1565 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1566 case ISD::SETLE:
1567 case ISD::SETULE: LowCC = ISD::SETULE; break;
1568 case ISD::SETGE:
1569 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1570 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001571
Chris Lattner3e928bb2005-01-07 07:47:09 +00001572 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1573 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1574 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1575
1576 // NOTE: on targets without efficient SELECT of bools, we can always use
1577 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001578 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1579 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1580 Node->getOperand(2));
1581 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001582 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1583 Result, Tmp1, Tmp2));
Chris Lattner69a889e2005-12-20 00:53:54 +00001584 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001585 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001586 }
1587 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001588
1589 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1590 default:
1591 assert(0 && "Cannot handle this action for SETCC yet!");
1592 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001593 case TargetLowering::Promote: {
1594 // First step, figure out the appropriate operation to use.
1595 // Allow SETCC to not be supported for all legal data types
1596 // Mostly this targets FP
1597 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1598 MVT::ValueType OldVT = NewInTy;
1599
1600 // Scan for the appropriate larger type to use.
1601 while (1) {
1602 NewInTy = (MVT::ValueType)(NewInTy+1);
1603
1604 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1605 "Fell off of the edge of the integer world");
1606 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1607 "Fell off of the edge of the floating point world");
1608
1609 // If the target supports SETCC of this type, use it.
1610 if (TLI.getOperationAction(ISD::SETCC, NewInTy) == TargetLowering::Legal)
1611 break;
1612 }
1613 if (MVT::isInteger(NewInTy))
1614 assert(0 && "Cannot promote Legal Integer SETCC yet");
1615 else {
1616 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1617 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1618 }
1619
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001620 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1621 Node->getOperand(2));
1622 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001623 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001624 case TargetLowering::Legal:
1625 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1626 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1627 Node->getOperand(2));
1628 break;
1629 case TargetLowering::Expand:
1630 // Expand a setcc node into a select_cc of the same condition, lhs, and
1631 // rhs that selects between const 1 (true) and const 0 (false).
1632 MVT::ValueType VT = Node->getValueType(0);
1633 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1634 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1635 Node->getOperand(2));
1636 Result = LegalizeOp(Result);
1637 break;
1638 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001639 break;
1640
Chris Lattnere1bd8222005-01-11 05:57:22 +00001641 case ISD::MEMSET:
1642 case ISD::MEMCPY:
1643 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001644 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001645 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1646
1647 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1648 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1649 case Expand: assert(0 && "Cannot expand a byte!");
1650 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001651 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001652 break;
1653 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001654 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001655 break;
1656 }
1657 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001658 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001659 }
Chris Lattner272455b2005-02-02 03:44:41 +00001660
1661 SDOperand Tmp4;
1662 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001663 case Expand: {
1664 // Length is too big, just take the lo-part of the length.
1665 SDOperand HiPart;
1666 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1667 break;
1668 }
Chris Lattnere5605212005-01-28 22:29:18 +00001669 case Legal:
1670 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001671 break;
1672 case Promote:
1673 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001674 break;
1675 }
1676
1677 SDOperand Tmp5;
1678 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1679 case Expand: assert(0 && "Cannot expand this yet!");
1680 case Legal:
1681 Tmp5 = LegalizeOp(Node->getOperand(4));
1682 break;
1683 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001684 Tmp5 = PromoteOp(Node->getOperand(4));
1685 break;
1686 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001687
1688 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1689 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001690 case TargetLowering::Custom: {
1691 SDOperand Tmp =
1692 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1693 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1694 if (Tmp.Val) {
1695 Result = LegalizeOp(Tmp);
1696 break;
1697 }
1698 // FALLTHROUGH if the target thinks it is legal.
1699 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001700 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001701 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1702 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1703 Tmp5 != Node->getOperand(4)) {
1704 std::vector<SDOperand> Ops;
1705 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1706 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1707 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1708 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001709 break;
1710 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001711 // Otherwise, the target does not support this operation. Lower the
1712 // operation to an explicit libcall as appropriate.
1713 MVT::ValueType IntPtr = TLI.getPointerTy();
1714 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1715 std::vector<std::pair<SDOperand, const Type*> > Args;
1716
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001717 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001718 if (Node->getOpcode() == ISD::MEMSET) {
1719 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1720 // Extend the ubyte argument to be an int value for the call.
1721 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1722 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1723 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1724
1725 FnName = "memset";
1726 } else if (Node->getOpcode() == ISD::MEMCPY ||
1727 Node->getOpcode() == ISD::MEMMOVE) {
1728 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1729 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1730 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1731 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1732 } else {
1733 assert(0 && "Unknown op!");
1734 }
Chris Lattner45982da2005-05-12 16:53:42 +00001735
Chris Lattnere1bd8222005-01-11 05:57:22 +00001736 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001737 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001738 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner69a889e2005-12-20 00:53:54 +00001739 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001740 break;
1741 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001742 }
1743 break;
1744 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001745
1746 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001747 Tmp1 = LegalizeOp(Node->getOperand(0));
1748 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001749
Chris Lattner3e011362005-05-14 07:45:46 +00001750 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1751 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1752 std::vector<SDOperand> Ops;
1753 Ops.push_back(Tmp1);
1754 Ops.push_back(Tmp2);
1755 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1756 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001757 Result = SDOperand(Node, 0);
1758 // Since these produce two values, make sure to remember that we legalized
1759 // both of them.
1760 AddLegalizedOperand(SDOperand(Node, 0), Result);
1761 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1762 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001763 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001764 Tmp1 = LegalizeOp(Node->getOperand(0));
1765 Tmp2 = LegalizeOp(Node->getOperand(1));
1766 Tmp3 = LegalizeOp(Node->getOperand(2));
1767 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1768 Tmp3 != Node->getOperand(2))
1769 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1770 break;
1771
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001772 case ISD::READIO:
1773 Tmp1 = LegalizeOp(Node->getOperand(0));
1774 Tmp2 = LegalizeOp(Node->getOperand(1));
1775
1776 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1777 case TargetLowering::Custom:
1778 default: assert(0 && "This action not implemented for this operation!");
1779 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001780 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1781 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1782 std::vector<SDOperand> Ops;
1783 Ops.push_back(Tmp1);
1784 Ops.push_back(Tmp2);
1785 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1786 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001787 Result = SDOperand(Node, 0);
1788 break;
1789 case TargetLowering::Expand:
1790 // Replace this with a load from memory.
1791 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1792 Node->getOperand(1), DAG.getSrcValue(NULL));
1793 Result = LegalizeOp(Result);
1794 break;
1795 }
1796
1797 // Since these produce two values, make sure to remember that we legalized
1798 // both of them.
1799 AddLegalizedOperand(SDOperand(Node, 0), Result);
1800 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1801 return Result.getValue(Op.ResNo);
1802
1803 case ISD::WRITEIO:
1804 Tmp1 = LegalizeOp(Node->getOperand(0));
1805 Tmp2 = LegalizeOp(Node->getOperand(1));
1806 Tmp3 = LegalizeOp(Node->getOperand(2));
1807
1808 switch (TLI.getOperationAction(Node->getOpcode(),
1809 Node->getOperand(1).getValueType())) {
1810 case TargetLowering::Custom:
1811 default: assert(0 && "This action not implemented for this operation!");
1812 case TargetLowering::Legal:
1813 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1814 Tmp3 != Node->getOperand(2))
1815 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1816 break;
1817 case TargetLowering::Expand:
1818 // Replace this with a store to memory.
1819 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1820 Node->getOperand(1), Node->getOperand(2),
1821 DAG.getSrcValue(NULL));
1822 Result = LegalizeOp(Result);
1823 break;
1824 }
1825 break;
1826
Chris Lattner84f67882005-01-20 18:52:28 +00001827 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001828 case ISD::SUB_PARTS:
1829 case ISD::SHL_PARTS:
1830 case ISD::SRA_PARTS:
1831 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001832 std::vector<SDOperand> Ops;
1833 bool Changed = false;
1834 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1835 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1836 Changed |= Ops.back() != Node->getOperand(i);
1837 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001838 if (Changed) {
1839 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1840 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1841 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001842
1843 // Since these produce multiple values, make sure to remember that we
1844 // legalized all of them.
1845 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1846 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1847 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001848 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001849
1850 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001851 case ISD::ADD:
1852 case ISD::SUB:
1853 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001854 case ISD::MULHS:
1855 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001856 case ISD::UDIV:
1857 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001858 case ISD::AND:
1859 case ISD::OR:
1860 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001861 case ISD::SHL:
1862 case ISD::SRL:
1863 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001864 case ISD::FADD:
1865 case ISD::FSUB:
1866 case ISD::FMUL:
1867 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001868 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001869 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1870 case Expand: assert(0 && "Not possible");
1871 case Legal:
1872 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1873 break;
1874 case Promote:
1875 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1876 break;
1877 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001878 if (Tmp1 != Node->getOperand(0) ||
1879 Tmp2 != Node->getOperand(1))
1880 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1881 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001882
Nate Begeman419f8b62005-10-18 00:27:41 +00001883 case ISD::BUILD_PAIR: {
1884 MVT::ValueType PairTy = Node->getValueType(0);
1885 // TODO: handle the case where the Lo and Hi operands are not of legal type
1886 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1887 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1888 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1889 case TargetLowering::Legal:
1890 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1891 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1892 break;
1893 case TargetLowering::Promote:
1894 case TargetLowering::Custom:
1895 assert(0 && "Cannot promote/custom this yet!");
1896 case TargetLowering::Expand:
1897 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1898 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1899 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1900 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1901 TLI.getShiftAmountTy()));
1902 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1903 break;
1904 }
1905 break;
1906 }
1907
Nate Begemanc105e192005-04-06 00:23:54 +00001908 case ISD::UREM:
1909 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001910 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001911 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1912 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1913 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1914 case TargetLowering::Legal:
1915 if (Tmp1 != Node->getOperand(0) ||
1916 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001917 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001918 Tmp2);
1919 break;
1920 case TargetLowering::Promote:
1921 case TargetLowering::Custom:
1922 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001923 case TargetLowering::Expand:
1924 if (MVT::isInteger(Node->getValueType(0))) {
1925 MVT::ValueType VT = Node->getValueType(0);
1926 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1927 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1928 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1929 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1930 } else {
1931 // Floating point mod -> fmod libcall.
1932 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1933 SDOperand Dummy;
1934 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001935 }
1936 break;
1937 }
1938 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001939
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001940 case ISD::CTPOP:
1941 case ISD::CTTZ:
1942 case ISD::CTLZ:
1943 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1944 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1945 case TargetLowering::Legal:
1946 if (Tmp1 != Node->getOperand(0))
1947 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1948 break;
1949 case TargetLowering::Promote: {
1950 MVT::ValueType OVT = Tmp1.getValueType();
1951 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001952
1953 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001954 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1955 // Perform the larger operation, then subtract if needed.
1956 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1957 switch(Node->getOpcode())
1958 {
1959 case ISD::CTPOP:
1960 Result = Tmp1;
1961 break;
1962 case ISD::CTTZ:
1963 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001964 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1965 DAG.getConstant(getSizeInBits(NVT), NVT),
1966 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001967 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001968 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1969 break;
1970 case ISD::CTLZ:
1971 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001972 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1973 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001974 getSizeInBits(OVT), NVT));
1975 break;
1976 }
1977 break;
1978 }
1979 case TargetLowering::Custom:
1980 assert(0 && "Cannot custom handle this yet!");
1981 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001982 switch(Node->getOpcode())
1983 {
1984 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001985 static const uint64_t mask[6] = {
1986 0x5555555555555555ULL, 0x3333333333333333ULL,
1987 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1988 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1989 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001990 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001991 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1992 unsigned len = getSizeInBits(VT);
1993 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001994 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001995 Tmp2 = DAG.getConstant(mask[i], VT);
1996 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001997 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001998 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1999 DAG.getNode(ISD::AND, VT,
2000 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
2001 Tmp2));
2002 }
2003 Result = Tmp1;
2004 break;
2005 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00002006 case ISD::CTLZ: {
2007 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002008 x = x | (x >> 1);
2009 x = x | (x >> 2);
2010 ...
2011 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00002012 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002013 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00002014
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002015 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2016 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00002017 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2018 unsigned len = getSizeInBits(VT);
2019 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2020 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002021 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00002022 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2023 }
2024 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002025 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00002026 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002027 }
2028 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00002029 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002030 // unless the target has ctlz but not ctpop, in which case we use:
2031 // { return 32 - nlz(~x & (x-1)); }
2032 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002033 MVT::ValueType VT = Tmp1.getValueType();
2034 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002035 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002036 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2037 DAG.getNode(ISD::SUB, VT, Tmp1,
2038 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002039 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002040 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2041 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00002042 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002043 DAG.getConstant(getSizeInBits(VT), VT),
2044 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2045 } else {
2046 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2047 }
Chris Lattner18aa6802005-05-11 05:27:09 +00002048 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002049 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002050 default:
2051 assert(0 && "Cannot expand this yet!");
2052 break;
2053 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002054 break;
2055 }
2056 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002057
Chris Lattner2c8086f2005-04-02 05:00:07 +00002058 // Unary operators
2059 case ISD::FABS:
2060 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002061 case ISD::FSQRT:
2062 case ISD::FSIN:
2063 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002064 Tmp1 = LegalizeOp(Node->getOperand(0));
2065 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2066 case TargetLowering::Legal:
2067 if (Tmp1 != Node->getOperand(0))
2068 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2069 break;
2070 case TargetLowering::Promote:
2071 case TargetLowering::Custom:
2072 assert(0 && "Cannot promote/custom handle this yet!");
2073 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002074 switch(Node->getOpcode()) {
2075 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00002076 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2077 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002078 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00002079 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002080 break;
2081 }
2082 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002083 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2084 MVT::ValueType VT = Node->getValueType(0);
2085 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002086 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002087 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2088 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2089 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002090 break;
2091 }
2092 case ISD::FSQRT:
2093 case ISD::FSIN:
2094 case ISD::FCOS: {
2095 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002096 const char *FnName = 0;
2097 switch(Node->getOpcode()) {
2098 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2099 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2100 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2101 default: assert(0 && "Unreachable!");
2102 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002103 SDOperand Dummy;
2104 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002105 break;
2106 }
2107 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002108 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00002109 }
2110 break;
2111 }
2112 break;
2113
2114 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002115 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002116 case ISD::UINT_TO_FP: {
2117 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002118 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2119 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002120 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002121 Node->getOperand(0).getValueType())) {
2122 default: assert(0 && "Unknown operation action!");
2123 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002124 Result = ExpandLegalINT_TO_FP(isSigned,
2125 LegalizeOp(Node->getOperand(0)),
2126 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002127 AddLegalizedOperand(Op, Result);
2128 return Result;
2129 case TargetLowering::Promote:
2130 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2131 Node->getValueType(0),
2132 isSigned);
2133 AddLegalizedOperand(Op, Result);
2134 return Result;
2135 case TargetLowering::Legal:
2136 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002137 case TargetLowering::Custom: {
2138 Tmp1 = LegalizeOp(Node->getOperand(0));
2139 SDOperand Tmp =
2140 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2141 Tmp = TLI.LowerOperation(Tmp, DAG);
2142 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002143 Tmp = LegalizeOp(Tmp); // Relegalize input.
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002144 AddLegalizedOperand(Op, Tmp);
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002145 return Tmp;
2146 } else {
2147 assert(0 && "Target Must Lower this");
2148 }
2149 }
Andrew Lenharthf4b32782005-06-27 23:28:32 +00002150 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002151
Chris Lattner3e928bb2005-01-07 07:47:09 +00002152 Tmp1 = LegalizeOp(Node->getOperand(0));
2153 if (Tmp1 != Node->getOperand(0))
2154 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2155 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002156 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002157 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2158 Node->getValueType(0), Node->getOperand(0));
2159 break;
2160 case Promote:
2161 if (isSigned) {
2162 Result = PromoteOp(Node->getOperand(0));
2163 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2164 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2165 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2166 } else {
2167 Result = PromoteOp(Node->getOperand(0));
2168 Result = DAG.getZeroExtendInReg(Result,
2169 Node->getOperand(0).getValueType());
2170 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00002171 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002172 break;
2173 }
2174 break;
2175 }
2176 case ISD::TRUNCATE:
2177 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2178 case Legal:
2179 Tmp1 = LegalizeOp(Node->getOperand(0));
2180 if (Tmp1 != Node->getOperand(0))
2181 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2182 break;
2183 case Expand:
2184 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2185
2186 // Since the result is legal, we should just be able to truncate the low
2187 // part of the source.
2188 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2189 break;
2190 case Promote:
2191 Result = PromoteOp(Node->getOperand(0));
2192 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2193 break;
2194 }
2195 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002196
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002197 case ISD::FP_TO_SINT:
2198 case ISD::FP_TO_UINT:
2199 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2200 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002201 Tmp1 = LegalizeOp(Node->getOperand(0));
2202
Chris Lattner1618beb2005-07-29 00:11:56 +00002203 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2204 default: assert(0 && "Unknown operation action!");
2205 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002206 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2207 SDOperand True, False;
2208 MVT::ValueType VT = Node->getOperand(0).getValueType();
2209 MVT::ValueType NVT = Node->getValueType(0);
2210 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2211 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2212 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2213 Node->getOperand(0), Tmp2, ISD::SETLT);
2214 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2215 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002216 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002217 Tmp2));
2218 False = DAG.getNode(ISD::XOR, NVT, False,
2219 DAG.getConstant(1ULL << ShiftAmt, NVT));
2220 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Chris Lattner69a889e2005-12-20 00:53:54 +00002221 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman2d56e722005-08-14 18:38:32 +00002222 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00002223 } else {
2224 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2225 }
2226 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002227 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002228 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00002229 Node->getOpcode() == ISD::FP_TO_SINT);
2230 AddLegalizedOperand(Op, Result);
2231 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00002232 case TargetLowering::Custom: {
2233 SDOperand Tmp =
2234 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2235 Tmp = TLI.LowerOperation(Tmp, DAG);
2236 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002237 Tmp = LegalizeOp(Tmp);
Chris Lattner07dffd62005-08-26 00:14:16 +00002238 AddLegalizedOperand(Op, Tmp);
Chris Lattner507f7522005-08-29 17:30:00 +00002239 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00002240 } else {
2241 // The target thinks this is legal afterall.
2242 break;
2243 }
2244 }
Chris Lattner1618beb2005-07-29 00:11:56 +00002245 case TargetLowering::Legal:
2246 break;
2247 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002248
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002249 if (Tmp1 != Node->getOperand(0))
2250 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2251 break;
2252 case Expand:
2253 assert(0 && "Shouldn't need to expand other operators here!");
2254 case Promote:
2255 Result = PromoteOp(Node->getOperand(0));
2256 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2257 break;
2258 }
2259 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002260
Chris Lattner13c78e22005-09-02 00:18:10 +00002261 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002262 case ISD::ZERO_EXTEND:
2263 case ISD::SIGN_EXTEND:
2264 case ISD::FP_EXTEND:
2265 case ISD::FP_ROUND:
2266 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2267 case Legal:
2268 Tmp1 = LegalizeOp(Node->getOperand(0));
2269 if (Tmp1 != Node->getOperand(0))
2270 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2271 break;
2272 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002273 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00002274
Chris Lattner03c85462005-01-15 05:21:40 +00002275 case Promote:
2276 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002277 case ISD::ANY_EXTEND:
2278 Result = PromoteOp(Node->getOperand(0));
2279 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2280 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002281 case ISD::ZERO_EXTEND:
2282 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002283 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002284 Result = DAG.getZeroExtendInReg(Result,
2285 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002286 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002287 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002288 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002289 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002290 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002291 Result,
2292 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002293 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002294 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002295 Result = PromoteOp(Node->getOperand(0));
2296 if (Result.getValueType() != Op.getValueType())
2297 // Dynamically dead while we have only 2 FP types.
2298 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2299 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002300 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002301 Result = PromoteOp(Node->getOperand(0));
2302 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2303 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002304 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002305 }
2306 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002307 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002308 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002309 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002310 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002311
2312 // If this operation is not supported, convert it to a shl/shr or load/store
2313 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002314 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2315 default: assert(0 && "This action not supported for this op yet!");
2316 case TargetLowering::Legal:
2317 if (Tmp1 != Node->getOperand(0))
2318 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00002319 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002320 break;
2321 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002322 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002323 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002324 // NOTE: we could fall back on load/store here too for targets without
2325 // SAR. However, it is doubtful that any exist.
2326 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2327 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002328 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002329 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2330 Node->getOperand(0), ShiftCst);
2331 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2332 Result, ShiftCst);
2333 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2334 // The only way we can lower this is to turn it into a STORETRUNC,
2335 // EXTLOAD pair, targetting a temporary location (a stack slot).
2336
2337 // NOTE: there is a choice here between constantly creating new stack
2338 // slots and always reusing the same one. We currently always create
2339 // new ones, as reuse may inhibit scheduling.
2340 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2341 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2342 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2343 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002344 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002345 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2346 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2347 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002348 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002349 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002350 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2351 Result, StackSlot, DAG.getSrcValue(NULL),
2352 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002353 } else {
2354 assert(0 && "Unknown op");
2355 }
2356 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002357 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002358 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002359 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002360 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002361 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002362
Chris Lattner45982da2005-05-12 16:53:42 +00002363 // Note that LegalizeOp may be reentered even from single-use nodes, which
2364 // means that we always must cache transformed nodes.
2365 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002366 return Result;
2367}
2368
Chris Lattner8b6fa222005-01-15 22:16:26 +00002369/// PromoteOp - Given an operation that produces a value in an invalid type,
2370/// promote it to compute the value into a larger type. The produced value will
2371/// have the correct bits for the low portion of the register, but no guarantee
2372/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002373SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2374 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002375 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002376 assert(getTypeAction(VT) == Promote &&
2377 "Caller should expand or legalize operands that are not promotable!");
2378 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2379 "Cannot promote to smaller type!");
2380
Chris Lattner03c85462005-01-15 05:21:40 +00002381 SDOperand Tmp1, Tmp2, Tmp3;
2382
2383 SDOperand Result;
2384 SDNode *Node = Op.Val;
2385
Chris Lattner6fdcb252005-09-02 20:32:45 +00002386 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2387 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002388
Chris Lattner0f69b292005-01-15 06:18:18 +00002389 // Promotion needs an optimization step to clean up after it, and is not
2390 // careful to avoid operations the target does not support. Make sure that
2391 // all generated operations are legalized in the next iteration.
2392 NeedsAnotherIteration = true;
2393
Chris Lattner03c85462005-01-15 05:21:40 +00002394 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002395 case ISD::CopyFromReg:
2396 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002397 default:
2398 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2399 assert(0 && "Do not know how to promote this operator!");
2400 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002401 case ISD::UNDEF:
2402 Result = DAG.getNode(ISD::UNDEF, NVT);
2403 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002404 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002405 if (VT != MVT::i1)
2406 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2407 else
2408 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002409 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2410 break;
2411 case ISD::ConstantFP:
2412 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2413 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2414 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002415
Chris Lattner82fbfb62005-01-18 02:59:52 +00002416 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002417 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002418 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2419 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002420 Result = LegalizeOp(Result);
2421 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002422
2423 case ISD::TRUNCATE:
2424 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2425 case Legal:
2426 Result = LegalizeOp(Node->getOperand(0));
2427 assert(Result.getValueType() >= NVT &&
2428 "This truncation doesn't make sense!");
2429 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2430 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2431 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002432 case Promote:
2433 // The truncation is not required, because we don't guarantee anything
2434 // about high bits anyway.
2435 Result = PromoteOp(Node->getOperand(0));
2436 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002437 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002438 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2439 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002440 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002441 }
2442 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002443 case ISD::SIGN_EXTEND:
2444 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002445 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002446 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2447 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2448 case Legal:
2449 // Input is legal? Just do extend all the way to the larger type.
2450 Result = LegalizeOp(Node->getOperand(0));
2451 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2452 break;
2453 case Promote:
2454 // Promote the reg if it's smaller.
2455 Result = PromoteOp(Node->getOperand(0));
2456 // The high bits are not guaranteed to be anything. Insert an extend.
2457 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002458 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002459 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002460 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002461 Result = DAG.getZeroExtendInReg(Result,
2462 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002463 break;
2464 }
2465 break;
2466
2467 case ISD::FP_EXTEND:
2468 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2469 case ISD::FP_ROUND:
2470 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2471 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2472 case Promote: assert(0 && "Unreachable with 2 FP types!");
2473 case Legal:
2474 // Input is legal? Do an FP_ROUND_INREG.
2475 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002476 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2477 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002478 break;
2479 }
2480 break;
2481
2482 case ISD::SINT_TO_FP:
2483 case ISD::UINT_TO_FP:
2484 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2485 case Legal:
2486 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002487 // No extra round required here.
2488 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002489 break;
2490
2491 case Promote:
2492 Result = PromoteOp(Node->getOperand(0));
2493 if (Node->getOpcode() == ISD::SINT_TO_FP)
2494 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002495 Result,
2496 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002497 else
Chris Lattner23993562005-04-13 02:38:47 +00002498 Result = DAG.getZeroExtendInReg(Result,
2499 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002500 // No extra round required here.
2501 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002502 break;
2503 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002504 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2505 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002506 // Round if we cannot tolerate excess precision.
2507 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002508 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2509 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002510 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002511 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002512 break;
2513
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002514 case ISD::SIGN_EXTEND_INREG:
2515 Result = PromoteOp(Node->getOperand(0));
2516 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2517 Node->getOperand(1));
2518 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002519 case ISD::FP_TO_SINT:
2520 case ISD::FP_TO_UINT:
2521 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2522 case Legal:
2523 Tmp1 = LegalizeOp(Node->getOperand(0));
2524 break;
2525 case Promote:
2526 // The input result is prerounded, so we don't have to do anything
2527 // special.
2528 Tmp1 = PromoteOp(Node->getOperand(0));
2529 break;
2530 case Expand:
2531 assert(0 && "not implemented");
2532 }
Nate Begemand2558e32005-08-14 01:20:53 +00002533 // If we're promoting a UINT to a larger size, check to see if the new node
2534 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2535 // we can use that instead. This allows us to generate better code for
2536 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2537 // legal, such as PowerPC.
2538 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002539 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002540 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2541 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002542 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2543 } else {
2544 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2545 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002546 break;
2547
Chris Lattner2c8086f2005-04-02 05:00:07 +00002548 case ISD::FABS:
2549 case ISD::FNEG:
2550 Tmp1 = PromoteOp(Node->getOperand(0));
2551 assert(Tmp1.getValueType() == NVT);
2552 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2553 // NOTE: we do not have to do any extra rounding here for
2554 // NoExcessFPPrecision, because we know the input will have the appropriate
2555 // precision, and these operations don't modify precision at all.
2556 break;
2557
Chris Lattnerda6ba872005-04-28 21:44:33 +00002558 case ISD::FSQRT:
2559 case ISD::FSIN:
2560 case ISD::FCOS:
2561 Tmp1 = PromoteOp(Node->getOperand(0));
2562 assert(Tmp1.getValueType() == NVT);
2563 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2564 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002565 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2566 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002567 break;
2568
Chris Lattner03c85462005-01-15 05:21:40 +00002569 case ISD::AND:
2570 case ISD::OR:
2571 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002572 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002573 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002574 case ISD::MUL:
2575 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002576 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002577 // that too is okay if they are integer operations.
2578 Tmp1 = PromoteOp(Node->getOperand(0));
2579 Tmp2 = PromoteOp(Node->getOperand(1));
2580 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2581 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002582 break;
2583 case ISD::FADD:
2584 case ISD::FSUB:
2585 case ISD::FMUL:
2586 // The input may have strange things in the top bits of the registers, but
2587 // these operations don't care.
2588 Tmp1 = PromoteOp(Node->getOperand(0));
2589 Tmp2 = PromoteOp(Node->getOperand(1));
2590 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2591 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2592
2593 // Floating point operations will give excess precision that we may not be
2594 // able to tolerate. If we DO allow excess precision, just leave it,
2595 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002596 // FIXME: Why would we need to round FP ops more than integer ones?
2597 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002598 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002599 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2600 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002601 break;
2602
Chris Lattner8b6fa222005-01-15 22:16:26 +00002603 case ISD::SDIV:
2604 case ISD::SREM:
2605 // These operators require that their input be sign extended.
2606 Tmp1 = PromoteOp(Node->getOperand(0));
2607 Tmp2 = PromoteOp(Node->getOperand(1));
2608 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002609 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2610 DAG.getValueType(VT));
2611 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2612 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002613 }
2614 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2615
2616 // Perform FP_ROUND: this is probably overly pessimistic.
2617 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002618 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2619 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002620 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002621 case ISD::FDIV:
2622 case ISD::FREM:
2623 // These operators require that their input be fp extended.
2624 Tmp1 = PromoteOp(Node->getOperand(0));
2625 Tmp2 = PromoteOp(Node->getOperand(1));
2626 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2627
2628 // Perform FP_ROUND: this is probably overly pessimistic.
2629 if (NoExcessFPPrecision)
2630 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2631 DAG.getValueType(VT));
2632 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002633
2634 case ISD::UDIV:
2635 case ISD::UREM:
2636 // These operators require that their input be zero extended.
2637 Tmp1 = PromoteOp(Node->getOperand(0));
2638 Tmp2 = PromoteOp(Node->getOperand(1));
2639 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002640 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2641 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002642 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2643 break;
2644
2645 case ISD::SHL:
2646 Tmp1 = PromoteOp(Node->getOperand(0));
2647 Tmp2 = LegalizeOp(Node->getOperand(1));
2648 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2649 break;
2650 case ISD::SRA:
2651 // The input value must be properly sign extended.
2652 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002653 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2654 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002655 Tmp2 = LegalizeOp(Node->getOperand(1));
2656 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2657 break;
2658 case ISD::SRL:
2659 // The input value must be properly zero extended.
2660 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002661 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002662 Tmp2 = LegalizeOp(Node->getOperand(1));
2663 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2664 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002665 case ISD::LOAD:
2666 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2667 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begemanded49632005-10-13 03:11:28 +00002668 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2669 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002670 // Remember that we legalized the chain.
2671 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2672 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002673 case ISD::SEXTLOAD:
2674 case ISD::ZEXTLOAD:
2675 case ISD::EXTLOAD:
2676 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2677 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner8136cda2005-10-15 20:24:07 +00002678 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2679 Node->getOperand(2),
2680 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002681 // Remember that we legalized the chain.
2682 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2683 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002684 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002685 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2686 case Expand: assert(0 && "It's impossible to expand bools");
2687 case Legal:
2688 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2689 break;
2690 case Promote:
2691 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2692 break;
2693 }
Chris Lattner03c85462005-01-15 05:21:40 +00002694 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2695 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2696 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2697 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002698 case ISD::SELECT_CC:
2699 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2700 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2701 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2702 Node->getOperand(1), Tmp2, Tmp3,
2703 Node->getOperand(4));
2704 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002705 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002706 case ISD::CALL: {
2707 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2708 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2709
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002710 std::vector<SDOperand> Ops;
2711 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2712 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2713
Chris Lattner8ac532c2005-01-16 19:46:48 +00002714 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2715 "Can only promote single result calls");
2716 std::vector<MVT::ValueType> RetTyVTs;
2717 RetTyVTs.reserve(2);
2718 RetTyVTs.push_back(NVT);
2719 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002720 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2721 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002722 Result = SDOperand(NC, 0);
2723
2724 // Insert the new chain mapping.
2725 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2726 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002727 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002728 case ISD::CTPOP:
2729 case ISD::CTTZ:
2730 case ISD::CTLZ:
2731 Tmp1 = Node->getOperand(0);
2732 //Zero extend the argument
2733 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2734 // Perform the larger operation, then subtract if needed.
2735 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2736 switch(Node->getOpcode())
2737 {
2738 case ISD::CTPOP:
2739 Result = Tmp1;
2740 break;
2741 case ISD::CTTZ:
2742 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002743 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002744 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002745 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002746 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2747 break;
2748 case ISD::CTLZ:
2749 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002750 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2751 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002752 getSizeInBits(VT), NVT));
2753 break;
2754 }
2755 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002756 }
2757
2758 assert(Result.Val && "Didn't set a result!");
2759 AddPromotedOperand(Op, Result);
2760 return Result;
2761}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002762
Chris Lattner84f67882005-01-20 18:52:28 +00002763/// ExpandAddSub - Find a clever way to expand this add operation into
2764/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002765void SelectionDAGLegalize::
2766ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2767 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002768 // Expand the subcomponents.
2769 SDOperand LHSL, LHSH, RHSL, RHSH;
2770 ExpandOp(LHS, LHSL, LHSH);
2771 ExpandOp(RHS, RHSL, RHSH);
2772
Chris Lattner84f67882005-01-20 18:52:28 +00002773 std::vector<SDOperand> Ops;
2774 Ops.push_back(LHSL);
2775 Ops.push_back(LHSH);
2776 Ops.push_back(RHSL);
2777 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002778 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2779 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002780 Hi = Lo.getValue(1);
2781}
2782
Chris Lattner5b359c62005-04-02 04:00:59 +00002783void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2784 SDOperand Op, SDOperand Amt,
2785 SDOperand &Lo, SDOperand &Hi) {
2786 // Expand the subcomponents.
2787 SDOperand LHSL, LHSH;
2788 ExpandOp(Op, LHSL, LHSH);
2789
2790 std::vector<SDOperand> Ops;
2791 Ops.push_back(LHSL);
2792 Ops.push_back(LHSH);
2793 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002794 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002795 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002796 Hi = Lo.getValue(1);
2797}
2798
2799
Chris Lattnere34b3962005-01-19 04:19:40 +00002800/// ExpandShift - Try to find a clever way to expand this shift operation out to
2801/// smaller elements. If we can't find a way that is more efficient than a
2802/// libcall on this target, return false. Otherwise, return true with the
2803/// low-parts expanded into Lo and Hi.
2804bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2805 SDOperand &Lo, SDOperand &Hi) {
2806 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2807 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002808
Chris Lattnere34b3962005-01-19 04:19:40 +00002809 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002810 SDOperand ShAmt = LegalizeOp(Amt);
2811 MVT::ValueType ShTy = ShAmt.getValueType();
2812 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2813 unsigned NVTBits = MVT::getSizeInBits(NVT);
2814
2815 // Handle the case when Amt is an immediate. Other cases are currently broken
2816 // and are disabled.
2817 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2818 unsigned Cst = CN->getValue();
2819 // Expand the incoming operand to be shifted, so that we have its parts
2820 SDOperand InL, InH;
2821 ExpandOp(Op, InL, InH);
2822 switch(Opc) {
2823 case ISD::SHL:
2824 if (Cst > VTBits) {
2825 Lo = DAG.getConstant(0, NVT);
2826 Hi = DAG.getConstant(0, NVT);
2827 } else if (Cst > NVTBits) {
2828 Lo = DAG.getConstant(0, NVT);
2829 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002830 } else if (Cst == NVTBits) {
2831 Lo = DAG.getConstant(0, NVT);
2832 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002833 } else {
2834 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2835 Hi = DAG.getNode(ISD::OR, NVT,
2836 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2837 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2838 }
2839 return true;
2840 case ISD::SRL:
2841 if (Cst > VTBits) {
2842 Lo = DAG.getConstant(0, NVT);
2843 Hi = DAG.getConstant(0, NVT);
2844 } else if (Cst > NVTBits) {
2845 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2846 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002847 } else if (Cst == NVTBits) {
2848 Lo = InH;
2849 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002850 } else {
2851 Lo = DAG.getNode(ISD::OR, NVT,
2852 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2853 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2854 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2855 }
2856 return true;
2857 case ISD::SRA:
2858 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002859 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002860 DAG.getConstant(NVTBits-1, ShTy));
2861 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002862 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002863 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002864 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002865 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002866 } else if (Cst == NVTBits) {
2867 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002868 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002869 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002870 } else {
2871 Lo = DAG.getNode(ISD::OR, NVT,
2872 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2873 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2874 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2875 }
2876 return true;
2877 }
2878 }
2879 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2880 // so disable it for now. Currently targets are handling this via SHL_PARTS
2881 // and friends.
2882 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002883
2884 // If we have an efficient select operation (or if the selects will all fold
2885 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002886 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00002887 return false;
2888
2889 SDOperand InL, InH;
2890 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002891 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2892 DAG.getConstant(NVTBits, ShTy), ShAmt);
2893
Chris Lattnere5544f82005-01-20 20:29:23 +00002894 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002895 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2896 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002897
Chris Lattnere34b3962005-01-19 04:19:40 +00002898 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2899 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2900 DAG.getConstant(NVTBits-1, ShTy));
2901 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2902 DAG.getConstant(NVTBits-1, ShTy));
2903 }
2904
2905 if (Opc == ISD::SHL) {
2906 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2907 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2908 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002909 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002910
Chris Lattnere34b3962005-01-19 04:19:40 +00002911 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2912 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2913 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002914 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002915 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2916 DAG.getConstant(32, ShTy),
2917 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002918 DAG.getConstant(0, NVT),
2919 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002920 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002921 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002922 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002923 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002924
2925 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002926 if (Opc == ISD::SRA)
2927 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2928 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002929 else
2930 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002931 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002932 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002933 }
2934 return true;
2935}
Chris Lattner77e77a62005-01-21 06:05:23 +00002936
Chris Lattner9530ddc2005-05-13 05:09:11 +00002937/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2938/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002939/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002940static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002941 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002942
Chris Lattner16cd04d2005-05-12 23:24:06 +00002943 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002944 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002945 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002946 Found = Node;
2947 return;
2948 }
2949
2950 // Otherwise, scan the operands of Node to see if any of them is a call.
2951 assert(Node->getNumOperands() != 0 &&
2952 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00002953 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002954 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002955
2956 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002957 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002958 Found);
2959}
2960
2961
Chris Lattner9530ddc2005-05-13 05:09:11 +00002962/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2963/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002964/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002965static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2966 std::set<SDNode*> &Visited) {
2967 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2968 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002969
Chris Lattner16cd04d2005-05-12 23:24:06 +00002970 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002971 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002972 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002973 Found = Node;
2974 return;
2975 }
2976
2977 // Otherwise, scan the operands of Node to see if any of them is a call.
2978 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2979 if (UI == E) return;
2980 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002981 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002982
2983 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002984 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002985}
2986
Chris Lattner9530ddc2005-05-13 05:09:11 +00002987/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002988/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002989static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002990 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002991 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002992 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002993 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002994
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002995 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002996 if (TheChain.getValueType() != MVT::Other)
2997 TheChain = SDOperand(Node, 0);
Nate Begeman1aa19722005-10-04 02:10:55 +00002998 if (TheChain.getValueType() != MVT::Other)
2999 return 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003000
3001 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00003002 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003003
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003004 // Make sure to only follow users of our token chain.
3005 SDNode *User = *UI;
3006 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3007 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00003008 if (SDNode *Result = FindCallSeqEnd(User))
3009 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003010 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00003011 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003012}
3013
Chris Lattner9530ddc2005-05-13 05:09:11 +00003014/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003015/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003016static SDNode *FindCallSeqStart(SDNode *Node) {
3017 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00003018 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003019
3020 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3021 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00003022 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003023}
3024
3025
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003026/// FindInputOutputChains - If we are replacing an operation with a call we need
3027/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003028/// properly serialize the calls in the block. The returned operand is the
3029/// input chain value for the new call (e.g. the entry node or the previous
3030/// call), and OutChain is set to be the chain node to update to point to the
3031/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003032static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3033 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003034 SDNode *LatestCallSeqStart = Entry.Val;
3035 SDNode *LatestCallSeqEnd = 0;
3036 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
3037 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003038
Chris Lattner16cd04d2005-05-12 23:24:06 +00003039 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00003040 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00003041 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3042 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00003043 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003044 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00003045 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3046 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003047 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00003048 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00003049 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00003050
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003051 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00003052 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003053 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00003054 std::set<SDNode*> Visited;
3055 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003056
Chris Lattner9530ddc2005-05-13 05:09:11 +00003057 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003058 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00003059 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003060
Chris Lattner9530ddc2005-05-13 05:09:11 +00003061 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003062}
3063
Jeff Cohen00b168892005-07-27 06:12:32 +00003064/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003065void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3066 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003067 // Nothing to splice it into?
3068 if (OutChain == 0) return;
3069
3070 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3071 //OutChain->dump();
3072
3073 // Form a token factor node merging the old inval and the new inval.
3074 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3075 OutChain->getOperand(0));
3076 // Change the node to refer to the new token.
3077 OutChain->setAdjCallChain(InToken);
3078}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003079
3080
Chris Lattner77e77a62005-01-21 06:05:23 +00003081// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3082// does not fit into a register, return the lo part and set the hi part to the
3083// by-reg argument. If it does fit into a single register, return the result
3084// and leave the Hi part unset.
3085SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3086 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003087 SDNode *OutChain;
3088 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3089 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00003090 if (InChain.Val == 0)
3091 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003092
Chris Lattner77e77a62005-01-21 06:05:23 +00003093 TargetLowering::ArgListTy Args;
3094 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3095 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3096 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3097 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3098 }
3099 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003100
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003101 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003102 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003103 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003104 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3105 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003106
Chris Lattner99c25b82005-09-02 20:26:58 +00003107 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003108 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003109 default: assert(0 && "Unknown thing");
3110 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00003111 Result = CallInfo.first;
3112 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003113 case Promote:
3114 assert(0 && "Cannot promote this yet!");
3115 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003116 ExpandOp(CallInfo.first, Result, Hi);
3117 CallInfo.second = LegalizeOp(CallInfo.second);
3118 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003119 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003120
3121 SpliceCallInto(CallInfo.second, OutChain);
3122 NeedsAnotherIteration = true;
3123 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003124}
3125
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003126
Chris Lattner77e77a62005-01-21 06:05:23 +00003127/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3128/// destination type is legal.
3129SDOperand SelectionDAGLegalize::
3130ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003131 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003132 assert(getTypeAction(Source.getValueType()) == Expand &&
3133 "This is not an expansion!");
3134 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3135
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003136 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003137 assert(Source.getValueType() == MVT::i64 &&
3138 "This only works for 64-bit -> FP");
3139 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3140 // incoming integer is set. To handle this, we dynamically test to see if
3141 // it is set, and, if so, add a fudge factor.
3142 SDOperand Lo, Hi;
3143 ExpandOp(Source, Lo, Hi);
3144
Chris Lattner66de05b2005-05-13 04:45:13 +00003145 // If this is unsigned, and not supported, first perform the conversion to
3146 // signed, then adjust the result if the sign bit is set.
3147 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3148 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3149
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003150 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3151 DAG.getConstant(0, Hi.getValueType()),
3152 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003153 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3154 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3155 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003156 uint64_t FF = 0x5f800000ULL;
3157 if (TLI.isLittleEndian()) FF <<= 32;
3158 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003159
Chris Lattner5839bf22005-08-26 17:15:30 +00003160 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003161 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3162 SDOperand FudgeInReg;
3163 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003164 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3165 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003166 else {
3167 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003168 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3169 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003170 }
Chris Lattner473a9902005-09-29 06:44:39 +00003171 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003172 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003173
Chris Lattnera88a2602005-05-14 05:33:54 +00003174 // Check to see if the target has a custom way to lower this. If so, use it.
3175 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3176 default: assert(0 && "This action not implemented for this operation!");
3177 case TargetLowering::Legal:
3178 case TargetLowering::Expand:
3179 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003180 case TargetLowering::Custom: {
3181 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3182 Source), DAG);
3183 if (NV.Val)
3184 return LegalizeOp(NV);
3185 break; // The target decided this was legal after all
3186 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003187 }
3188
Chris Lattner13689e22005-05-12 07:00:44 +00003189 // Expand the source, then glue it back together for the call. We must expand
3190 // the source in case it is shared (this pass of legalize must traverse it).
3191 SDOperand SrcLo, SrcHi;
3192 ExpandOp(Source, SrcLo, SrcHi);
3193 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3194
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003195 SDNode *OutChain = 0;
3196 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3197 DAG.getEntryNode());
3198 const char *FnName = 0;
3199 if (DestTy == MVT::f32)
3200 FnName = "__floatdisf";
3201 else {
3202 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3203 FnName = "__floatdidf";
3204 }
3205
Chris Lattner77e77a62005-01-21 06:05:23 +00003206 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3207
3208 TargetLowering::ArgListTy Args;
3209 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00003210
Chris Lattner77e77a62005-01-21 06:05:23 +00003211 Args.push_back(std::make_pair(Source, ArgTy));
3212
3213 // We don't care about token chains for libcalls. We just use the entry
3214 // node as our input and ignore the output chain. This allows us to place
3215 // calls wherever we need them to satisfy data dependences.
3216 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003217
3218 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00003219 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3220 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003221
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003222 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003223 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003224}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003225
Chris Lattnere34b3962005-01-19 04:19:40 +00003226
3227
Chris Lattner3e928bb2005-01-07 07:47:09 +00003228/// ExpandOp - Expand the specified SDOperand into its two component pieces
3229/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3230/// LegalizeNodes map is filled in for any results that are not expanded, the
3231/// ExpandedNodes map is filled in for any results that are expanded, and the
3232/// Lo/Hi values are returned.
3233void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3234 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003235 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003236 SDNode *Node = Op.Val;
3237 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003238 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3239 "Cannot expand FP values!");
3240 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003241 "Cannot expand to FP value or to larger int value!");
3242
Chris Lattner6fdcb252005-09-02 20:32:45 +00003243 // See if we already expanded it.
3244 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3245 = ExpandedNodes.find(Op);
3246 if (I != ExpandedNodes.end()) {
3247 Lo = I->second.first;
3248 Hi = I->second.second;
3249 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003250 }
3251
Chris Lattner4e6c7462005-01-08 19:27:05 +00003252 // Expanding to multiple registers needs to perform an optimization step, and
3253 // is not careful to avoid operations the target does not support. Make sure
3254 // that all generated operations are legalized in the next iteration.
3255 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003256
Chris Lattner3e928bb2005-01-07 07:47:09 +00003257 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003258 case ISD::CopyFromReg:
3259 assert(0 && "CopyFromReg must be legal!");
3260 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003261 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3262 assert(0 && "Do not know how to expand this operator!");
3263 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003264 case ISD::UNDEF:
3265 Lo = DAG.getNode(ISD::UNDEF, NVT);
3266 Hi = DAG.getNode(ISD::UNDEF, NVT);
3267 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003268 case ISD::Constant: {
3269 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3270 Lo = DAG.getConstant(Cst, NVT);
3271 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3272 break;
3273 }
Nate Begemancc827e62005-12-07 19:48:11 +00003274 case ISD::ConstantVec: {
3275 unsigned NumElements = Node->getNumOperands();
3276 // If we only have two elements left in the constant vector, just break it
3277 // apart into the two scalar constants it contains. Otherwise, bisect the
3278 // ConstantVec, and return each half as a new ConstantVec.
3279 // FIXME: this is hard coded as big endian, it may have to change to support
3280 // SSE and Alpha MVI
3281 if (NumElements == 2) {
3282 Hi = Node->getOperand(0);
3283 Lo = Node->getOperand(1);
3284 } else {
3285 NumElements /= 2;
3286 std::vector<SDOperand> LoOps, HiOps;
3287 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3288 HiOps.push_back(Node->getOperand(I));
3289 LoOps.push_back(Node->getOperand(I+NumElements));
3290 }
3291 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3292 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3293 }
3294 break;
3295 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003296
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003297 case ISD::BUILD_PAIR:
3298 // Legalize both operands. FIXME: in the future we should handle the case
3299 // where the two elements are not legal.
3300 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3301 Lo = LegalizeOp(Node->getOperand(0));
3302 Hi = LegalizeOp(Node->getOperand(1));
3303 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003304
3305 case ISD::SIGN_EXTEND_INREG:
3306 ExpandOp(Node->getOperand(0), Lo, Hi);
3307 // Sign extend the lo-part.
3308 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3309 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3310 TLI.getShiftAmountTy()));
3311 // sext_inreg the low part if needed.
3312 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3313 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003314
Chris Lattneredb1add2005-05-11 04:51:16 +00003315 case ISD::CTPOP:
3316 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003317 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3318 DAG.getNode(ISD::CTPOP, NVT, Lo),
3319 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003320 Hi = DAG.getConstant(0, NVT);
3321 break;
3322
Chris Lattner39a8f332005-05-12 19:05:01 +00003323 case ISD::CTLZ: {
3324 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003325 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003326 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3327 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003328 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3329 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003330 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3331 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3332
3333 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3334 Hi = DAG.getConstant(0, NVT);
3335 break;
3336 }
3337
3338 case ISD::CTTZ: {
3339 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003340 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003341 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3342 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003343 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3344 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003345 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3346 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3347
3348 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3349 Hi = DAG.getConstant(0, NVT);
3350 break;
3351 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003352
Chris Lattner3e928bb2005-01-07 07:47:09 +00003353 case ISD::LOAD: {
3354 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3355 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003356 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003357
3358 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003359 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003360 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3361 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00003362 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003363 //are from the same instruction?
3364 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003365
3366 // Build a factor node to remember that this load is independent of the
3367 // other one.
3368 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3369 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003370
Chris Lattner3e928bb2005-01-07 07:47:09 +00003371 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00003372 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003373 if (!TLI.isLittleEndian())
3374 std::swap(Lo, Hi);
3375 break;
3376 }
Nate Begemanab48be32005-11-22 18:16:00 +00003377 case ISD::VLOAD: {
3378 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3379 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3380 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3381 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3382
3383 // If we only have two elements, turn into a pair of scalar loads.
3384 // FIXME: handle case where a vector of two elements is fine, such as
3385 // 2 x double on SSE2.
3386 if (NumElements == 2) {
3387 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3388 // Increment the pointer to the other half.
3389 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3390 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3391 getIntPtrConstant(IncrementSize));
3392 //Is this safe? declaring that the two parts of the split load
3393 //are from the same instruction?
3394 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3395 } else {
3396 NumElements /= 2; // Split the vector in half
3397 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3398 unsigned IncrementSize = NumElements * 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.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3404 }
3405
3406 // Build a factor node to remember that this load is independent of the
3407 // other one.
3408 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3409 Hi.getValue(1));
3410
3411 // Remember that we legalized the chain.
3412 AddLegalizedOperand(Op.getValue(1), TF);
3413 if (!TLI.isLittleEndian())
3414 std::swap(Lo, Hi);
3415 break;
3416 }
3417 case ISD::VADD:
3418 case ISD::VSUB:
3419 case ISD::VMUL: {
3420 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3421 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3422 SDOperand LL, LH, RL, RH;
3423
3424 ExpandOp(Node->getOperand(0), LL, LH);
3425 ExpandOp(Node->getOperand(1), RL, RH);
3426
3427 // If we only have two elements, turn into a pair of scalar loads.
3428 // FIXME: handle case where a vector of two elements is fine, such as
3429 // 2 x double on SSE2.
3430 if (NumElements == 2) {
3431 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3432 Lo = DAG.getNode(Opc, EVT, LL, RL);
3433 Hi = DAG.getNode(Opc, EVT, LH, RH);
3434 } else {
3435 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3436 LL.getOperand(3));
3437 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3438 LH.getOperand(3));
3439 }
3440 break;
3441 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00003442 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003443 case ISD::CALL: {
3444 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3445 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3446
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003447 bool Changed = false;
3448 std::vector<SDOperand> Ops;
3449 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3450 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3451 Changed |= Ops.back() != Node->getOperand(i);
3452 }
3453
Chris Lattner3e928bb2005-01-07 07:47:09 +00003454 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3455 "Can only expand a call once so far, not i64 -> i16!");
3456
3457 std::vector<MVT::ValueType> RetTyVTs;
3458 RetTyVTs.reserve(3);
3459 RetTyVTs.push_back(NVT);
3460 RetTyVTs.push_back(NVT);
3461 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003462 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3463 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003464 Lo = SDOperand(NC, 0);
3465 Hi = SDOperand(NC, 1);
3466
3467 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00003468 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003469 break;
3470 }
3471 case ISD::AND:
3472 case ISD::OR:
3473 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3474 SDOperand LL, LH, RL, RH;
3475 ExpandOp(Node->getOperand(0), LL, LH);
3476 ExpandOp(Node->getOperand(1), RL, RH);
3477 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3478 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3479 break;
3480 }
3481 case ISD::SELECT: {
3482 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00003483
3484 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3485 case Expand: assert(0 && "It's impossible to expand bools");
3486 case Legal:
3487 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3488 break;
3489 case Promote:
3490 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3491 break;
3492 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003493 ExpandOp(Node->getOperand(1), LL, LH);
3494 ExpandOp(Node->getOperand(2), RL, RH);
3495 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3496 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3497 break;
3498 }
Nate Begeman9373a812005-08-10 20:51:12 +00003499 case ISD::SELECT_CC: {
3500 SDOperand TL, TH, FL, FH;
3501 ExpandOp(Node->getOperand(2), TL, TH);
3502 ExpandOp(Node->getOperand(3), FL, FH);
3503 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3504 Node->getOperand(1), TL, FL, Node->getOperand(4));
3505 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3506 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003507 Lo = LegalizeOp(Lo);
3508 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003509 break;
3510 }
Nate Begeman144ff662005-10-13 17:15:37 +00003511 case ISD::SEXTLOAD: {
3512 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3513 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3514 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3515
3516 if (EVT == NVT)
3517 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3518 else
3519 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3520 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003521
3522 // Remember that we legalized the chain.
3523 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3524
Nate Begeman144ff662005-10-13 17:15:37 +00003525 // The high part is obtained by SRA'ing all but one of the bits of the lo
3526 // part.
3527 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3528 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3529 TLI.getShiftAmountTy()));
3530 Lo = LegalizeOp(Lo);
3531 Hi = LegalizeOp(Hi);
3532 break;
3533 }
3534 case ISD::ZEXTLOAD: {
3535 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3536 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3537 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3538
3539 if (EVT == NVT)
3540 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3541 else
3542 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3543 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003544
3545 // Remember that we legalized the chain.
3546 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3547
Nate Begeman144ff662005-10-13 17:15:37 +00003548 // The high part is just a zero.
Chris Lattner9ad84812005-10-13 21:44:47 +00003549 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begeman144ff662005-10-13 17:15:37 +00003550 Lo = LegalizeOp(Lo);
Chris Lattner9ad84812005-10-13 21:44:47 +00003551 break;
3552 }
3553 case ISD::EXTLOAD: {
3554 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3555 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3556 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3557
3558 if (EVT == NVT)
3559 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3560 else
3561 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3562 EVT);
3563
3564 // Remember that we legalized the chain.
3565 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3566
3567 // The high part is undefined.
3568 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3569 Lo = LegalizeOp(Lo);
Nate Begeman144ff662005-10-13 17:15:37 +00003570 break;
3571 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003572 case ISD::ANY_EXTEND: {
3573 SDOperand In;
3574 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3575 case Expand: assert(0 && "expand-expand not implemented yet!");
3576 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3577 case Promote:
3578 In = PromoteOp(Node->getOperand(0));
3579 break;
3580 }
3581
3582 // The low part is any extension of the input (which degenerates to a copy).
3583 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3584 // The high part is undefined.
3585 Hi = DAG.getNode(ISD::UNDEF, NVT);
3586 break;
3587 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003588 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003589 SDOperand In;
3590 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3591 case Expand: assert(0 && "expand-expand not implemented yet!");
3592 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3593 case Promote:
3594 In = PromoteOp(Node->getOperand(0));
3595 // Emit the appropriate sign_extend_inreg to get the value we want.
3596 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00003597 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00003598 break;
3599 }
3600
Chris Lattner3e928bb2005-01-07 07:47:09 +00003601 // The low part is just a sign extension of the input (which degenerates to
3602 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00003603 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003604
Chris Lattner3e928bb2005-01-07 07:47:09 +00003605 // The high part is obtained by SRA'ing all but one of the bits of the lo
3606 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003607 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00003608 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3609 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003610 break;
3611 }
Chris Lattner06098e02005-04-03 23:41:52 +00003612 case ISD::ZERO_EXTEND: {
3613 SDOperand In;
3614 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3615 case Expand: assert(0 && "expand-expand not implemented yet!");
3616 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3617 case Promote:
3618 In = PromoteOp(Node->getOperand(0));
3619 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003620 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003621 break;
3622 }
3623
Chris Lattner3e928bb2005-01-07 07:47:09 +00003624 // The low part is just a zero extension of the input (which degenerates to
3625 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003626 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003627
Chris Lattner3e928bb2005-01-07 07:47:09 +00003628 // The high part is just a zero.
3629 Hi = DAG.getConstant(0, NVT);
3630 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003631 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003632
Chris Lattner308575b2005-11-20 22:56:56 +00003633 case ISD::READCYCLECOUNTER: {
3634 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3635 TargetLowering::Custom &&
3636 "Must custom expand ReadCycleCounter");
3637 SDOperand T = TLI.LowerOperation(Op, DAG);
3638 assert(T.Val && "Node must be custom expanded!");
3639 Lo = LegalizeOp(T.getValue(0));
3640 Hi = LegalizeOp(T.getValue(1));
3641 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3642 LegalizeOp(T.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003643 break;
Chris Lattner308575b2005-11-20 22:56:56 +00003644 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003645
Chris Lattner4e6c7462005-01-08 19:27:05 +00003646 // These operators cannot be expanded directly, emit them as calls to
3647 // library functions.
3648 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003649 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003650 SDOperand Op;
3651 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3652 case Expand: assert(0 && "cannot expand FP!");
3653 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3654 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3655 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003656
Chris Lattnerf20d1832005-07-30 01:40:57 +00003657 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3658
Chris Lattner80a3e942005-07-29 00:33:32 +00003659 // Now that the custom expander is done, expand the result, which is still
3660 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003661 if (Op.Val) {
3662 ExpandOp(Op, Lo, Hi);
3663 break;
3664 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003665 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003666
Chris Lattner4e6c7462005-01-08 19:27:05 +00003667 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003668 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003669 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003670 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003671 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003672
Chris Lattner4e6c7462005-01-08 19:27:05 +00003673 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003674 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3675 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3676 LegalizeOp(Node->getOperand(0)));
3677 // Now that the custom expander is done, expand the result, which is still
3678 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003679 Op = TLI.LowerOperation(Op, DAG);
3680 if (Op.Val) {
3681 ExpandOp(Op, Lo, Hi);
3682 break;
3683 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003684 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003685
Chris Lattner4e6c7462005-01-08 19:27:05 +00003686 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003687 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003688 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003689 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003690 break;
3691
Chris Lattnere34b3962005-01-19 04:19:40 +00003692 case ISD::SHL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003693 // If the target wants custom lowering, do so.
3694 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3695 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3696 LegalizeOp(Node->getOperand(1)));
3697 Op = TLI.LowerOperation(Op, DAG);
3698 if (Op.Val) {
3699 // Now that the custom expander is done, expand the result, which is
3700 // still VT.
3701 ExpandOp(Op, Lo, Hi);
3702 break;
3703 }
3704 }
3705
Chris Lattnere34b3962005-01-19 04:19:40 +00003706 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003707 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003708 break;
Chris Lattner47599822005-04-02 03:38:53 +00003709
3710 // If this target supports SHL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003711 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003712 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3713 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003714 break;
3715 }
3716
Chris Lattnere34b3962005-01-19 04:19:40 +00003717 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003718 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003719 break;
3720
3721 case ISD::SRA:
Chris Lattner50ec8972005-08-31 19:01:53 +00003722 // If the target wants custom lowering, do so.
3723 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3724 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3725 LegalizeOp(Node->getOperand(1)));
3726 Op = TLI.LowerOperation(Op, DAG);
3727 if (Op.Val) {
3728 // Now that the custom expander is done, expand the result, which is
3729 // still VT.
3730 ExpandOp(Op, Lo, Hi);
3731 break;
3732 }
3733 }
3734
Chris Lattnere34b3962005-01-19 04:19:40 +00003735 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003736 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003737 break;
Chris Lattner47599822005-04-02 03:38:53 +00003738
3739 // If this target supports SRA_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003740 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003741 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3742 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003743 break;
3744 }
3745
Chris Lattnere34b3962005-01-19 04:19:40 +00003746 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003747 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003748 break;
3749 case ISD::SRL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003750 // If the target wants custom lowering, do so.
3751 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3752 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3753 LegalizeOp(Node->getOperand(1)));
3754 Op = TLI.LowerOperation(Op, DAG);
3755 if (Op.Val) {
3756 // Now that the custom expander is done, expand the result, which is
3757 // still VT.
3758 ExpandOp(Op, Lo, Hi);
3759 break;
3760 }
3761 }
3762
Chris Lattnere34b3962005-01-19 04:19:40 +00003763 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003764 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003765 break;
Chris Lattner47599822005-04-02 03:38:53 +00003766
3767 // If this target supports SRL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003768 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003769 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3770 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003771 break;
3772 }
3773
Chris Lattnere34b3962005-01-19 04:19:40 +00003774 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003775 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003776 break;
3777
Misha Brukmanedf128a2005-04-21 22:36:52 +00003778 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003779 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3780 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003781 break;
3782 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003783 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3784 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003785 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003786 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003787 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003788 SDOperand LL, LH, RL, RH;
3789 ExpandOp(Node->getOperand(0), LL, LH);
3790 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003791 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3792 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3793 // extended the sign bit of the low half through the upper half, and if so
3794 // emit a MULHS instead of the alternate sequence that is valid for any
3795 // i64 x i64 multiply.
3796 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3797 // is RH an extension of the sign bit of RL?
3798 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3799 RH.getOperand(1).getOpcode() == ISD::Constant &&
3800 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3801 // is LH an extension of the sign bit of LL?
3802 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3803 LH.getOperand(1).getOpcode() == ISD::Constant &&
3804 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3805 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3806 } else {
3807 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3808 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3809 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3810 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3811 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3812 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003813 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3814 } else {
3815 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3816 }
3817 break;
3818 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003819 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3820 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3821 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3822 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003823 }
3824
3825 // Remember in a map if the values will be reused later.
Chris Lattner6fdcb252005-09-02 20:32:45 +00003826 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3827 std::make_pair(Lo, Hi))).second;
3828 assert(isNew && "Value already expanded?!?");
Chris Lattner69a889e2005-12-20 00:53:54 +00003829
3830 // Make sure the resultant values have been legalized themselves.
3831 Lo = LegalizeOp(Lo);
3832 Hi = LegalizeOp(Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003833}
3834
3835
3836// SelectionDAG::Legalize - This is the entry point for the file.
3837//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003838void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003839 /// run - This is the main entry point to this class.
3840 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003841 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003842}
3843