blob: d76a89df8fe9a1305e3a83cfecd6a059f4692275 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-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 Lattnerdc750592005-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 Begeman89b049a2005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattnerdc750592005-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 Lattner1f2c9d82005-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 Lattnerdc750592005-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 Lattnerea4ca942005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-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 Lattnerea4ca942005-01-07 22:28:47 +000084 }
Chris Lattner1f2c9d82005-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 Lattner2af3ee42005-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 Lattner1f2c9d82005-01-15 05:21:40 +000090 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000091
Chris Lattnerdc750592005-01-07 07:47:09 +000092public:
93
Chris Lattner4add7e32005-01-23 04:42:50 +000094 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-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 Lattner1f2c9d82005-01-15 05:21:40 +0000126 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000127
Chris Lattneraac464e2005-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 Lattnere3e847b2005-07-16 00:19:57 +0000132
Jim Laskeyf2516a92005-08-17 00:39:29 +0000133 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
134 SDOperand LegalOp,
135 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000136 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
137 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000138 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
139 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000140
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000141 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
142 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-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 Lattner2e5872c2005-04-02 03:38:53 +0000146 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000147
Chris Lattnera5bf1032005-05-12 04:49:08 +0000148 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
149
Chris Lattnerdc750592005-01-07 07:47:09 +0000150 SDOperand getIntPtrConstant(uint64_t Val) {
151 return DAG.getConstant(Val, TLI.getPointerTy());
152 }
153};
154}
155
Chris Lattner301015a2005-11-19 05:51:46 +0000156static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000157 switch (VecOp) {
158 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-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 Lattnerdc750592005-01-07 07:47:09 +0000164
Chris Lattner4add7e32005-01-23 04:42:50 +0000165SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
166 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
167 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000168 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000169 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000170}
171
Jim Laskeyf2516a92005-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 Lattnere3e847b2005-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 Laskeyf2516a92005-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 Laskey686d6a12005-08-17 17:42:52 +0000219 SDOperand Bias = DAG.getConstantFP(isSigned ?
220 BitsToDouble(0x4330000080000000ULL)
221 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000222 MVT::f64);
223 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000224 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-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 Lattner2af3ee42005-12-20 00:53:54 +0000235 return LegalizeOp(Result);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000236 }
237 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000238 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000239
Chris Lattnerd47675e2005-08-09 20:20:18 +0000240 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
241 DAG.getConstant(0, Op0.getValueType()),
242 ISD::SETLT);
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000246
Jim Laskeyf2516a92005-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 Lattnerb35912e2005-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 Lattnere3e847b2005-07-16 00:19:57 +0000258 if (TLI.isLittleEndian()) FF <<= 32;
259 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000260
Chris Lattnerc30405e2005-08-26 17:15:30 +0000261 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000273
Chris Lattner2af3ee42005-12-20 00:53:54 +0000274 return LegalizeOp(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000275}
276
Chris Lattner19732782005-08-16 18:17:10 +0000277/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000278/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-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 Begeman7e74c832005-07-16 02:02:34 +0000282SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
283 MVT::ValueType DestVT,
284 bool isSigned) {
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000287
Chris Lattnere3e847b2005-07-16 00:19:57 +0000288 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000289
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000294
Chris Lattnere3e847b2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000299 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-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 Begeman7e74c832005-07-16 02:02:34 +0000307 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000308
Chris Lattnere3e847b2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000313 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000321
Chris Lattnere3e847b2005-07-16 00:19:57 +0000322 // Otherwise, try a larger type.
323 }
324
Chris Lattnere3e847b2005-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 Lattner2af3ee42005-12-20 00:53:54 +0000327 SDOperand N = DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000328 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
329 NewInTy, LegalOp));
Chris Lattner2af3ee42005-12-20 00:53:54 +0000330 // Make sure to legalize any nodes we create here.
331 return LegalizeOp(N);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000332}
333
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000344
Chris Lattner44fe26f2005-07-29 00:11:56 +0000345 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000346
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000351
Chris Lattner44fe26f2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000356 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000364
Chris Lattner44fe26f2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000369 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000377
Chris Lattner44fe26f2005-07-29 00:11:56 +0000378 // Otherwise, try a larger type.
379 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000380
Chris Lattner44fe26f2005-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 Lattner2af3ee42005-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 Lattner44fe26f2005-07-29 00:11:56 +0000387}
388
Chris Lattner4bbbb9e2005-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 Lattner4bbbb9e2005-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 Lattner44fe26f2005-07-29 00:11:56 +0000409
Chris Lattnerdc750592005-01-07 07:47:09 +0000410void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-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 Lattner4bbbb9e2005-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 Lattner9cfccfb2005-10-02 17:49:46 +0000419
Chris Lattner4bbbb9e2005-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 Lattnerbf4f23322005-11-09 23:47:37 +0000424 if (I->getNumOperands() == 0) {
425 Visited[I] = 0 - 1U;
426 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000427 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000428 }
429
Chris Lattnerbf4f23322005-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 Lattner4bbbb9e2005-10-06 01:20:27 +0000433 "Error: DAG is cyclic!");
434 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000435
Chris Lattner4bbbb9e2005-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 Lattnerdc750592005-01-07 07:47:09 +0000455 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000456 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
457 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000458
459 ExpandedNodes.clear();
460 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000461 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000462
463 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000464 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000465}
466
467SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000468 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000469 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000470 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000471
Chris Lattnerdc750592005-01-07 07:47:09 +0000472 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000473 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-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 Lattnerdc750592005-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 Lattner1f2c9d82005-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 Lattnerdc750592005-01-07 07:47:09 +0000490 }
491 }
492
Chris Lattnerb5a78e02005-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 Lattner85d70c62005-01-11 05:57:22 +0000495 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
496 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000497
Nate Begemane5b86d72005-08-10 20:51:12 +0000498 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000499
500 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000501
502 switch (Node->getOpcode()) {
503 default:
Chris Lattner3eb86932005-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 Lattnerdc750592005-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 Lattner4bbbb9e2005-10-06 01:20:27 +0000532 case ISD::TargetFrameIndex:
533 case ISD::Register:
534 case ISD::TargetConstant:
Nate Begeman4e56db62005-12-10 02:36:00 +0000535 case ISD::TargetConstantPool:
Chris Lattnerdc750592005-01-07 07:47:09 +0000536 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000537 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000538 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000539 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000540 case ISD::BasicBlock:
541 case ISD::CONDCODE:
542 case ISD::VALUETYPE:
543 case ISD::SRCVALUE:
Chris Lattner435b4022005-11-29 06:21:05 +0000544 case ISD::STRING:
Chris Lattner45ca1c02005-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 Lattnerdc750592005-01-07 07:47:09 +0000558 break;
Chris Lattnerd9af1aa2005-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 Lattner44c28c22005-11-20 22:56:56 +0000566 case ISD::MERGE_VALUES:
567 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000568 case ISD::CopyFromReg:
569 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000570 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000571 if (Node->getNumValues() == 2) {
Chris Lattnere3c67e92005-12-18 15:27:43 +0000572 if (Tmp1 != Node->getOperand(0))
573 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattner33182322005-08-16 21:55:35 +0000574 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattnere3c67e92005-12-18 15:27:43 +0000575 Node->getValueType(0));
576 } else {
Chris Lattnerebcfa0c22005-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 Lattnere3c67e92005-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 Lattnereb6614d2005-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 Lattnere727af02005-01-13 20:50:02 +0000592 case ISD::ImplicitDef:
593 Tmp1 = LegalizeOp(Node->getOperand(0));
594 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000595 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
596 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000597 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000598 case ISD::UNDEF: {
599 MVT::ValueType VT = Op.getValueType();
600 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000601 default: assert(0 && "This action is not supported yet!");
602 case TargetLowering::Expand:
603 case TargetLowering::Promote:
Nate Begemancda9aa72005-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 Begeman69d39432005-04-02 00:41:14 +0000611 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000612 break;
613 }
614 break;
615 }
Chris Lattner435b4022005-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 Laskey7c462762005-12-16 22:45:29 +0000624 case TargetLowering::Expand: {
Chris Lattnerc06da622005-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 Lattner435b4022005-11-29 06:21:05 +0000638 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000639 }
Chris Lattner435b4022005-11-29 06:21:05 +0000640 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000641 if (Tmp1 != Node->getOperand(0) ||
642 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000643 std::vector<SDOperand> Ops;
644 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-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 Lattner435b4022005-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 Laskey7c462762005-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 Lattner435b4022005-11-29 06:21:05 +0000682
Chris Lattnerdc750592005-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 Lattnerdc750592005-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 Lattnerbc7497d2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000721 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000722 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
723 VT = MVT::f32;
724 Extend = true;
725 }
Misha Brukman835702a2005-04-21 22:36:52 +0000726
Nate Begeman4e56db62005-12-10 02:36:00 +0000727 SDOperand CPIdx =
728 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000729 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000730 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
731 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000732 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000733 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
734 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000735 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000736 }
737 break;
738 }
Nate Begeman41b1cdc2005-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 Begeman956aef42005-12-13 03:03:23 +0000761 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000762 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
763 break;
764 }
Chris Lattneraf3aefa2005-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 Lattner05b4e372005-01-13 17:59:25 +0000783 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000784 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000785
Chris Lattner2dce7032005-05-12 23:24:06 +0000786 case ISD::CALLSEQ_START:
787 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000789 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000790 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000791 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000792 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000793
Chris Lattner2dce7032005-05-12 23:24:06 +0000794 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-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 Lattnerdc750592005-01-07 07:47:09 +0000798 break;
Chris Lattnerec26b482005-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 Lattner96c262e2005-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 Lattner02f5ce22005-01-09 19:07:54 +0000810 Result = Op.getValue(0);
Chris Lattnerec26b482005-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 Lattnerd0feb642005-05-13 18:43:43 +0000818 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000819 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000820 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
821 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-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 Lattnerdc750592005-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 Lattnerf025d672005-01-07 21:34:13 +0000834 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000835 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
836 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000837 } else {
838 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000839 }
Chris Lattner9242c502005-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 Lattner3d95c142005-01-19 20:24:35 +0000845 }
Chris Lattner68a12142005-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 Lattnerec3fe7c2005-01-07 08:19:42 +0000852 case ISD::BRCOND:
853 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000854
Chris Lattnerd65c3f32005-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 Begeman371e4952005-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 Lattner539c3fa2005-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 Begeman371e4952005-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 Lattnerc06da622005-12-18 23:54:29 +0000884 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +0000885 break;
Evan Cheng6fc31042005-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 Begeman371e4952005-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 Lattnerbf0bd992005-12-17 23:46:46 +0000906 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-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 Lattnerbf0bd992005-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 Begeman371e4952005-08-16 19:49:35 +0000949 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000950 break;
Chris Lattnerfd986782005-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 Begeman371e4952005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000981 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-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 Lattnerfd986782005-04-09 03:30:19 +0000993 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000994 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
995 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000996 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattnerfd986782005-04-09 03:30:19 +0000997 break;
998 }
999 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001000 case ISD::BRTWOWAY_CC:
1001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001002 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-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));
1039 break;
1040 }
Chris Lattner0fab4592005-12-21 19:40:42 +00001041 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +00001042 }
1043 break;
Chris Lattnerdc750592005-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 Lenharth4a73c2c2005-04-27 20:10:01 +00001047
Chris Lattnerdc750592005-01-07 07:47:09 +00001048 if (Tmp1 != Node->getOperand(0) ||
1049 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +00001050 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1051 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001052 else
1053 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +00001054
Chris Lattnerea4ca942005-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 Begemanb2e089c2005-11-19 00:36:38 +00001060
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001061 case ISD::EXTLOAD:
1062 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001063 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001064 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1065 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001066
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001067 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001068 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001069 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001070 case TargetLowering::Promote:
1071 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001072 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1073 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-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 Brukman835702a2005-04-21 22:36:52 +00001079
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001080 case TargetLowering::Legal:
1081 if (Tmp1 != Node->getOperand(0) ||
1082 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001083 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1084 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-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 Lattnera3b7ef02005-04-10 22:54:25 +00001093 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001094 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-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 Lenharth0a370f42005-06-30 19:32:57 +00001097 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc06da622005-12-18 23:54:29 +00001098 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001099 Load = LegalizeOp(Load);
1100 AddLegalizedOperand(SDOperand(Node, 0), Result);
1101 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001102 if (Op.ResNo)
1103 return Load.getValue(1);
1104 return Result;
1105 }
Chris Lattnera3b7ef02005-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 Lattnerde0a4b12005-07-10 01:55:33 +00001110 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1111 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-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 Lattner0b6ba902005-07-10 00:07:11 +00001115 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001116 else
1117 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc06da622005-12-18 23:54:29 +00001118 Result = LegalizeOp(Result); // Relegalize new nodes.
1119 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001120 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1121 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001122 if (Op.ResNo)
1123 return Result.getValue(1);
1124 return ValRes;
1125 }
1126 assert(0 && "Unreachable");
1127 }
Nate Begeman5172ce62005-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 Lattnerdc750592005-01-07 07:47:09 +00001157 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001158 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001159
1160 case ISD::CopyToReg:
1161 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001162
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001163 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001164 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001165 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001166 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001167 if (Node->getNumValues() == 1) {
Chris Lattnere3c67e92005-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 Lattnerebcfa0c22005-12-18 15:36:21 +00001172 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1173 if (Node->getNumOperands() == 4)
1174 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001175 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001176 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattnere3c67e92005-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 Lattnerdc750592005-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 Lattnerea4ca942005-01-07 22:28:47 +00001196 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00001203 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001204 }
1205 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001206 Tmp2 = PromoteOp(Node->getOperand(1));
1207 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1208 break;
Chris Lattnerdc750592005-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 Lattner7e6eeba2005-01-08 19:27:05 +00001221 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00001228 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001229 }
1230 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001231 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-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 Lattnere69daaf2005-01-08 06:25:56 +00001242 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001243 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001244 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001245 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001246 DAG.getConstant(FloatToBits(CFP->getValue()),
1247 MVT::i32),
1248 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001249 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001250 } else {
1251 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001252 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001253 DAG.getConstant(DoubleToBits(CFP->getValue()),
1254 MVT::i64),
1255 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001256 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001257 }
Chris Lattnera4743132005-02-22 07:23:39 +00001258 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001259 }
1260
Chris Lattnerdc750592005-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 Lattner5385db52005-05-09 20:23:03 +00001266 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1267 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001268 break;
1269 }
1270 case Promote:
Chris Lattner1f2c9d82005-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 Lenharth4a73c2c2005-04-27 20:10:01 +00001274 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001275 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001276 break;
1277
Chris Lattnerdc750592005-01-07 07:47:09 +00001278 case Expand:
1279 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001280 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001281 ExpandOp(Node->getOperand(1), Lo, Hi);
1282
1283 if (!TLI.isLittleEndian())
1284 std::swap(Lo, Hi);
1285
Chris Lattner55e9cde2005-05-11 04:51:16 +00001286 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1287 Node->getOperand(3));
Nate Begemand37c1312005-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 Lattnerdc750592005-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 Lenharth4a73c2c2005-04-27 20:10:01 +00001302 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001303 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1304 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001305 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1306 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001307 }
1308 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001309 case ISD::PCMARKER:
1310 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001311 if (Tmp1 != Node->getOperand(0))
1312 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001313 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001314 case ISD::READCYCLECOUNTER:
1315 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthf9b27d72005-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 Lenharth73420b32005-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 Lenharth627cbd42005-11-20 21:32:07 +00001330
Chris Lattnerc6c9a5b2005-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 Lattner2d454bf2005-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 Lattner99222f72005-01-15 07:15:18 +00001352 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001353 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001354 }
Chris Lattnerc6c9a5b2005-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 Lattner39c67442005-01-14 22:08:15 +00001361 case ISD::SELECT:
Chris Lattnerd65c3f32005-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 Lattnerdc750592005-01-07 07:47:09 +00001371 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001372 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001373
Nate Begeman987121a2005-08-23 04:29:48 +00001374 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001375 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-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 Lattner539c3fa2005-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 Begemane5b86d72005-08-10 20:51:12 +00001385 Result = DAG.getSelectCC(Tmp1,
1386 DAG.getConstant(0, Tmp1.getValueType()),
1387 Tmp2, Tmp3, ISD::SETNE);
1388 }
Chris Lattnerc06da622005-12-18 23:54:29 +00001389 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begemane5b86d72005-08-10 20:51:12 +00001390 break;
Evan Cheng225a4d02005-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 Lattner3c0dd462005-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 Lattner7753f172005-09-02 00:18:10 +00001412 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-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 Lattnerdc750592005-01-07 07:47:09 +00001427 break;
Nate Begemane5b86d72005-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 Lattnerf12eb4d2005-08-24 16:35:28 +00001432 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-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 Lattnerc6d481d2005-08-26 00:43:46 +00001442 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-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 Begemane5b86d72005-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 Begeman371e4952005-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 Begemane5b86d72005-08-10 20:51:12 +00001477 }
1478 break;
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +00001484 break;
1485 case Promote:
Chris Lattner4d978642005-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 Lattner87a769c2005-01-16 01:11:45 +00001492 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-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 Lattnerd47675e2005-08-09 20:20:18 +00001498 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-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 Lattner0e852af2005-04-13 02:38:47 +00001509 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1510 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001511 break;
1512 case ISD::SETGE:
1513 case ISD::SETGT:
1514 case ISD::SETLT:
1515 case ISD::SETLE:
Chris Lattner0b6ba902005-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 Lattner4d978642005-01-15 22:16:26 +00001520 break;
1521 }
Chris Lattner4d978642005-01-15 22:16:26 +00001522 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001523 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001524 case Expand:
Chris Lattnerdc750592005-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 Lattnerd47675e2005-08-09 20:20:18 +00001528 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001529 case ISD::SETEQ:
1530 case ISD::SETNE:
Chris Lattner71ff44e2005-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 Begeman987121a2005-08-23 04:29:48 +00001536 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001537 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001538 }
1539
Chris Lattnerdc750592005-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 Begeman987121a2005-08-23 04:29:48 +00001543 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001544 break;
1545 default:
Chris Lattneraedcabe2005-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 Lattnerd47675e2005-08-09 20:20:18 +00001549 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001550 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001551 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001552 (CST->isAllOnesValue()))) { // X > -1
1553 Tmp1 = LHSHi;
1554 Tmp2 = RHSHi;
1555 break;
1556 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001557
Chris Lattnerdc750592005-01-07 07:47:09 +00001558 // FIXME: This generated code sucks.
1559 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001560 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00001571
Chris Lattnerdc750592005-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 Lattnerd47675e2005-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 Begeman987121a2005-08-23 04:29:48 +00001582 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1583 Result, Tmp1, Tmp2));
Chris Lattner2af3ee42005-12-20 00:53:54 +00001584 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman987121a2005-08-23 04:29:48 +00001585 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001586 }
1587 }
Nate Begeman987121a2005-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 Lenharth6ee85662005-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 Lenharth835cbb32005-08-29 20:46:51 +00001620 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1621 Node->getOperand(2));
1622 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001623 }
Evan Chengc1583db2005-12-21 20:21:51 +00001624 case TargetLowering::Custom: {
1625 SDOperand Tmp =
1626 TLI.LowerOperation(DAG.getNode(ISD::SETCC, Node->getValueType(0),
1627 Tmp1, Tmp2, Node->getOperand(2)), DAG);
1628 if (Tmp.Val) {
1629 Result = LegalizeOp(Tmp);
1630 break;
1631 }
1632 // FALLTHROUGH if the target thinks it is legal.
1633 }
Nate Begeman987121a2005-08-23 04:29:48 +00001634 case TargetLowering::Legal:
1635 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1636 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1637 Node->getOperand(2));
1638 break;
1639 case TargetLowering::Expand:
1640 // Expand a setcc node into a select_cc of the same condition, lhs, and
1641 // rhs that selects between const 1 (true) and const 0 (false).
1642 MVT::ValueType VT = Node->getValueType(0);
1643 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1644 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1645 Node->getOperand(2));
1646 Result = LegalizeOp(Result);
1647 break;
1648 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001649 break;
1650
Chris Lattner85d70c62005-01-11 05:57:22 +00001651 case ISD::MEMSET:
1652 case ISD::MEMCPY:
1653 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001654 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001655 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1656
1657 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1658 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1659 case Expand: assert(0 && "Cannot expand a byte!");
1660 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001661 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001662 break;
1663 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001664 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001665 break;
1666 }
1667 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001668 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001669 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001670
1671 SDOperand Tmp4;
1672 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001673 case Expand: {
1674 // Length is too big, just take the lo-part of the length.
1675 SDOperand HiPart;
1676 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1677 break;
1678 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001679 case Legal:
1680 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001681 break;
1682 case Promote:
1683 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001684 break;
1685 }
1686
1687 SDOperand Tmp5;
1688 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1689 case Expand: assert(0 && "Cannot expand this yet!");
1690 case Legal:
1691 Tmp5 = LegalizeOp(Node->getOperand(4));
1692 break;
1693 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001694 Tmp5 = PromoteOp(Node->getOperand(4));
1695 break;
1696 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001697
1698 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1699 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001700 case TargetLowering::Custom: {
1701 SDOperand Tmp =
1702 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1703 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1704 if (Tmp.Val) {
1705 Result = LegalizeOp(Tmp);
1706 break;
1707 }
1708 // FALLTHROUGH if the target thinks it is legal.
1709 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001710 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001711 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1712 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1713 Tmp5 != Node->getOperand(4)) {
1714 std::vector<SDOperand> Ops;
1715 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1716 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1717 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1718 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001719 break;
1720 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001721 // Otherwise, the target does not support this operation. Lower the
1722 // operation to an explicit libcall as appropriate.
1723 MVT::ValueType IntPtr = TLI.getPointerTy();
1724 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1725 std::vector<std::pair<SDOperand, const Type*> > Args;
1726
Reid Spencer6dced922005-01-12 14:53:45 +00001727 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001728 if (Node->getOpcode() == ISD::MEMSET) {
1729 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1730 // Extend the ubyte argument to be an int value for the call.
1731 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1732 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1733 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1734
1735 FnName = "memset";
1736 } else if (Node->getOpcode() == ISD::MEMCPY ||
1737 Node->getOpcode() == ISD::MEMMOVE) {
1738 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1739 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1740 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1741 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1742 } else {
1743 assert(0 && "Unknown op!");
1744 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001745
Chris Lattner85d70c62005-01-11 05:57:22 +00001746 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001747 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001748 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner2af3ee42005-12-20 00:53:54 +00001749 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001750 break;
1751 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001752 }
1753 break;
1754 }
Chris Lattner5385db52005-05-09 20:23:03 +00001755
1756 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001757 Tmp1 = LegalizeOp(Node->getOperand(0));
1758 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001759
Chris Lattner86535992005-05-14 07:45:46 +00001760 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1761 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1762 std::vector<SDOperand> Ops;
1763 Ops.push_back(Tmp1);
1764 Ops.push_back(Tmp2);
1765 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1766 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001767 Result = SDOperand(Node, 0);
1768 // Since these produce two values, make sure to remember that we legalized
1769 // both of them.
1770 AddLegalizedOperand(SDOperand(Node, 0), Result);
1771 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1772 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001773 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001774 Tmp1 = LegalizeOp(Node->getOperand(0));
1775 Tmp2 = LegalizeOp(Node->getOperand(1));
1776 Tmp3 = LegalizeOp(Node->getOperand(2));
1777 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1778 Tmp3 != Node->getOperand(2))
1779 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1780 break;
1781
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001782 case ISD::READIO:
1783 Tmp1 = LegalizeOp(Node->getOperand(0));
1784 Tmp2 = LegalizeOp(Node->getOperand(1));
1785
1786 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1787 case TargetLowering::Custom:
1788 default: assert(0 && "This action not implemented for this operation!");
1789 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001790 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1791 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1792 std::vector<SDOperand> Ops;
1793 Ops.push_back(Tmp1);
1794 Ops.push_back(Tmp2);
1795 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1796 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001797 Result = SDOperand(Node, 0);
1798 break;
1799 case TargetLowering::Expand:
1800 // Replace this with a load from memory.
1801 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1802 Node->getOperand(1), DAG.getSrcValue(NULL));
1803 Result = LegalizeOp(Result);
1804 break;
1805 }
1806
1807 // Since these produce two values, make sure to remember that we legalized
1808 // both of them.
1809 AddLegalizedOperand(SDOperand(Node, 0), Result);
1810 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1811 return Result.getValue(Op.ResNo);
1812
1813 case ISD::WRITEIO:
1814 Tmp1 = LegalizeOp(Node->getOperand(0));
1815 Tmp2 = LegalizeOp(Node->getOperand(1));
1816 Tmp3 = LegalizeOp(Node->getOperand(2));
1817
1818 switch (TLI.getOperationAction(Node->getOpcode(),
1819 Node->getOperand(1).getValueType())) {
1820 case TargetLowering::Custom:
1821 default: assert(0 && "This action not implemented for this operation!");
1822 case TargetLowering::Legal:
1823 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1824 Tmp3 != Node->getOperand(2))
1825 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1826 break;
1827 case TargetLowering::Expand:
1828 // Replace this with a store to memory.
1829 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1830 Node->getOperand(1), Node->getOperand(2),
1831 DAG.getSrcValue(NULL));
1832 Result = LegalizeOp(Result);
1833 break;
1834 }
1835 break;
1836
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001837 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001838 case ISD::SUB_PARTS:
1839 case ISD::SHL_PARTS:
1840 case ISD::SRA_PARTS:
1841 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001842 std::vector<SDOperand> Ops;
1843 bool Changed = false;
1844 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1845 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1846 Changed |= Ops.back() != Node->getOperand(i);
1847 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001848 if (Changed) {
1849 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1850 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1851 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001852
1853 // Since these produce multiple values, make sure to remember that we
1854 // legalized all of them.
1855 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1856 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1857 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001858 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001859
1860 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001861 case ISD::ADD:
1862 case ISD::SUB:
1863 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001864 case ISD::MULHS:
1865 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001866 case ISD::UDIV:
1867 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001868 case ISD::AND:
1869 case ISD::OR:
1870 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001871 case ISD::SHL:
1872 case ISD::SRL:
1873 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001874 case ISD::FADD:
1875 case ISD::FSUB:
1876 case ISD::FMUL:
1877 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001878 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001879 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1880 case Expand: assert(0 && "Not possible");
1881 case Legal:
1882 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1883 break;
1884 case Promote:
1885 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1886 break;
1887 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001888 if (Tmp1 != Node->getOperand(0) ||
1889 Tmp2 != Node->getOperand(1))
1890 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1891 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001892
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001893 case ISD::BUILD_PAIR: {
1894 MVT::ValueType PairTy = Node->getValueType(0);
1895 // TODO: handle the case where the Lo and Hi operands are not of legal type
1896 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1897 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1898 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1899 case TargetLowering::Legal:
1900 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1901 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1902 break;
1903 case TargetLowering::Promote:
1904 case TargetLowering::Custom:
1905 assert(0 && "Cannot promote/custom this yet!");
1906 case TargetLowering::Expand:
1907 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1908 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1909 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1910 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1911 TLI.getShiftAmountTy()));
1912 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1913 break;
1914 }
1915 break;
1916 }
1917
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001918 case ISD::UREM:
1919 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001920 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001921 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1922 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1923 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1924 case TargetLowering::Legal:
1925 if (Tmp1 != Node->getOperand(0) ||
1926 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001927 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001928 Tmp2);
1929 break;
1930 case TargetLowering::Promote:
1931 case TargetLowering::Custom:
1932 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001933 case TargetLowering::Expand:
1934 if (MVT::isInteger(Node->getValueType(0))) {
1935 MVT::ValueType VT = Node->getValueType(0);
1936 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1937 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1938 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1939 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1940 } else {
1941 // Floating point mod -> fmod libcall.
1942 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1943 SDOperand Dummy;
1944 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001945 }
1946 break;
1947 }
1948 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001949
Andrew Lenharth5e177822005-05-03 17:19:30 +00001950 case ISD::CTPOP:
1951 case ISD::CTTZ:
1952 case ISD::CTLZ:
1953 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1954 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1955 case TargetLowering::Legal:
1956 if (Tmp1 != Node->getOperand(0))
1957 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1958 break;
1959 case TargetLowering::Promote: {
1960 MVT::ValueType OVT = Tmp1.getValueType();
1961 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001962
1963 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001964 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1965 // Perform the larger operation, then subtract if needed.
1966 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1967 switch(Node->getOpcode())
1968 {
1969 case ISD::CTPOP:
1970 Result = Tmp1;
1971 break;
1972 case ISD::CTTZ:
1973 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001974 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1975 DAG.getConstant(getSizeInBits(NVT), NVT),
1976 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001977 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001978 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1979 break;
1980 case ISD::CTLZ:
1981 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001982 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1983 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001984 getSizeInBits(OVT), NVT));
1985 break;
1986 }
1987 break;
1988 }
1989 case TargetLowering::Custom:
1990 assert(0 && "Cannot custom handle this yet!");
1991 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001992 switch(Node->getOpcode())
1993 {
1994 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001995 static const uint64_t mask[6] = {
1996 0x5555555555555555ULL, 0x3333333333333333ULL,
1997 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1998 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1999 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002000 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00002001 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2002 unsigned len = getSizeInBits(VT);
2003 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002004 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00002005 Tmp2 = DAG.getConstant(mask[i], VT);
2006 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002007 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002008 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
2009 DAG.getNode(ISD::AND, VT,
2010 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
2011 Tmp2));
2012 }
2013 Result = Tmp1;
2014 break;
2015 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002016 case ISD::CTLZ: {
2017 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00002018 x = x | (x >> 1);
2019 x = x | (x >> 2);
2020 ...
2021 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002022 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00002023 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002024
Chris Lattner56add052005-05-11 18:35:21 +00002025 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2026 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002027 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2028 unsigned len = getSizeInBits(VT);
2029 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2030 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002031 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002032 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2033 }
2034 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00002035 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00002036 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002037 }
2038 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002039 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002040 // unless the target has ctlz but not ctpop, in which case we use:
2041 // { return 32 - nlz(~x & (x-1)); }
2042 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00002043 MVT::ValueType VT = Tmp1.getValueType();
2044 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002045 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00002046 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2047 DAG.getNode(ISD::SUB, VT, Tmp1,
2048 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002049 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002050 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2051 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002052 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002053 DAG.getConstant(getSizeInBits(VT), VT),
2054 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2055 } else {
2056 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2057 }
Chris Lattner72473242005-05-11 05:27:09 +00002058 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002059 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002060 default:
2061 assert(0 && "Cannot expand this yet!");
2062 break;
2063 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002064 break;
2065 }
2066 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002067
Chris Lattner13fe99c2005-04-02 05:00:07 +00002068 // Unary operators
2069 case ISD::FABS:
2070 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002071 case ISD::FSQRT:
2072 case ISD::FSIN:
2073 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002074 Tmp1 = LegalizeOp(Node->getOperand(0));
2075 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2076 case TargetLowering::Legal:
2077 if (Tmp1 != Node->getOperand(0))
2078 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2079 break;
2080 case TargetLowering::Promote:
2081 case TargetLowering::Custom:
2082 assert(0 && "Cannot promote/custom handle this yet!");
2083 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00002084 switch(Node->getOpcode()) {
2085 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00002086 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2087 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00002088 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00002089 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00002090 break;
2091 }
2092 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002093 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2094 MVT::ValueType VT = Node->getValueType(0);
2095 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002096 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002097 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2098 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2099 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00002100 break;
2101 }
2102 case ISD::FSQRT:
2103 case ISD::FSIN:
2104 case ISD::FCOS: {
2105 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002106 const char *FnName = 0;
2107 switch(Node->getOpcode()) {
2108 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2109 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2110 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2111 default: assert(0 && "Unreachable!");
2112 }
Nate Begeman77558da2005-08-04 21:43:28 +00002113 SDOperand Dummy;
2114 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002115 break;
2116 }
2117 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002118 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002119 }
2120 break;
2121 }
2122 break;
2123
2124 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002125 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002126 case ISD::UINT_TO_FP: {
2127 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002128 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2129 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002130 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002131 Node->getOperand(0).getValueType())) {
2132 default: assert(0 && "Unknown operation action!");
2133 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002134 Result = ExpandLegalINT_TO_FP(isSigned,
2135 LegalizeOp(Node->getOperand(0)),
2136 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002137 AddLegalizedOperand(Op, Result);
2138 return Result;
2139 case TargetLowering::Promote:
2140 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2141 Node->getValueType(0),
2142 isSigned);
2143 AddLegalizedOperand(Op, Result);
2144 return Result;
2145 case TargetLowering::Legal:
2146 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002147 case TargetLowering::Custom: {
2148 Tmp1 = LegalizeOp(Node->getOperand(0));
2149 SDOperand Tmp =
2150 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2151 Tmp = TLI.LowerOperation(Tmp, DAG);
2152 if (Tmp.Val) {
Chris Lattner2af3ee42005-12-20 00:53:54 +00002153 Tmp = LegalizeOp(Tmp); // Relegalize input.
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002154 AddLegalizedOperand(Op, Tmp);
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002155 return Tmp;
2156 } else {
2157 assert(0 && "Target Must Lower this");
2158 }
2159 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002160 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002161
Chris Lattnerdc750592005-01-07 07:47:09 +00002162 Tmp1 = LegalizeOp(Node->getOperand(0));
2163 if (Tmp1 != Node->getOperand(0))
2164 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2165 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002166 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002167 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2168 Node->getValueType(0), Node->getOperand(0));
2169 break;
2170 case Promote:
2171 if (isSigned) {
2172 Result = PromoteOp(Node->getOperand(0));
2173 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2174 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2175 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2176 } else {
2177 Result = PromoteOp(Node->getOperand(0));
2178 Result = DAG.getZeroExtendInReg(Result,
2179 Node->getOperand(0).getValueType());
2180 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002181 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002182 break;
2183 }
2184 break;
2185 }
2186 case ISD::TRUNCATE:
2187 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2188 case Legal:
2189 Tmp1 = LegalizeOp(Node->getOperand(0));
2190 if (Tmp1 != Node->getOperand(0))
2191 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2192 break;
2193 case Expand:
2194 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2195
2196 // Since the result is legal, we should just be able to truncate the low
2197 // part of the source.
2198 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2199 break;
2200 case Promote:
2201 Result = PromoteOp(Node->getOperand(0));
2202 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2203 break;
2204 }
2205 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002206
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002207 case ISD::FP_TO_SINT:
2208 case ISD::FP_TO_UINT:
2209 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2210 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002211 Tmp1 = LegalizeOp(Node->getOperand(0));
2212
Chris Lattner44fe26f2005-07-29 00:11:56 +00002213 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2214 default: assert(0 && "Unknown operation action!");
2215 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002216 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2217 SDOperand True, False;
2218 MVT::ValueType VT = Node->getOperand(0).getValueType();
2219 MVT::ValueType NVT = Node->getValueType(0);
2220 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2221 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2222 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2223 Node->getOperand(0), Tmp2, ISD::SETLT);
2224 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2225 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002226 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002227 Tmp2));
2228 False = DAG.getNode(ISD::XOR, NVT, False,
2229 DAG.getConstant(1ULL << ShiftAmt, NVT));
2230 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Chris Lattner2af3ee42005-12-20 00:53:54 +00002231 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemand5e739d2005-08-14 18:38:32 +00002232 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002233 } else {
2234 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2235 }
2236 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002237 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002238 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002239 Node->getOpcode() == ISD::FP_TO_SINT);
2240 AddLegalizedOperand(Op, Result);
2241 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002242 case TargetLowering::Custom: {
2243 SDOperand Tmp =
2244 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2245 Tmp = TLI.LowerOperation(Tmp, DAG);
2246 if (Tmp.Val) {
Chris Lattner2af3ee42005-12-20 00:53:54 +00002247 Tmp = LegalizeOp(Tmp);
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002248 AddLegalizedOperand(Op, Tmp);
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002249 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002250 } else {
2251 // The target thinks this is legal afterall.
2252 break;
2253 }
2254 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002255 case TargetLowering::Legal:
2256 break;
2257 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002258
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002259 if (Tmp1 != Node->getOperand(0))
2260 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2261 break;
2262 case Expand:
2263 assert(0 && "Shouldn't need to expand other operators here!");
2264 case Promote:
2265 Result = PromoteOp(Node->getOperand(0));
2266 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2267 break;
2268 }
2269 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002270
Chris Lattner7753f172005-09-02 00:18:10 +00002271 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002272 case ISD::ZERO_EXTEND:
2273 case ISD::SIGN_EXTEND:
2274 case ISD::FP_EXTEND:
2275 case ISD::FP_ROUND:
2276 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2277 case Legal:
2278 Tmp1 = LegalizeOp(Node->getOperand(0));
2279 if (Tmp1 != Node->getOperand(0))
2280 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2281 break;
2282 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002283 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002284
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002285 case Promote:
2286 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002287 case ISD::ANY_EXTEND:
2288 Result = PromoteOp(Node->getOperand(0));
2289 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2290 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002291 case ISD::ZERO_EXTEND:
2292 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002293 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002294 Result = DAG.getZeroExtendInReg(Result,
2295 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002296 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002297 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002298 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002299 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002300 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002301 Result,
2302 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002303 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002304 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002305 Result = PromoteOp(Node->getOperand(0));
2306 if (Result.getValueType() != Op.getValueType())
2307 // Dynamically dead while we have only 2 FP types.
2308 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2309 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002310 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002311 Result = PromoteOp(Node->getOperand(0));
2312 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2313 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002314 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002315 }
2316 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002317 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002318 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002319 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002320 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002321
2322 // If this operation is not supported, convert it to a shl/shr or load/store
2323 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002324 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2325 default: assert(0 && "This action not supported for this op yet!");
2326 case TargetLowering::Legal:
2327 if (Tmp1 != Node->getOperand(0))
2328 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002329 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002330 break;
2331 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002332 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002333 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002334 // NOTE: we could fall back on load/store here too for targets without
2335 // SAR. However, it is doubtful that any exist.
2336 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2337 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002338 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002339 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2340 Node->getOperand(0), ShiftCst);
2341 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2342 Result, ShiftCst);
2343 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2344 // The only way we can lower this is to turn it into a STORETRUNC,
2345 // EXTLOAD pair, targetting a temporary location (a stack slot).
2346
2347 // NOTE: there is a choice here between constantly creating new stack
2348 // slots and always reusing the same one. We currently always create
2349 // new ones, as reuse may inhibit scheduling.
2350 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2351 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2352 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2353 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002354 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002355 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2356 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2357 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002358 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002359 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002360 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2361 Result, StackSlot, DAG.getSrcValue(NULL),
2362 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002363 } else {
2364 assert(0 && "Unknown op");
2365 }
2366 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002367 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002368 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002369 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002370 }
Chris Lattner99222f72005-01-15 07:15:18 +00002371 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002372
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002373 // Note that LegalizeOp may be reentered even from single-use nodes, which
2374 // means that we always must cache transformed nodes.
2375 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002376 return Result;
2377}
2378
Chris Lattner4d978642005-01-15 22:16:26 +00002379/// PromoteOp - Given an operation that produces a value in an invalid type,
2380/// promote it to compute the value into a larger type. The produced value will
2381/// have the correct bits for the low portion of the register, but no guarantee
2382/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002383SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2384 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002385 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002386 assert(getTypeAction(VT) == Promote &&
2387 "Caller should expand or legalize operands that are not promotable!");
2388 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2389 "Cannot promote to smaller type!");
2390
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002391 SDOperand Tmp1, Tmp2, Tmp3;
2392
2393 SDOperand Result;
2394 SDNode *Node = Op.Val;
2395
Chris Lattner1a570f12005-09-02 20:32:45 +00002396 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2397 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002398
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002399 // Promotion needs an optimization step to clean up after it, and is not
2400 // careful to avoid operations the target does not support. Make sure that
2401 // all generated operations are legalized in the next iteration.
2402 NeedsAnotherIteration = true;
2403
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002404 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002405 case ISD::CopyFromReg:
2406 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002407 default:
2408 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2409 assert(0 && "Do not know how to promote this operator!");
2410 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002411 case ISD::UNDEF:
2412 Result = DAG.getNode(ISD::UNDEF, NVT);
2413 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002414 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002415 if (VT != MVT::i1)
2416 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2417 else
2418 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002419 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2420 break;
2421 case ISD::ConstantFP:
2422 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2423 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2424 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002425
Chris Lattner2cb338d2005-01-18 02:59:52 +00002426 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002427 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002428 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2429 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002430 Result = LegalizeOp(Result);
2431 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002432
2433 case ISD::TRUNCATE:
2434 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2435 case Legal:
2436 Result = LegalizeOp(Node->getOperand(0));
2437 assert(Result.getValueType() >= NVT &&
2438 "This truncation doesn't make sense!");
2439 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2440 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2441 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002442 case Promote:
2443 // The truncation is not required, because we don't guarantee anything
2444 // about high bits anyway.
2445 Result = PromoteOp(Node->getOperand(0));
2446 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002447 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002448 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2449 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002450 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002451 }
2452 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002453 case ISD::SIGN_EXTEND:
2454 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002455 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002456 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2457 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2458 case Legal:
2459 // Input is legal? Just do extend all the way to the larger type.
2460 Result = LegalizeOp(Node->getOperand(0));
2461 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2462 break;
2463 case Promote:
2464 // Promote the reg if it's smaller.
2465 Result = PromoteOp(Node->getOperand(0));
2466 // The high bits are not guaranteed to be anything. Insert an extend.
2467 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002468 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002469 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002470 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002471 Result = DAG.getZeroExtendInReg(Result,
2472 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002473 break;
2474 }
2475 break;
2476
2477 case ISD::FP_EXTEND:
2478 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2479 case ISD::FP_ROUND:
2480 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2481 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2482 case Promote: assert(0 && "Unreachable with 2 FP types!");
2483 case Legal:
2484 // Input is legal? Do an FP_ROUND_INREG.
2485 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002486 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2487 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002488 break;
2489 }
2490 break;
2491
2492 case ISD::SINT_TO_FP:
2493 case ISD::UINT_TO_FP:
2494 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2495 case Legal:
2496 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002497 // No extra round required here.
2498 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002499 break;
2500
2501 case Promote:
2502 Result = PromoteOp(Node->getOperand(0));
2503 if (Node->getOpcode() == ISD::SINT_TO_FP)
2504 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002505 Result,
2506 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002507 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002508 Result = DAG.getZeroExtendInReg(Result,
2509 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002510 // No extra round required here.
2511 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002512 break;
2513 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002514 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2515 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002516 // Round if we cannot tolerate excess precision.
2517 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002518 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2519 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002520 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002521 }
Chris Lattner4d978642005-01-15 22:16:26 +00002522 break;
2523
Chris Lattner268d4572005-12-09 17:32:47 +00002524 case ISD::SIGN_EXTEND_INREG:
2525 Result = PromoteOp(Node->getOperand(0));
2526 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2527 Node->getOperand(1));
2528 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002529 case ISD::FP_TO_SINT:
2530 case ISD::FP_TO_UINT:
2531 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2532 case Legal:
2533 Tmp1 = LegalizeOp(Node->getOperand(0));
2534 break;
2535 case Promote:
2536 // The input result is prerounded, so we don't have to do anything
2537 // special.
2538 Tmp1 = PromoteOp(Node->getOperand(0));
2539 break;
2540 case Expand:
2541 assert(0 && "not implemented");
2542 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002543 // If we're promoting a UINT to a larger size, check to see if the new node
2544 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2545 // we can use that instead. This allows us to generate better code for
2546 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2547 // legal, such as PowerPC.
2548 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002549 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002550 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2551 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002552 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2553 } else {
2554 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2555 }
Chris Lattner4d978642005-01-15 22:16:26 +00002556 break;
2557
Chris Lattner13fe99c2005-04-02 05:00:07 +00002558 case ISD::FABS:
2559 case ISD::FNEG:
2560 Tmp1 = PromoteOp(Node->getOperand(0));
2561 assert(Tmp1.getValueType() == NVT);
2562 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2563 // NOTE: we do not have to do any extra rounding here for
2564 // NoExcessFPPrecision, because we know the input will have the appropriate
2565 // precision, and these operations don't modify precision at all.
2566 break;
2567
Chris Lattner9d6fa982005-04-28 21:44:33 +00002568 case ISD::FSQRT:
2569 case ISD::FSIN:
2570 case ISD::FCOS:
2571 Tmp1 = PromoteOp(Node->getOperand(0));
2572 assert(Tmp1.getValueType() == NVT);
2573 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2574 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002575 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2576 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002577 break;
2578
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002579 case ISD::AND:
2580 case ISD::OR:
2581 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002582 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002583 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002584 case ISD::MUL:
2585 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002586 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002587 // that too is okay if they are integer operations.
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);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002592 break;
2593 case ISD::FADD:
2594 case ISD::FSUB:
2595 case ISD::FMUL:
2596 // The input may have strange things in the top bits of the registers, but
2597 // these operations don't care.
2598 Tmp1 = PromoteOp(Node->getOperand(0));
2599 Tmp2 = PromoteOp(Node->getOperand(1));
2600 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2601 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2602
2603 // Floating point operations will give excess precision that we may not be
2604 // able to tolerate. If we DO allow excess precision, just leave it,
2605 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002606 // FIXME: Why would we need to round FP ops more than integer ones?
2607 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002608 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002609 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2610 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002611 break;
2612
Chris Lattner4d978642005-01-15 22:16:26 +00002613 case ISD::SDIV:
2614 case ISD::SREM:
2615 // These operators require that their input be sign extended.
2616 Tmp1 = PromoteOp(Node->getOperand(0));
2617 Tmp2 = PromoteOp(Node->getOperand(1));
2618 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002619 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2620 DAG.getValueType(VT));
2621 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2622 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002623 }
2624 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2625
2626 // Perform FP_ROUND: this is probably overly pessimistic.
2627 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002628 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2629 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002630 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002631 case ISD::FDIV:
2632 case ISD::FREM:
2633 // These operators require that their input be fp extended.
2634 Tmp1 = PromoteOp(Node->getOperand(0));
2635 Tmp2 = PromoteOp(Node->getOperand(1));
2636 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2637
2638 // Perform FP_ROUND: this is probably overly pessimistic.
2639 if (NoExcessFPPrecision)
2640 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2641 DAG.getValueType(VT));
2642 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002643
2644 case ISD::UDIV:
2645 case ISD::UREM:
2646 // These operators require that their input be zero extended.
2647 Tmp1 = PromoteOp(Node->getOperand(0));
2648 Tmp2 = PromoteOp(Node->getOperand(1));
2649 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002650 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2651 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002652 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2653 break;
2654
2655 case ISD::SHL:
2656 Tmp1 = PromoteOp(Node->getOperand(0));
2657 Tmp2 = LegalizeOp(Node->getOperand(1));
2658 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2659 break;
2660 case ISD::SRA:
2661 // The input value must be properly sign extended.
2662 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002663 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2664 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002665 Tmp2 = LegalizeOp(Node->getOperand(1));
2666 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2667 break;
2668 case ISD::SRL:
2669 // The input value must be properly zero extended.
2670 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002671 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002672 Tmp2 = LegalizeOp(Node->getOperand(1));
2673 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2674 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002675 case ISD::LOAD:
2676 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2677 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002678 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2679 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002680 // Remember that we legalized the chain.
2681 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2682 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002683 case ISD::SEXTLOAD:
2684 case ISD::ZEXTLOAD:
2685 case ISD::EXTLOAD:
2686 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2687 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002688 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2689 Node->getOperand(2),
2690 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002691 // Remember that we legalized the chain.
2692 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2693 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002694 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002695 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2696 case Expand: assert(0 && "It's impossible to expand bools");
2697 case Legal:
2698 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2699 break;
2700 case Promote:
2701 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2702 break;
2703 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002704 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2705 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2706 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2707 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002708 case ISD::SELECT_CC:
2709 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2710 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2711 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2712 Node->getOperand(1), Tmp2, Tmp3,
2713 Node->getOperand(4));
2714 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002715 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002716 case ISD::CALL: {
2717 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2718 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2719
Chris Lattner3d95c142005-01-19 20:24:35 +00002720 std::vector<SDOperand> Ops;
2721 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2722 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2723
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002724 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2725 "Can only promote single result calls");
2726 std::vector<MVT::ValueType> RetTyVTs;
2727 RetTyVTs.reserve(2);
2728 RetTyVTs.push_back(NVT);
2729 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002730 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2731 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002732 Result = SDOperand(NC, 0);
2733
2734 // Insert the new chain mapping.
2735 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2736 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002737 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002738 case ISD::CTPOP:
2739 case ISD::CTTZ:
2740 case ISD::CTLZ:
2741 Tmp1 = Node->getOperand(0);
2742 //Zero extend the argument
2743 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2744 // Perform the larger operation, then subtract if needed.
2745 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2746 switch(Node->getOpcode())
2747 {
2748 case ISD::CTPOP:
2749 Result = Tmp1;
2750 break;
2751 case ISD::CTTZ:
2752 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002753 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002754 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002755 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002756 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2757 break;
2758 case ISD::CTLZ:
2759 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002760 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2761 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002762 getSizeInBits(VT), NVT));
2763 break;
2764 }
2765 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002766 }
2767
2768 assert(Result.Val && "Didn't set a result!");
2769 AddPromotedOperand(Op, Result);
2770 return Result;
2771}
Chris Lattnerdc750592005-01-07 07:47:09 +00002772
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002773/// ExpandAddSub - Find a clever way to expand this add operation into
2774/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002775void SelectionDAGLegalize::
2776ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2777 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002778 // Expand the subcomponents.
2779 SDOperand LHSL, LHSH, RHSL, RHSH;
2780 ExpandOp(LHS, LHSL, LHSH);
2781 ExpandOp(RHS, RHSL, RHSH);
2782
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002783 std::vector<SDOperand> Ops;
2784 Ops.push_back(LHSL);
2785 Ops.push_back(LHSH);
2786 Ops.push_back(RHSL);
2787 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002788 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2789 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002790 Hi = Lo.getValue(1);
2791}
2792
Chris Lattner4157c412005-04-02 04:00:59 +00002793void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2794 SDOperand Op, SDOperand Amt,
2795 SDOperand &Lo, SDOperand &Hi) {
2796 // Expand the subcomponents.
2797 SDOperand LHSL, LHSH;
2798 ExpandOp(Op, LHSL, LHSH);
2799
2800 std::vector<SDOperand> Ops;
2801 Ops.push_back(LHSL);
2802 Ops.push_back(LHSH);
2803 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002804 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002805 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002806 Hi = Lo.getValue(1);
2807}
2808
2809
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002810/// ExpandShift - Try to find a clever way to expand this shift operation out to
2811/// smaller elements. If we can't find a way that is more efficient than a
2812/// libcall on this target, return false. Otherwise, return true with the
2813/// low-parts expanded into Lo and Hi.
2814bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2815 SDOperand &Lo, SDOperand &Hi) {
2816 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2817 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002818
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002819 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002820 SDOperand ShAmt = LegalizeOp(Amt);
2821 MVT::ValueType ShTy = ShAmt.getValueType();
2822 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2823 unsigned NVTBits = MVT::getSizeInBits(NVT);
2824
2825 // Handle the case when Amt is an immediate. Other cases are currently broken
2826 // and are disabled.
2827 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2828 unsigned Cst = CN->getValue();
2829 // Expand the incoming operand to be shifted, so that we have its parts
2830 SDOperand InL, InH;
2831 ExpandOp(Op, InL, InH);
2832 switch(Opc) {
2833 case ISD::SHL:
2834 if (Cst > VTBits) {
2835 Lo = DAG.getConstant(0, NVT);
2836 Hi = DAG.getConstant(0, NVT);
2837 } else if (Cst > NVTBits) {
2838 Lo = DAG.getConstant(0, NVT);
2839 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002840 } else if (Cst == NVTBits) {
2841 Lo = DAG.getConstant(0, NVT);
2842 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002843 } else {
2844 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2845 Hi = DAG.getNode(ISD::OR, NVT,
2846 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2847 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2848 }
2849 return true;
2850 case ISD::SRL:
2851 if (Cst > VTBits) {
2852 Lo = DAG.getConstant(0, NVT);
2853 Hi = DAG.getConstant(0, NVT);
2854 } else if (Cst > NVTBits) {
2855 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2856 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002857 } else if (Cst == NVTBits) {
2858 Lo = InH;
2859 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002860 } else {
2861 Lo = DAG.getNode(ISD::OR, NVT,
2862 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2863 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2864 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2865 }
2866 return true;
2867 case ISD::SRA:
2868 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002869 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002870 DAG.getConstant(NVTBits-1, ShTy));
2871 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002872 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002873 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002874 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002875 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002876 } else if (Cst == NVTBits) {
2877 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002878 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002879 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002880 } else {
2881 Lo = DAG.getNode(ISD::OR, NVT,
2882 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2883 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2884 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2885 }
2886 return true;
2887 }
2888 }
2889 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2890 // so disable it for now. Currently targets are handling this via SHL_PARTS
2891 // and friends.
2892 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002893
2894 // If we have an efficient select operation (or if the selects will all fold
2895 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002896 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002897 return false;
2898
2899 SDOperand InL, InH;
2900 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002901 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2902 DAG.getConstant(NVTBits, ShTy), ShAmt);
2903
Chris Lattner4d25c042005-01-20 20:29:23 +00002904 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002905 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2906 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002907
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002908 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2909 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2910 DAG.getConstant(NVTBits-1, ShTy));
2911 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2912 DAG.getConstant(NVTBits-1, ShTy));
2913 }
2914
2915 if (Opc == ISD::SHL) {
2916 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2917 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2918 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002919 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002920
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002921 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2922 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2923 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002924 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002925 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2926 DAG.getConstant(32, ShTy),
2927 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002928 DAG.getConstant(0, NVT),
2929 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002930 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002931 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002932 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002933 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002934
2935 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002936 if (Opc == ISD::SRA)
2937 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2938 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002939 else
2940 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002941 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002942 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002943 }
2944 return true;
2945}
Chris Lattneraac464e2005-01-21 06:05:23 +00002946
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002947/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2948/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002949/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002950static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002951 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002952
Chris Lattner2dce7032005-05-12 23:24:06 +00002953 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002954 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002955 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002956 Found = Node;
2957 return;
2958 }
2959
2960 // Otherwise, scan the operands of Node to see if any of them is a call.
2961 assert(Node->getNumOperands() != 0 &&
2962 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002963 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002964 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002965
2966 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002967 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002968 Found);
2969}
2970
2971
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002972/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2973/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002974/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002975static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2976 std::set<SDNode*> &Visited) {
2977 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2978 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002979
Chris Lattner2dce7032005-05-12 23:24:06 +00002980 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002981 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002982 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002983 Found = Node;
2984 return;
2985 }
2986
2987 // Otherwise, scan the operands of Node to see if any of them is a call.
2988 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2989 if (UI == E) return;
2990 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002991 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002992
2993 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002994 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002995}
2996
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002997/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002998/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002999static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00003000 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00003001 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00003002 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003003 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00003004
Chris Lattner4add7e32005-01-23 04:42:50 +00003005 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00003006 if (TheChain.getValueType() != MVT::Other)
3007 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00003008 if (TheChain.getValueType() != MVT::Other)
3009 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00003010
3011 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00003012 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00003013
Chris Lattner4add7e32005-01-23 04:42:50 +00003014 // Make sure to only follow users of our token chain.
3015 SDNode *User = *UI;
3016 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3017 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00003018 if (SDNode *Result = FindCallSeqEnd(User))
3019 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00003020 }
Chris Lattnercabdc342005-08-05 16:23:57 +00003021 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00003022}
3023
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003024/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003025/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003026static SDNode *FindCallSeqStart(SDNode *Node) {
3027 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00003028 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003029
3030 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3031 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003032 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003033}
3034
3035
Chris Lattner4add7e32005-01-23 04:42:50 +00003036/// FindInputOutputChains - If we are replacing an operation with a call we need
3037/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00003038/// properly serialize the calls in the block. The returned operand is the
3039/// input chain value for the new call (e.g. the entry node or the previous
3040/// call), and OutChain is set to be the chain node to update to point to the
3041/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00003042static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3043 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003044 SDNode *LatestCallSeqStart = Entry.Val;
3045 SDNode *LatestCallSeqEnd = 0;
3046 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
3047 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00003048
Chris Lattner2dce7032005-05-12 23:24:06 +00003049 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00003050 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00003051 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3052 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00003053 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003054 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00003055 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3056 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003057 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00003058 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003059 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00003060
Chris Lattner06bbeb62005-05-11 19:02:11 +00003061 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003062 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003063 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00003064 std::set<SDNode*> Visited;
3065 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003066
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003067 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003068 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003069 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00003070
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003071 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00003072}
3073
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003074/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00003075void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3076 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00003077 // Nothing to splice it into?
3078 if (OutChain == 0) return;
3079
3080 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3081 //OutChain->dump();
3082
3083 // Form a token factor node merging the old inval and the new inval.
3084 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3085 OutChain->getOperand(0));
3086 // Change the node to refer to the new token.
3087 OutChain->setAdjCallChain(InToken);
3088}
Chris Lattner4add7e32005-01-23 04:42:50 +00003089
3090
Chris Lattneraac464e2005-01-21 06:05:23 +00003091// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3092// does not fit into a register, return the lo part and set the hi part to the
3093// by-reg argument. If it does fit into a single register, return the result
3094// and leave the Hi part unset.
3095SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3096 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003097 SDNode *OutChain;
3098 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3099 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00003100 if (InChain.Val == 0)
3101 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00003102
Chris Lattneraac464e2005-01-21 06:05:23 +00003103 TargetLowering::ArgListTy Args;
3104 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3105 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3106 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3107 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3108 }
3109 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003110
Chris Lattner06bbeb62005-05-11 19:02:11 +00003111 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003112 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003113 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003114 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3115 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003116
Chris Lattner63022662005-09-02 20:26:58 +00003117 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003118 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003119 default: assert(0 && "Unknown thing");
3120 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00003121 Result = CallInfo.first;
3122 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003123 case Promote:
3124 assert(0 && "Cannot promote this yet!");
3125 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003126 ExpandOp(CallInfo.first, Result, Hi);
3127 CallInfo.second = LegalizeOp(CallInfo.second);
3128 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003129 }
Chris Lattner63022662005-09-02 20:26:58 +00003130
3131 SpliceCallInto(CallInfo.second, OutChain);
3132 NeedsAnotherIteration = true;
3133 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003134}
3135
Chris Lattner4add7e32005-01-23 04:42:50 +00003136
Chris Lattneraac464e2005-01-21 06:05:23 +00003137/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3138/// destination type is legal.
3139SDOperand SelectionDAGLegalize::
3140ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003141 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003142 assert(getTypeAction(Source.getValueType()) == Expand &&
3143 "This is not an expansion!");
3144 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3145
Chris Lattner06bbeb62005-05-11 19:02:11 +00003146 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003147 assert(Source.getValueType() == MVT::i64 &&
3148 "This only works for 64-bit -> FP");
3149 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3150 // incoming integer is set. To handle this, we dynamically test to see if
3151 // it is set, and, if so, add a fudge factor.
3152 SDOperand Lo, Hi;
3153 ExpandOp(Source, Lo, Hi);
3154
Chris Lattner2a4f7312005-05-13 04:45:13 +00003155 // If this is unsigned, and not supported, first perform the conversion to
3156 // signed, then adjust the result if the sign bit is set.
3157 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3158 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3159
Chris Lattnerd47675e2005-08-09 20:20:18 +00003160 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3161 DAG.getConstant(0, Hi.getValueType()),
3162 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003163 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3164 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3165 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003166 uint64_t FF = 0x5f800000ULL;
3167 if (TLI.isLittleEndian()) FF <<= 32;
3168 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003169
Chris Lattnerc30405e2005-08-26 17:15:30 +00003170 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003171 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3172 SDOperand FudgeInReg;
3173 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003174 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3175 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003176 else {
3177 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003178 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3179 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003180 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003181 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003182 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003183
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003184 // Check to see if the target has a custom way to lower this. If so, use it.
3185 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3186 default: assert(0 && "This action not implemented for this operation!");
3187 case TargetLowering::Legal:
3188 case TargetLowering::Expand:
3189 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003190 case TargetLowering::Custom: {
3191 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3192 Source), DAG);
3193 if (NV.Val)
3194 return LegalizeOp(NV);
3195 break; // The target decided this was legal after all
3196 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003197 }
3198
Chris Lattner153587e2005-05-12 07:00:44 +00003199 // Expand the source, then glue it back together for the call. We must expand
3200 // the source in case it is shared (this pass of legalize must traverse it).
3201 SDOperand SrcLo, SrcHi;
3202 ExpandOp(Source, SrcLo, SrcHi);
3203 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3204
Chris Lattner06bbeb62005-05-11 19:02:11 +00003205 SDNode *OutChain = 0;
3206 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3207 DAG.getEntryNode());
3208 const char *FnName = 0;
3209 if (DestTy == MVT::f32)
3210 FnName = "__floatdisf";
3211 else {
3212 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3213 FnName = "__floatdidf";
3214 }
3215
Chris Lattneraac464e2005-01-21 06:05:23 +00003216 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3217
3218 TargetLowering::ArgListTy Args;
3219 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003220
Chris Lattneraac464e2005-01-21 06:05:23 +00003221 Args.push_back(std::make_pair(Source, ArgTy));
3222
3223 // We don't care about token chains for libcalls. We just use the entry
3224 // node as our input and ignore the output chain. This allows us to place
3225 // calls wherever we need them to satisfy data dependences.
3226 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003227
3228 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003229 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3230 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003231
Chris Lattnera5bf1032005-05-12 04:49:08 +00003232 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003233 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003234}
Misha Brukman835702a2005-04-21 22:36:52 +00003235
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003236
3237
Chris Lattnerdc750592005-01-07 07:47:09 +00003238/// ExpandOp - Expand the specified SDOperand into its two component pieces
3239/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3240/// LegalizeNodes map is filled in for any results that are not expanded, the
3241/// ExpandedNodes map is filled in for any results that are expanded, and the
3242/// Lo/Hi values are returned.
3243void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3244 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003245 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003246 SDNode *Node = Op.Val;
3247 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003248 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3249 "Cannot expand FP values!");
3250 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003251 "Cannot expand to FP value or to larger int value!");
3252
Chris Lattner1a570f12005-09-02 20:32:45 +00003253 // See if we already expanded it.
3254 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3255 = ExpandedNodes.find(Op);
3256 if (I != ExpandedNodes.end()) {
3257 Lo = I->second.first;
3258 Hi = I->second.second;
3259 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003260 }
3261
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003262 // Expanding to multiple registers needs to perform an optimization step, and
3263 // is not careful to avoid operations the target does not support. Make sure
3264 // that all generated operations are legalized in the next iteration.
3265 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003266
Chris Lattnerdc750592005-01-07 07:47:09 +00003267 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003268 case ISD::CopyFromReg:
3269 assert(0 && "CopyFromReg must be legal!");
3270 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003271 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3272 assert(0 && "Do not know how to expand this operator!");
3273 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003274 case ISD::UNDEF:
3275 Lo = DAG.getNode(ISD::UNDEF, NVT);
3276 Hi = DAG.getNode(ISD::UNDEF, NVT);
3277 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003278 case ISD::Constant: {
3279 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3280 Lo = DAG.getConstant(Cst, NVT);
3281 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3282 break;
3283 }
Nate Begemanae89d862005-12-07 19:48:11 +00003284 case ISD::ConstantVec: {
3285 unsigned NumElements = Node->getNumOperands();
3286 // If we only have two elements left in the constant vector, just break it
3287 // apart into the two scalar constants it contains. Otherwise, bisect the
3288 // ConstantVec, and return each half as a new ConstantVec.
3289 // FIXME: this is hard coded as big endian, it may have to change to support
3290 // SSE and Alpha MVI
3291 if (NumElements == 2) {
3292 Hi = Node->getOperand(0);
3293 Lo = Node->getOperand(1);
3294 } else {
3295 NumElements /= 2;
3296 std::vector<SDOperand> LoOps, HiOps;
3297 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3298 HiOps.push_back(Node->getOperand(I));
3299 LoOps.push_back(Node->getOperand(I+NumElements));
3300 }
3301 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3302 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3303 }
3304 break;
3305 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003306
Chris Lattner32e08b72005-03-28 22:03:13 +00003307 case ISD::BUILD_PAIR:
3308 // Legalize both operands. FIXME: in the future we should handle the case
3309 // where the two elements are not legal.
3310 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3311 Lo = LegalizeOp(Node->getOperand(0));
3312 Hi = LegalizeOp(Node->getOperand(1));
3313 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003314
3315 case ISD::SIGN_EXTEND_INREG:
3316 ExpandOp(Node->getOperand(0), Lo, Hi);
3317 // Sign extend the lo-part.
3318 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3319 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3320 TLI.getShiftAmountTy()));
3321 // sext_inreg the low part if needed.
3322 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3323 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003324
Chris Lattner55e9cde2005-05-11 04:51:16 +00003325 case ISD::CTPOP:
3326 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003327 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3328 DAG.getNode(ISD::CTPOP, NVT, Lo),
3329 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003330 Hi = DAG.getConstant(0, NVT);
3331 break;
3332
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003333 case ISD::CTLZ: {
3334 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003335 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003336 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3337 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003338 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3339 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003340 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3341 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3342
3343 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3344 Hi = DAG.getConstant(0, NVT);
3345 break;
3346 }
3347
3348 case ISD::CTTZ: {
3349 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003350 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003351 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3352 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003353 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3354 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003355 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3356 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3357
3358 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3359 Hi = DAG.getConstant(0, NVT);
3360 break;
3361 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003362
Chris Lattnerdc750592005-01-07 07:47:09 +00003363 case ISD::LOAD: {
3364 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3365 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003366 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003367
3368 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003369 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003370 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3371 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003372 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003373 //are from the same instruction?
3374 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003375
3376 // Build a factor node to remember that this load is independent of the
3377 // other one.
3378 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3379 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003380
Chris Lattnerdc750592005-01-07 07:47:09 +00003381 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003382 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003383 if (!TLI.isLittleEndian())
3384 std::swap(Lo, Hi);
3385 break;
3386 }
Nate Begemand37c1312005-11-22 18:16:00 +00003387 case ISD::VLOAD: {
3388 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3389 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3390 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3391 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3392
3393 // If we only have two elements, turn into a pair of scalar loads.
3394 // FIXME: handle case where a vector of two elements is fine, such as
3395 // 2 x double on SSE2.
3396 if (NumElements == 2) {
3397 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3398 // Increment the pointer to the other half.
3399 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3400 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3401 getIntPtrConstant(IncrementSize));
3402 //Is this safe? declaring that the two parts of the split load
3403 //are from the same instruction?
3404 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3405 } else {
3406 NumElements /= 2; // Split the vector in half
3407 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3408 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3409 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3410 getIntPtrConstant(IncrementSize));
3411 //Is this safe? declaring that the two parts of the split load
3412 //are from the same instruction?
3413 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3414 }
3415
3416 // Build a factor node to remember that this load is independent of the
3417 // other one.
3418 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3419 Hi.getValue(1));
3420
3421 // Remember that we legalized the chain.
3422 AddLegalizedOperand(Op.getValue(1), TF);
3423 if (!TLI.isLittleEndian())
3424 std::swap(Lo, Hi);
3425 break;
3426 }
3427 case ISD::VADD:
3428 case ISD::VSUB:
3429 case ISD::VMUL: {
3430 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3431 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3432 SDOperand LL, LH, RL, RH;
3433
3434 ExpandOp(Node->getOperand(0), LL, LH);
3435 ExpandOp(Node->getOperand(1), RL, RH);
3436
3437 // If we only have two elements, turn into a pair of scalar loads.
3438 // FIXME: handle case where a vector of two elements is fine, such as
3439 // 2 x double on SSE2.
3440 if (NumElements == 2) {
3441 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3442 Lo = DAG.getNode(Opc, EVT, LL, RL);
3443 Hi = DAG.getNode(Opc, EVT, LH, RH);
3444 } else {
3445 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3446 LL.getOperand(3));
3447 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3448 LH.getOperand(3));
3449 }
3450 break;
3451 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003452 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003453 case ISD::CALL: {
3454 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3455 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3456
Chris Lattner3d95c142005-01-19 20:24:35 +00003457 bool Changed = false;
3458 std::vector<SDOperand> Ops;
3459 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3460 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3461 Changed |= Ops.back() != Node->getOperand(i);
3462 }
3463
Chris Lattnerdc750592005-01-07 07:47:09 +00003464 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3465 "Can only expand a call once so far, not i64 -> i16!");
3466
3467 std::vector<MVT::ValueType> RetTyVTs;
3468 RetTyVTs.reserve(3);
3469 RetTyVTs.push_back(NVT);
3470 RetTyVTs.push_back(NVT);
3471 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003472 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3473 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003474 Lo = SDOperand(NC, 0);
3475 Hi = SDOperand(NC, 1);
3476
3477 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003478 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003479 break;
3480 }
3481 case ISD::AND:
3482 case ISD::OR:
3483 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3484 SDOperand LL, LH, RL, RH;
3485 ExpandOp(Node->getOperand(0), LL, LH);
3486 ExpandOp(Node->getOperand(1), RL, RH);
3487 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3488 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3489 break;
3490 }
3491 case ISD::SELECT: {
3492 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003493
3494 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3495 case Expand: assert(0 && "It's impossible to expand bools");
3496 case Legal:
3497 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3498 break;
3499 case Promote:
3500 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3501 break;
3502 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003503 ExpandOp(Node->getOperand(1), LL, LH);
3504 ExpandOp(Node->getOperand(2), RL, RH);
3505 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3506 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3507 break;
3508 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003509 case ISD::SELECT_CC: {
3510 SDOperand TL, TH, FL, FH;
3511 ExpandOp(Node->getOperand(2), TL, TH);
3512 ExpandOp(Node->getOperand(3), FL, FH);
3513 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3514 Node->getOperand(1), TL, FL, Node->getOperand(4));
3515 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3516 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003517 Lo = LegalizeOp(Lo);
3518 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003519 break;
3520 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003521 case ISD::SEXTLOAD: {
3522 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3523 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3524 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3525
3526 if (EVT == NVT)
3527 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3528 else
3529 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3530 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003531
3532 // Remember that we legalized the chain.
3533 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3534
Nate Begemanc3a89c52005-10-13 17:15:37 +00003535 // The high part is obtained by SRA'ing all but one of the bits of the lo
3536 // part.
3537 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3538 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3539 TLI.getShiftAmountTy()));
3540 Lo = LegalizeOp(Lo);
3541 Hi = LegalizeOp(Hi);
3542 break;
3543 }
3544 case ISD::ZEXTLOAD: {
3545 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3546 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3547 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3548
3549 if (EVT == NVT)
3550 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3551 else
3552 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3553 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003554
3555 // Remember that we legalized the chain.
3556 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3557
Nate Begemanc3a89c52005-10-13 17:15:37 +00003558 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003559 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003560 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003561 break;
3562 }
3563 case ISD::EXTLOAD: {
3564 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3565 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3566 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3567
3568 if (EVT == NVT)
3569 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3570 else
3571 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3572 EVT);
3573
3574 // Remember that we legalized the chain.
3575 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3576
3577 // The high part is undefined.
3578 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3579 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003580 break;
3581 }
Chris Lattner7753f172005-09-02 00:18:10 +00003582 case ISD::ANY_EXTEND: {
3583 SDOperand In;
3584 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3585 case Expand: assert(0 && "expand-expand not implemented yet!");
3586 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3587 case Promote:
3588 In = PromoteOp(Node->getOperand(0));
3589 break;
3590 }
3591
3592 // The low part is any extension of the input (which degenerates to a copy).
3593 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3594 // The high part is undefined.
3595 Hi = DAG.getNode(ISD::UNDEF, NVT);
3596 break;
3597 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003598 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003599 SDOperand In;
3600 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3601 case Expand: assert(0 && "expand-expand not implemented yet!");
3602 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3603 case Promote:
3604 In = PromoteOp(Node->getOperand(0));
3605 // Emit the appropriate sign_extend_inreg to get the value we want.
3606 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003607 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003608 break;
3609 }
3610
Chris Lattnerdc750592005-01-07 07:47:09 +00003611 // The low part is just a sign extension of the input (which degenerates to
3612 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003613 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003614
Chris Lattnerdc750592005-01-07 07:47:09 +00003615 // The high part is obtained by SRA'ing all but one of the bits of the lo
3616 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003617 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003618 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3619 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003620 break;
3621 }
Chris Lattner47844892005-04-03 23:41:52 +00003622 case ISD::ZERO_EXTEND: {
3623 SDOperand In;
3624 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3625 case Expand: assert(0 && "expand-expand not implemented yet!");
3626 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3627 case Promote:
3628 In = PromoteOp(Node->getOperand(0));
3629 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003630 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003631 break;
3632 }
3633
Chris Lattnerdc750592005-01-07 07:47:09 +00003634 // The low part is just a zero extension of the input (which degenerates to
3635 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003636 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003637
Chris Lattnerdc750592005-01-07 07:47:09 +00003638 // The high part is just a zero.
3639 Hi = DAG.getConstant(0, NVT);
3640 break;
Chris Lattner47844892005-04-03 23:41:52 +00003641 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003642
Chris Lattner44c28c22005-11-20 22:56:56 +00003643 case ISD::READCYCLECOUNTER: {
3644 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3645 TargetLowering::Custom &&
3646 "Must custom expand ReadCycleCounter");
3647 SDOperand T = TLI.LowerOperation(Op, DAG);
3648 assert(T.Val && "Node must be custom expanded!");
3649 Lo = LegalizeOp(T.getValue(0));
3650 Hi = LegalizeOp(T.getValue(1));
3651 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3652 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003653 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003654 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003655
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003656 // These operators cannot be expanded directly, emit them as calls to
3657 // library functions.
3658 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003659 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003660 SDOperand Op;
3661 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3662 case Expand: assert(0 && "cannot expand FP!");
3663 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3664 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3665 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003666
Chris Lattner941d84a2005-07-30 01:40:57 +00003667 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3668
Chris Lattnerfe68d752005-07-29 00:33:32 +00003669 // Now that the custom expander is done, expand the result, which is still
3670 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003671 if (Op.Val) {
3672 ExpandOp(Op, Lo, Hi);
3673 break;
3674 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003675 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003676
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003677 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003678 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003679 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003680 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003681 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003682
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003683 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003684 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3685 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3686 LegalizeOp(Node->getOperand(0)));
3687 // Now that the custom expander is done, expand the result, which is still
3688 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003689 Op = TLI.LowerOperation(Op, DAG);
3690 if (Op.Val) {
3691 ExpandOp(Op, Lo, Hi);
3692 break;
3693 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003694 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003695
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003696 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003697 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003698 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003699 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003700 break;
3701
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003702 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003703 // If the target wants custom lowering, do so.
3704 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3705 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3706 LegalizeOp(Node->getOperand(1)));
3707 Op = TLI.LowerOperation(Op, DAG);
3708 if (Op.Val) {
3709 // Now that the custom expander is done, expand the result, which is
3710 // still VT.
3711 ExpandOp(Op, Lo, Hi);
3712 break;
3713 }
3714 }
3715
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003716 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003717 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003718 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003719
3720 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003721 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003722 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3723 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003724 break;
3725 }
3726
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003727 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003728 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003729 break;
3730
3731 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003732 // If the target wants custom lowering, do so.
3733 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3734 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3735 LegalizeOp(Node->getOperand(1)));
3736 Op = TLI.LowerOperation(Op, DAG);
3737 if (Op.Val) {
3738 // Now that the custom expander is done, expand the result, which is
3739 // still VT.
3740 ExpandOp(Op, Lo, Hi);
3741 break;
3742 }
3743 }
3744
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003745 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003746 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003747 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003748
3749 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003750 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003751 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3752 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003753 break;
3754 }
3755
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003756 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003757 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003758 break;
3759 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003760 // If the target wants custom lowering, do so.
3761 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3762 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3763 LegalizeOp(Node->getOperand(1)));
3764 Op = TLI.LowerOperation(Op, DAG);
3765 if (Op.Val) {
3766 // Now that the custom expander is done, expand the result, which is
3767 // still VT.
3768 ExpandOp(Op, Lo, Hi);
3769 break;
3770 }
3771 }
3772
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003773 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003774 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003775 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003776
3777 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003778 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003779 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3780 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003781 break;
3782 }
3783
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003784 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003785 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003786 break;
3787
Misha Brukman835702a2005-04-21 22:36:52 +00003788 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003789 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3790 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003791 break;
3792 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003793 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3794 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003795 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003796 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003797 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003798 SDOperand LL, LH, RL, RH;
3799 ExpandOp(Node->getOperand(0), LL, LH);
3800 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003801 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3802 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3803 // extended the sign bit of the low half through the upper half, and if so
3804 // emit a MULHS instead of the alternate sequence that is valid for any
3805 // i64 x i64 multiply.
3806 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3807 // is RH an extension of the sign bit of RL?
3808 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3809 RH.getOperand(1).getOpcode() == ISD::Constant &&
3810 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3811 // is LH an extension of the sign bit of LL?
3812 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3813 LH.getOperand(1).getOpcode() == ISD::Constant &&
3814 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3815 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3816 } else {
3817 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3818 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3819 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3820 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3821 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3822 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003823 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3824 } else {
3825 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3826 }
3827 break;
3828 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003829 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3830 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3831 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3832 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003833 }
3834
3835 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003836 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3837 std::make_pair(Lo, Hi))).second;
3838 assert(isNew && "Value already expanded?!?");
Chris Lattner2af3ee42005-12-20 00:53:54 +00003839
Chris Lattnerac12f682005-12-21 18:02:52 +00003840 // Make sure the resultant values have been legalized themselves, unless this
3841 // is a type that requires multi-step expansion.
3842 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
3843 Lo = LegalizeOp(Lo);
3844 Hi = LegalizeOp(Hi);
3845 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003846}
3847
3848
3849// SelectionDAG::Legalize - This is the entry point for the file.
3850//
Chris Lattner4add7e32005-01-23 04:42:50 +00003851void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003852 /// run - This is the main entry point to this class.
3853 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003854 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003855}
3856