blob: 6d6aab6946c3058813890c3ae6b1e3528fd02cd4 [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: {
Jim Laskey9e296be2005-12-21 20:51:37 +0000625 if (TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other)) {
626 MachineDebugInfo &DebugInfo = DAG.getMachineFunction().getDebugInfo();
627 std::vector<SDOperand> Ops;
628 Ops.push_back(Tmp1); // chain
629 Ops.push_back(Node->getOperand(1)); // line #
630 Ops.push_back(Node->getOperand(2)); // col #
631 const std::string &fname =
632 cast<StringSDNode>(Node->getOperand(3))->getValue();
633 const std::string &dirname =
634 cast<StringSDNode>(Node->getOperand(4))->getValue();
635 unsigned id = DebugInfo.RecordSource(fname, dirname);
636 Ops.push_back(DAG.getConstant(id, MVT::i32)); // source file id
637 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
638 } else {
639 Result = Tmp1; // chain
640 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000641 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner435b4022005-11-29 06:21:05 +0000642 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000643 }
Chris Lattner435b4022005-11-29 06:21:05 +0000644 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000645 if (Tmp1 != Node->getOperand(0) ||
646 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000647 std::vector<SDOperand> Ops;
648 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000649 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
650 Ops.push_back(Node->getOperand(1)); // line # must be legal.
651 Ops.push_back(Node->getOperand(2)); // col # must be legal.
652 } else {
653 // Otherwise promote them.
654 Ops.push_back(PromoteOp(Node->getOperand(1)));
655 Ops.push_back(PromoteOp(Node->getOperand(2)));
656 }
Chris Lattner435b4022005-11-29 06:21:05 +0000657 Ops.push_back(Node->getOperand(3)); // filename must be legal.
658 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
659 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
660 }
661 break;
662 }
663 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000664
665 case ISD::DEBUG_LOC:
666 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
667 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
668 case TargetLowering::Promote:
669 case TargetLowering::Expand:
670 default: assert(0 && "This action is not supported yet!");
671 case TargetLowering::Legal:
672 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
673 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
674 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
675 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
676
677 if (Tmp1 != Node->getOperand(0) ||
678 Tmp2 != Node->getOperand(1) ||
679 Tmp3 != Node->getOperand(2) ||
680 Tmp4 != Node->getOperand(3)) {
681 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
682 }
683 break;
684 }
685 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000686
Chris Lattnerdc750592005-01-07 07:47:09 +0000687 case ISD::Constant:
688 // We know we don't need to expand constants here, constants only have one
689 // value and we check that it is fine above.
690
691 // FIXME: Maybe we should handle things like targets that don't support full
692 // 32-bit immediates?
693 break;
694 case ISD::ConstantFP: {
695 // Spill FP immediates to the constant pool if the target cannot directly
696 // codegen them. Targets often have some immediate values that can be
697 // efficiently generated into an FP register without a load. We explicitly
698 // leave these constants as ConstantFP nodes for the target to deal with.
699
700 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
701
702 // Check to see if this FP immediate is already legal.
703 bool isLegal = false;
704 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
705 E = TLI.legal_fpimm_end(); I != E; ++I)
706 if (CFP->isExactlyValue(*I)) {
707 isLegal = true;
708 break;
709 }
710
711 if (!isLegal) {
712 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000713 bool Extend = false;
714
715 // If a FP immediate is precise when represented as a float, we put it
716 // into the constant pool as a float, even if it's is statically typed
717 // as a double.
718 MVT::ValueType VT = CFP->getValueType(0);
719 bool isDouble = VT == MVT::f64;
720 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
721 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000722 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
723 // Only do this if the target has a native EXTLOAD instruction from
724 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000725 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000726 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
727 VT = MVT::f32;
728 Extend = true;
729 }
Misha Brukman835702a2005-04-21 22:36:52 +0000730
Nate Begeman4e56db62005-12-10 02:36:00 +0000731 SDOperand CPIdx =
732 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000733 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000734 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
735 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000736 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000737 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
738 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000739 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000740 }
741 break;
742 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000743 case ISD::ConstantVec: {
744 // We assume that vector constants are not legal, and will be immediately
745 // spilled to the constant pool.
746 //
747 // FIXME: revisit this when we have some kind of mechanism by which targets
748 // can decided legality of vector constants, of which there may be very
749 // many.
750 //
751 // Create a ConstantPacked, and put it in the constant pool.
752 std::vector<Constant*> CV;
753 MVT::ValueType VT = Node->getValueType(0);
754 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
755 SDOperand OpN = Node->getOperand(I);
756 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
757 if (MVT::isFloatingPoint(VT))
758 CV.push_back(ConstantFP::get(OpNTy,
759 cast<ConstantFPSDNode>(OpN)->getValue()));
760 else
761 CV.push_back(ConstantUInt::get(OpNTy,
762 cast<ConstantSDNode>(OpN)->getValue()));
763 }
764 Constant *CP = ConstantPacked::get(CV);
Nate Begeman956aef42005-12-13 03:03:23 +0000765 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000766 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
767 break;
768 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000769 case ISD::TokenFactor:
770 if (Node->getNumOperands() == 2) {
771 bool Changed = false;
772 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
773 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
774 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
775 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
776 } else {
777 std::vector<SDOperand> Ops;
778 bool Changed = false;
779 // Legalize the operands.
780 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
781 SDOperand Op = Node->getOperand(i);
782 Ops.push_back(LegalizeOp(Op));
783 Changed |= Ops[i] != Op;
784 }
785 if (Changed)
786 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000787 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000788 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000789
Chris Lattner2dce7032005-05-12 23:24:06 +0000790 case ISD::CALLSEQ_START:
791 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000792 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000793 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000794 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000795 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000796 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000797
Chris Lattner2dce7032005-05-12 23:24:06 +0000798 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000799 // nodes are treated specially and are mutated in place. This makes the dag
800 // legalization process more efficient and also makes libcall insertion
801 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000802 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000803 case ISD::DYNAMIC_STACKALLOC:
804 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
805 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
806 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
807 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000808 Tmp3 != Node->getOperand(2)) {
809 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
810 std::vector<SDOperand> Ops;
811 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
812 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
813 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000814 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000815
816 // Since this op produces two values, make sure to remember that we
817 // legalized both of them.
818 AddLegalizedOperand(SDOperand(Node, 0), Result);
819 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
820 return Result.getValue(Op.ResNo);
821
Chris Lattnerd0feb642005-05-13 18:43:43 +0000822 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000823 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000824 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
825 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000826
827 bool Changed = false;
828 std::vector<SDOperand> Ops;
829 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
830 Ops.push_back(LegalizeOp(Node->getOperand(i)));
831 Changed |= Ops.back() != Node->getOperand(i);
832 }
833
834 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000835 std::vector<MVT::ValueType> RetTyVTs;
836 RetTyVTs.reserve(Node->getNumValues());
837 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000838 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000839 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
840 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000841 } else {
842 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000843 }
Chris Lattner9242c502005-01-09 19:43:23 +0000844 // Since calls produce multiple values, make sure to remember that we
845 // legalized all of them.
846 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
847 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
848 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000849 }
Chris Lattner68a12142005-01-07 22:12:08 +0000850 case ISD::BR:
851 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
852 if (Tmp1 != Node->getOperand(0))
853 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
854 break;
855
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000856 case ISD::BRCOND:
857 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000858
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000859 switch (getTypeAction(Node->getOperand(1).getValueType())) {
860 case Expand: assert(0 && "It's impossible to expand bools");
861 case Legal:
862 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
863 break;
864 case Promote:
865 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
866 break;
867 }
Nate Begeman371e4952005-08-16 19:49:35 +0000868
869 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
870 default: assert(0 && "This action is not supported yet!");
871 case TargetLowering::Expand:
872 // Expand brcond's setcc into its constituent parts and create a BR_CC
873 // Node.
874 if (Tmp2.getOpcode() == ISD::SETCC) {
875 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
876 Tmp2.getOperand(0), Tmp2.getOperand(1),
877 Node->getOperand(2));
878 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000879 // Make sure the condition is either zero or one. It may have been
880 // promoted from something else.
881 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
882
Nate Begeman371e4952005-08-16 19:49:35 +0000883 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
884 DAG.getCondCode(ISD::SETNE), Tmp2,
885 DAG.getConstant(0, Tmp2.getValueType()),
886 Node->getOperand(2));
887 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000888 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +0000889 break;
Evan Cheng6fc31042005-12-19 23:12:38 +0000890 case TargetLowering::Custom: {
891 SDOperand Tmp =
892 TLI.LowerOperation(DAG.getNode(ISD::BRCOND, Node->getValueType(0),
893 Tmp1, Tmp2, Node->getOperand(2)), DAG);
894 if (Tmp.Val) {
895 Result = LegalizeOp(Tmp);
896 break;
897 }
898 // FALLTHROUGH if the target thinks it is legal.
899 }
Nate Begeman371e4952005-08-16 19:49:35 +0000900 case TargetLowering::Legal:
901 // Basic block destination (Op#2) is always legal.
902 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
903 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
904 Node->getOperand(2));
905 break;
906 }
907 break;
908 case ISD::BR_CC:
909 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000910 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000911 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
912 Node->getOperand(2), // LHS
913 Node->getOperand(3), // RHS
914 Node->getOperand(1)));
915 // If we get a SETCC back from legalizing the SETCC node we just
916 // created, then use its LHS, RHS, and CC directly in creating a new
917 // node. Otherwise, select between the true and false value based on
918 // comparing the result of the legalized with zero.
919 if (Tmp2.getOpcode() == ISD::SETCC) {
920 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
921 Tmp2.getOperand(0), Tmp2.getOperand(1),
922 Node->getOperand(4));
923 } else {
924 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
925 DAG.getCondCode(ISD::SETNE),
926 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
927 Node->getOperand(4));
928 }
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000929 break;
930 }
931
932 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
933 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
934
935 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
936 default: assert(0 && "Unexpected action for BR_CC!");
937 case TargetLowering::Custom: {
938 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
939 Tmp2, Tmp3, Node->getOperand(4));
940 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
941 if (Tmp4.Val) {
942 Result = LegalizeOp(Tmp4);
943 break;
944 }
945 } // FALLTHROUGH if the target doesn't want to lower this op after all.
946 case TargetLowering::Legal:
947 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
948 Tmp3 != Node->getOperand(3)) {
949 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
950 Tmp2, Tmp3, Node->getOperand(4));
951 }
952 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000953 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000954 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000955 case ISD::BRCONDTWOWAY:
956 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
957 switch (getTypeAction(Node->getOperand(1).getValueType())) {
958 case Expand: assert(0 && "It's impossible to expand bools");
959 case Legal:
960 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
961 break;
962 case Promote:
963 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
964 break;
965 }
966 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
967 // pair.
968 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
969 case TargetLowering::Promote:
970 default: assert(0 && "This action is not supported yet!");
971 case TargetLowering::Legal:
972 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
973 std::vector<SDOperand> Ops;
974 Ops.push_back(Tmp1);
975 Ops.push_back(Tmp2);
976 Ops.push_back(Node->getOperand(2));
977 Ops.push_back(Node->getOperand(3));
978 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
979 }
980 break;
981 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000982 // If BRTWOWAY_CC is legal for this target, then simply expand this node
983 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
984 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000985 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000986 if (Tmp2.getOpcode() == ISD::SETCC) {
987 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
988 Tmp2.getOperand(0), Tmp2.getOperand(1),
989 Node->getOperand(2), Node->getOperand(3));
990 } else {
991 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
992 DAG.getConstant(0, Tmp2.getValueType()),
993 Node->getOperand(2), Node->getOperand(3));
994 }
995 } else {
996 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000997 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000998 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
999 }
Chris Lattnerc06da622005-12-18 23:54:29 +00001000 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattnerfd986782005-04-09 03:30:19 +00001001 break;
1002 }
1003 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001004 case ISD::BRTWOWAY_CC:
1005 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001006 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +00001007 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1008 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1009 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1010 Tmp3 != Node->getOperand(3)) {
1011 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1012 Node->getOperand(4), Node->getOperand(5));
1013 }
1014 break;
1015 } else {
1016 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1017 Node->getOperand(2), // LHS
1018 Node->getOperand(3), // RHS
1019 Node->getOperand(1)));
1020 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1021 // pair.
1022 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1023 default: assert(0 && "This action is not supported yet!");
1024 case TargetLowering::Legal:
1025 // If we get a SETCC back from legalizing the SETCC node we just
1026 // created, then use its LHS, RHS, and CC directly in creating a new
1027 // node. Otherwise, select between the true and false value based on
1028 // comparing the result of the legalized with zero.
1029 if (Tmp2.getOpcode() == ISD::SETCC) {
1030 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1031 Tmp2.getOperand(0), Tmp2.getOperand(1),
1032 Node->getOperand(4), Node->getOperand(5));
1033 } else {
1034 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1035 DAG.getConstant(0, Tmp2.getValueType()),
1036 Node->getOperand(4), Node->getOperand(5));
1037 }
1038 break;
1039 case TargetLowering::Expand:
1040 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1041 Node->getOperand(4));
1042 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1043 break;
1044 }
Chris Lattner0fab4592005-12-21 19:40:42 +00001045 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +00001046 }
1047 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001048 case ISD::LOAD:
1049 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1050 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001051
Chris Lattnerdc750592005-01-07 07:47:09 +00001052 if (Tmp1 != Node->getOperand(0) ||
1053 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +00001054 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1055 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001056 else
1057 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +00001058
Chris Lattnerea4ca942005-01-07 22:28:47 +00001059 // Since loads produce two values, make sure to remember that we legalized
1060 // both of them.
1061 AddLegalizedOperand(SDOperand(Node, 0), Result);
1062 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1063 return Result.getValue(Op.ResNo);
Nate Begemanb2e089c2005-11-19 00:36:38 +00001064
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001065 case ISD::EXTLOAD:
1066 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001067 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001068 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1069 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001070
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001071 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001072 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001073 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001074 case TargetLowering::Promote:
1075 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001076 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1077 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001078 // Since loads produce two values, make sure to remember that we legalized
1079 // both of them.
1080 AddLegalizedOperand(SDOperand(Node, 0), Result);
1081 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1082 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +00001083
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001084 case TargetLowering::Legal:
1085 if (Tmp1 != Node->getOperand(0) ||
1086 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001087 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1088 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001089 else
1090 Result = SDOperand(Node, 0);
1091
1092 // Since loads produce two values, make sure to remember that we legalized
1093 // both of them.
1094 AddLegalizedOperand(SDOperand(Node, 0), Result);
1095 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1096 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001097 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001098 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001099 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1100 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001101 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc06da622005-12-18 23:54:29 +00001102 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001103 Load = LegalizeOp(Load);
1104 AddLegalizedOperand(SDOperand(Node, 0), Result);
1105 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001106 if (Op.ResNo)
1107 return Load.getValue(1);
1108 return Result;
1109 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001110 assert(Node->getOpcode() != ISD::EXTLOAD &&
1111 "EXTLOAD should always be supported!");
1112 // Turn the unsupported load into an EXTLOAD followed by an explicit
1113 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001114 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1115 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001116 SDOperand ValRes;
1117 if (Node->getOpcode() == ISD::SEXTLOAD)
1118 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001119 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001120 else
1121 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc06da622005-12-18 23:54:29 +00001122 Result = LegalizeOp(Result); // Relegalize new nodes.
1123 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001124 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1125 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001126 if (Op.ResNo)
1127 return Result.getValue(1);
1128 return ValRes;
1129 }
1130 assert(0 && "Unreachable");
1131 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001132 case ISD::EXTRACT_ELEMENT: {
1133 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1134 switch (getTypeAction(OpTy)) {
1135 default:
1136 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1137 break;
1138 case Legal:
1139 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1140 // 1 -> Hi
1141 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1142 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1143 TLI.getShiftAmountTy()));
1144 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1145 } else {
1146 // 0 -> Lo
1147 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1148 Node->getOperand(0));
1149 }
1150 Result = LegalizeOp(Result);
1151 break;
1152 case Expand:
1153 // Get both the low and high parts.
1154 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1155 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1156 Result = Tmp2; // 1 -> Hi
1157 else
1158 Result = Tmp1; // 0 -> Lo
1159 break;
1160 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001161 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001162 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001163
1164 case ISD::CopyToReg:
1165 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001166
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001167 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001168 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001169 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001170 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001171 if (Node->getNumValues() == 1) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001172 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1173 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1174 Node->getOperand(1), Tmp2);
1175 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001176 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1177 if (Node->getNumOperands() == 4)
1178 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001179 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001180 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001181 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1182 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1183 }
1184
1185 // Since this produces two values, make sure to remember that we legalized
1186 // both of them.
1187 AddLegalizedOperand(SDOperand(Node, 0), Result);
1188 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1189 return Result.getValue(Op.ResNo);
1190 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001191 break;
1192
1193 case ISD::RET:
1194 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1195 switch (Node->getNumOperands()) {
1196 case 2: // ret val
1197 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1198 case Legal:
1199 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001200 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001201 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1202 break;
1203 case Expand: {
1204 SDOperand Lo, Hi;
1205 ExpandOp(Node->getOperand(1), Lo, Hi);
1206 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001207 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001208 }
1209 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001210 Tmp2 = PromoteOp(Node->getOperand(1));
1211 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1212 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001213 }
1214 break;
1215 case 1: // ret void
1216 if (Tmp1 != Node->getOperand(0))
1217 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1218 break;
1219 default: { // ret <values>
1220 std::vector<SDOperand> NewValues;
1221 NewValues.push_back(Tmp1);
1222 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1223 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1224 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001225 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001226 break;
1227 case Expand: {
1228 SDOperand Lo, Hi;
1229 ExpandOp(Node->getOperand(i), Lo, Hi);
1230 NewValues.push_back(Lo);
1231 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001232 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001233 }
1234 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001235 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001236 }
1237 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1238 break;
1239 }
1240 }
1241 break;
1242 case ISD::STORE:
1243 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1244 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1245
Chris Lattnere69daaf2005-01-08 06:25:56 +00001246 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001247 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001248 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001249 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001250 DAG.getConstant(FloatToBits(CFP->getValue()),
1251 MVT::i32),
1252 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001253 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001254 } else {
1255 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001256 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001257 DAG.getConstant(DoubleToBits(CFP->getValue()),
1258 MVT::i64),
1259 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001260 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001261 }
Chris Lattnera4743132005-02-22 07:23:39 +00001262 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001263 }
1264
Chris Lattnerdc750592005-01-07 07:47:09 +00001265 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1266 case Legal: {
1267 SDOperand Val = LegalizeOp(Node->getOperand(1));
1268 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1269 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001270 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1271 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001272 break;
1273 }
1274 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001275 // Truncate the value and store the result.
1276 Tmp3 = PromoteOp(Node->getOperand(1));
1277 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001278 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001279 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001280 break;
1281
Chris Lattnerdc750592005-01-07 07:47:09 +00001282 case Expand:
1283 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001284 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001285 ExpandOp(Node->getOperand(1), Lo, Hi);
1286
1287 if (!TLI.isLittleEndian())
1288 std::swap(Lo, Hi);
1289
Chris Lattner55e9cde2005-05-11 04:51:16 +00001290 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1291 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001292 // If this is a vector type, then we have to calculate the increment as
1293 // the product of the element size in bytes, and the number of elements
1294 // in the high half of the vector.
1295 if (MVT::Vector == Hi.getValueType()) {
1296 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1297 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1298 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1299 } else {
1300 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1301 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001302 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1303 getIntPtrConstant(IncrementSize));
1304 assert(isTypeLegal(Tmp2.getValueType()) &&
1305 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001306 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001307 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1308 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001309 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1310 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001311 }
1312 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001313 case ISD::PCMARKER:
1314 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001315 if (Tmp1 != Node->getOperand(0))
1316 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001317 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001318 case ISD::READCYCLECOUNTER:
1319 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthf9b27d72005-12-02 06:08:08 +00001320 if (Tmp1 != Node->getOperand(0)) {
1321 std::vector<MVT::ValueType> rtypes;
1322 std::vector<SDOperand> rvals;
1323 rtypes.push_back(MVT::i64);
1324 rtypes.push_back(MVT::Other);
1325 rvals.push_back(Tmp1);
1326 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1327 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001328
1329 // Since rdcc produce two values, make sure to remember that we legalized
1330 // both of them.
1331 AddLegalizedOperand(SDOperand(Node, 0), Result);
1332 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1333 return Result.getValue(Op.ResNo);
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001334
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001335 case ISD::TRUNCSTORE:
1336 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1337 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1338
1339 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1340 case Legal:
1341 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001342
1343 // The only promote case we handle is TRUNCSTORE:i1 X into
1344 // -> TRUNCSTORE:i8 (and X, 1)
1345 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1346 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1347 TargetLowering::Promote) {
1348 // Promote the bool to a mask then store.
1349 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1350 DAG.getConstant(1, Tmp2.getValueType()));
1351 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1352 Node->getOperand(3), DAG.getValueType(MVT::i8));
1353
1354 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1355 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001356 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001357 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001358 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001359 break;
1360 case Promote:
1361 case Expand:
1362 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1363 }
1364 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001365 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001366 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1367 case Expand: assert(0 && "It's impossible to expand bools");
1368 case Legal:
1369 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1370 break;
1371 case Promote:
1372 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1373 break;
1374 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001375 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001376 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001377
Nate Begeman987121a2005-08-23 04:29:48 +00001378 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001379 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001380 case TargetLowering::Expand:
1381 if (Tmp1.getOpcode() == ISD::SETCC) {
1382 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1383 Tmp2, Tmp3,
1384 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1385 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001386 // Make sure the condition is either zero or one. It may have been
1387 // promoted from something else.
1388 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001389 Result = DAG.getSelectCC(Tmp1,
1390 DAG.getConstant(0, Tmp1.getValueType()),
1391 Tmp2, Tmp3, ISD::SETNE);
1392 }
Chris Lattnerc06da622005-12-18 23:54:29 +00001393 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begemane5b86d72005-08-10 20:51:12 +00001394 break;
Evan Cheng225a4d02005-12-17 01:21:05 +00001395 case TargetLowering::Custom: {
1396 SDOperand Tmp =
1397 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1398 Tmp1, Tmp2, Tmp3), DAG);
1399 if (Tmp.Val) {
1400 Result = LegalizeOp(Tmp);
1401 break;
1402 }
1403 // FALLTHROUGH if the target thinks it is legal.
1404 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001405 case TargetLowering::Legal:
1406 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1407 Tmp3 != Node->getOperand(2))
1408 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1409 Tmp1, Tmp2, Tmp3);
1410 break;
1411 case TargetLowering::Promote: {
1412 MVT::ValueType NVT =
1413 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1414 unsigned ExtOp, TruncOp;
1415 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001416 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001417 TruncOp = ISD::TRUNCATE;
1418 } else {
1419 ExtOp = ISD::FP_EXTEND;
1420 TruncOp = ISD::FP_ROUND;
1421 }
1422 // Promote each of the values to the new type.
1423 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1424 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1425 // Perform the larger operation, then round down.
1426 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1427 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1428 break;
1429 }
1430 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001431 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001432 case ISD::SELECT_CC:
1433 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1434 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1435
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001436 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001437 // Everything is legal, see if we should expand this op or something.
1438 switch (TLI.getOperationAction(ISD::SELECT_CC,
1439 Node->getOperand(0).getValueType())) {
1440 default: assert(0 && "This action is not supported yet!");
1441 case TargetLowering::Custom: {
1442 SDOperand Tmp =
1443 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1444 Node->getOperand(0),
1445 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001446 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001447 if (Tmp.Val) {
1448 Result = LegalizeOp(Tmp);
1449 break;
1450 }
1451 } // FALLTHROUGH if the target can't lower this operation after all.
1452 case TargetLowering::Legal:
1453 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1454 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1455 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1456 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1457 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1458 Tmp3, Tmp4, Node->getOperand(4));
1459 }
1460 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001461 }
1462 break;
1463 } else {
1464 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1465 Node->getOperand(0), // LHS
1466 Node->getOperand(1), // RHS
1467 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001468 // If we get a SETCC back from legalizing the SETCC node we just
1469 // created, then use its LHS, RHS, and CC directly in creating a new
1470 // node. Otherwise, select between the true and false value based on
1471 // comparing the result of the legalized with zero.
1472 if (Tmp1.getOpcode() == ISD::SETCC) {
1473 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1474 Tmp1.getOperand(0), Tmp1.getOperand(1),
1475 Tmp3, Tmp4, Tmp1.getOperand(2));
1476 } else {
1477 Result = DAG.getSelectCC(Tmp1,
1478 DAG.getConstant(0, Tmp1.getValueType()),
1479 Tmp3, Tmp4, ISD::SETNE);
1480 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001481 }
1482 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001483 case ISD::SETCC:
1484 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1485 case Legal:
1486 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1487 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001488 break;
1489 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001490 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1491 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1492
1493 // If this is an FP compare, the operands have already been extended.
1494 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1495 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001496 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001497
1498 // Otherwise, we have to insert explicit sign or zero extends. Note
1499 // that we could insert sign extends for ALL conditions, but zero extend
1500 // is cheaper on many machines (an AND instead of two shifts), so prefer
1501 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001502 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001503 default: assert(0 && "Unknown integer comparison!");
1504 case ISD::SETEQ:
1505 case ISD::SETNE:
1506 case ISD::SETUGE:
1507 case ISD::SETUGT:
1508 case ISD::SETULE:
1509 case ISD::SETULT:
1510 // ALL of these operations will work if we either sign or zero extend
1511 // the operands (including the unsigned comparisons!). Zero extend is
1512 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001513 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1514 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001515 break;
1516 case ISD::SETGE:
1517 case ISD::SETGT:
1518 case ISD::SETLT:
1519 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001520 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1521 DAG.getValueType(VT));
1522 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1523 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001524 break;
1525 }
Chris Lattner4d978642005-01-15 22:16:26 +00001526 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001527 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001528 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001529 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1530 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1531 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001532 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001533 case ISD::SETEQ:
1534 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001535 if (RHSLo == RHSHi)
1536 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1537 if (RHSCST->isAllOnesValue()) {
1538 // Comparison to -1.
1539 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001540 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001541 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001542 }
1543
Chris Lattnerdc750592005-01-07 07:47:09 +00001544 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1545 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1546 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001547 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001548 break;
1549 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001550 // If this is a comparison of the sign bit, just look at the top part.
1551 // X > -1, x < 0
1552 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001553 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001554 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001555 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001556 (CST->isAllOnesValue()))) { // X > -1
1557 Tmp1 = LHSHi;
1558 Tmp2 = RHSHi;
1559 break;
1560 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001561
Chris Lattnerdc750592005-01-07 07:47:09 +00001562 // FIXME: This generated code sucks.
1563 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001564 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001565 default: assert(0 && "Unknown integer setcc!");
1566 case ISD::SETLT:
1567 case ISD::SETULT: LowCC = ISD::SETULT; break;
1568 case ISD::SETGT:
1569 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1570 case ISD::SETLE:
1571 case ISD::SETULE: LowCC = ISD::SETULE; break;
1572 case ISD::SETGE:
1573 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1574 }
Misha Brukman835702a2005-04-21 22:36:52 +00001575
Chris Lattnerdc750592005-01-07 07:47:09 +00001576 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1577 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1578 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1579
1580 // NOTE: on targets without efficient SELECT of bools, we can always use
1581 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001582 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1583 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1584 Node->getOperand(2));
1585 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001586 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1587 Result, Tmp1, Tmp2));
Chris Lattner2af3ee42005-12-20 00:53:54 +00001588 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman987121a2005-08-23 04:29:48 +00001589 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001590 }
1591 }
Nate Begeman987121a2005-08-23 04:29:48 +00001592
1593 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1594 default:
1595 assert(0 && "Cannot handle this action for SETCC yet!");
1596 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001597 case TargetLowering::Promote: {
1598 // First step, figure out the appropriate operation to use.
1599 // Allow SETCC to not be supported for all legal data types
1600 // Mostly this targets FP
1601 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1602 MVT::ValueType OldVT = NewInTy;
1603
1604 // Scan for the appropriate larger type to use.
1605 while (1) {
1606 NewInTy = (MVT::ValueType)(NewInTy+1);
1607
1608 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1609 "Fell off of the edge of the integer world");
1610 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1611 "Fell off of the edge of the floating point world");
1612
1613 // If the target supports SETCC of this type, use it.
1614 if (TLI.getOperationAction(ISD::SETCC, NewInTy) == TargetLowering::Legal)
1615 break;
1616 }
1617 if (MVT::isInteger(NewInTy))
1618 assert(0 && "Cannot promote Legal Integer SETCC yet");
1619 else {
1620 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1621 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1622 }
1623
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001624 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1625 Node->getOperand(2));
1626 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001627 }
Evan Chengc1583db2005-12-21 20:21:51 +00001628 case TargetLowering::Custom: {
1629 SDOperand Tmp =
1630 TLI.LowerOperation(DAG.getNode(ISD::SETCC, Node->getValueType(0),
1631 Tmp1, Tmp2, Node->getOperand(2)), DAG);
1632 if (Tmp.Val) {
1633 Result = LegalizeOp(Tmp);
1634 break;
1635 }
1636 // FALLTHROUGH if the target thinks it is legal.
1637 }
Nate Begeman987121a2005-08-23 04:29:48 +00001638 case TargetLowering::Legal:
1639 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1640 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1641 Node->getOperand(2));
1642 break;
1643 case TargetLowering::Expand:
1644 // Expand a setcc node into a select_cc of the same condition, lhs, and
1645 // rhs that selects between const 1 (true) and const 0 (false).
1646 MVT::ValueType VT = Node->getValueType(0);
1647 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1648 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1649 Node->getOperand(2));
1650 Result = LegalizeOp(Result);
1651 break;
1652 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001653 break;
1654
Chris Lattner85d70c62005-01-11 05:57:22 +00001655 case ISD::MEMSET:
1656 case ISD::MEMCPY:
1657 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001658 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001659 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1660
1661 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1662 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1663 case Expand: assert(0 && "Cannot expand a byte!");
1664 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001665 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001666 break;
1667 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001668 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001669 break;
1670 }
1671 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001672 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001673 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001674
1675 SDOperand Tmp4;
1676 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001677 case Expand: {
1678 // Length is too big, just take the lo-part of the length.
1679 SDOperand HiPart;
1680 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1681 break;
1682 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001683 case Legal:
1684 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001685 break;
1686 case Promote:
1687 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001688 break;
1689 }
1690
1691 SDOperand Tmp5;
1692 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1693 case Expand: assert(0 && "Cannot expand this yet!");
1694 case Legal:
1695 Tmp5 = LegalizeOp(Node->getOperand(4));
1696 break;
1697 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001698 Tmp5 = PromoteOp(Node->getOperand(4));
1699 break;
1700 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001701
1702 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1703 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001704 case TargetLowering::Custom: {
1705 SDOperand Tmp =
1706 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1707 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1708 if (Tmp.Val) {
1709 Result = LegalizeOp(Tmp);
1710 break;
1711 }
1712 // FALLTHROUGH if the target thinks it is legal.
1713 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001714 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001715 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1716 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1717 Tmp5 != Node->getOperand(4)) {
1718 std::vector<SDOperand> Ops;
1719 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1720 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1721 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1722 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001723 break;
1724 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001725 // Otherwise, the target does not support this operation. Lower the
1726 // operation to an explicit libcall as appropriate.
1727 MVT::ValueType IntPtr = TLI.getPointerTy();
1728 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1729 std::vector<std::pair<SDOperand, const Type*> > Args;
1730
Reid Spencer6dced922005-01-12 14:53:45 +00001731 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001732 if (Node->getOpcode() == ISD::MEMSET) {
1733 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1734 // Extend the ubyte argument to be an int value for the call.
1735 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1736 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1737 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1738
1739 FnName = "memset";
1740 } else if (Node->getOpcode() == ISD::MEMCPY ||
1741 Node->getOpcode() == ISD::MEMMOVE) {
1742 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1743 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1744 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1745 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1746 } else {
1747 assert(0 && "Unknown op!");
1748 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001749
Chris Lattner85d70c62005-01-11 05:57:22 +00001750 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001751 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001752 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner2af3ee42005-12-20 00:53:54 +00001753 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001754 break;
1755 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001756 }
1757 break;
1758 }
Chris Lattner5385db52005-05-09 20:23:03 +00001759
1760 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001761 Tmp1 = LegalizeOp(Node->getOperand(0));
1762 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001763
Chris Lattner86535992005-05-14 07:45:46 +00001764 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1765 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1766 std::vector<SDOperand> Ops;
1767 Ops.push_back(Tmp1);
1768 Ops.push_back(Tmp2);
1769 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1770 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001771 Result = SDOperand(Node, 0);
1772 // Since these produce two values, make sure to remember that we legalized
1773 // both of them.
1774 AddLegalizedOperand(SDOperand(Node, 0), Result);
1775 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1776 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001777 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001778 Tmp1 = LegalizeOp(Node->getOperand(0));
1779 Tmp2 = LegalizeOp(Node->getOperand(1));
1780 Tmp3 = LegalizeOp(Node->getOperand(2));
1781 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1782 Tmp3 != Node->getOperand(2))
1783 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1784 break;
1785
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001786 case ISD::READIO:
1787 Tmp1 = LegalizeOp(Node->getOperand(0));
1788 Tmp2 = LegalizeOp(Node->getOperand(1));
1789
1790 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1791 case TargetLowering::Custom:
1792 default: assert(0 && "This action not implemented for this operation!");
1793 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001794 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1795 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1796 std::vector<SDOperand> Ops;
1797 Ops.push_back(Tmp1);
1798 Ops.push_back(Tmp2);
1799 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1800 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001801 Result = SDOperand(Node, 0);
1802 break;
1803 case TargetLowering::Expand:
1804 // Replace this with a load from memory.
1805 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1806 Node->getOperand(1), DAG.getSrcValue(NULL));
1807 Result = LegalizeOp(Result);
1808 break;
1809 }
1810
1811 // Since these produce two values, make sure to remember that we legalized
1812 // both of them.
1813 AddLegalizedOperand(SDOperand(Node, 0), Result);
1814 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1815 return Result.getValue(Op.ResNo);
1816
1817 case ISD::WRITEIO:
1818 Tmp1 = LegalizeOp(Node->getOperand(0));
1819 Tmp2 = LegalizeOp(Node->getOperand(1));
1820 Tmp3 = LegalizeOp(Node->getOperand(2));
1821
1822 switch (TLI.getOperationAction(Node->getOpcode(),
1823 Node->getOperand(1).getValueType())) {
1824 case TargetLowering::Custom:
1825 default: assert(0 && "This action not implemented for this operation!");
1826 case TargetLowering::Legal:
1827 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1828 Tmp3 != Node->getOperand(2))
1829 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1830 break;
1831 case TargetLowering::Expand:
1832 // Replace this with a store to memory.
1833 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1834 Node->getOperand(1), Node->getOperand(2),
1835 DAG.getSrcValue(NULL));
1836 Result = LegalizeOp(Result);
1837 break;
1838 }
1839 break;
1840
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001841 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001842 case ISD::SUB_PARTS:
1843 case ISD::SHL_PARTS:
1844 case ISD::SRA_PARTS:
1845 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001846 std::vector<SDOperand> Ops;
1847 bool Changed = false;
1848 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1849 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1850 Changed |= Ops.back() != Node->getOperand(i);
1851 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001852 if (Changed) {
1853 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1854 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1855 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001856
1857 // Since these produce multiple values, make sure to remember that we
1858 // legalized all of them.
1859 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1860 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1861 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001862 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001863
1864 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001865 case ISD::ADD:
1866 case ISD::SUB:
1867 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001868 case ISD::MULHS:
1869 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001870 case ISD::UDIV:
1871 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001872 case ISD::AND:
1873 case ISD::OR:
1874 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001875 case ISD::SHL:
1876 case ISD::SRL:
1877 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001878 case ISD::FADD:
1879 case ISD::FSUB:
1880 case ISD::FMUL:
1881 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001882 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001883 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1884 case Expand: assert(0 && "Not possible");
1885 case Legal:
1886 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1887 break;
1888 case Promote:
1889 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1890 break;
1891 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001892 if (Tmp1 != Node->getOperand(0) ||
1893 Tmp2 != Node->getOperand(1))
1894 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1895 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001896
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001897 case ISD::BUILD_PAIR: {
1898 MVT::ValueType PairTy = Node->getValueType(0);
1899 // TODO: handle the case where the Lo and Hi operands are not of legal type
1900 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1901 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1902 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1903 case TargetLowering::Legal:
1904 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1905 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1906 break;
1907 case TargetLowering::Promote:
1908 case TargetLowering::Custom:
1909 assert(0 && "Cannot promote/custom this yet!");
1910 case TargetLowering::Expand:
1911 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1912 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1913 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1914 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1915 TLI.getShiftAmountTy()));
1916 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1917 break;
1918 }
1919 break;
1920 }
1921
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001922 case ISD::UREM:
1923 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001924 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001925 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1926 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1927 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1928 case TargetLowering::Legal:
1929 if (Tmp1 != Node->getOperand(0) ||
1930 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001931 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001932 Tmp2);
1933 break;
1934 case TargetLowering::Promote:
1935 case TargetLowering::Custom:
1936 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001937 case TargetLowering::Expand:
1938 if (MVT::isInteger(Node->getValueType(0))) {
1939 MVT::ValueType VT = Node->getValueType(0);
1940 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1941 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1942 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1943 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1944 } else {
1945 // Floating point mod -> fmod libcall.
1946 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1947 SDOperand Dummy;
1948 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001949 }
1950 break;
1951 }
1952 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001953
Andrew Lenharth5e177822005-05-03 17:19:30 +00001954 case ISD::CTPOP:
1955 case ISD::CTTZ:
1956 case ISD::CTLZ:
1957 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1958 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1959 case TargetLowering::Legal:
1960 if (Tmp1 != Node->getOperand(0))
1961 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1962 break;
1963 case TargetLowering::Promote: {
1964 MVT::ValueType OVT = Tmp1.getValueType();
1965 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001966
1967 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001968 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1969 // Perform the larger operation, then subtract if needed.
1970 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1971 switch(Node->getOpcode())
1972 {
1973 case ISD::CTPOP:
1974 Result = Tmp1;
1975 break;
1976 case ISD::CTTZ:
1977 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001978 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1979 DAG.getConstant(getSizeInBits(NVT), NVT),
1980 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001981 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001982 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1983 break;
1984 case ISD::CTLZ:
1985 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001986 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1987 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001988 getSizeInBits(OVT), NVT));
1989 break;
1990 }
1991 break;
1992 }
1993 case TargetLowering::Custom:
1994 assert(0 && "Cannot custom handle this yet!");
1995 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001996 switch(Node->getOpcode())
1997 {
1998 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001999 static const uint64_t mask[6] = {
2000 0x5555555555555555ULL, 0x3333333333333333ULL,
2001 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2002 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2003 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002004 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00002005 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2006 unsigned len = getSizeInBits(VT);
2007 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002008 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00002009 Tmp2 = DAG.getConstant(mask[i], VT);
2010 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002011 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002012 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
2013 DAG.getNode(ISD::AND, VT,
2014 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
2015 Tmp2));
2016 }
2017 Result = Tmp1;
2018 break;
2019 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002020 case ISD::CTLZ: {
2021 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00002022 x = x | (x >> 1);
2023 x = x | (x >> 2);
2024 ...
2025 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002026 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00002027 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002028
Chris Lattner56add052005-05-11 18:35:21 +00002029 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2030 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002031 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2032 unsigned len = getSizeInBits(VT);
2033 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2034 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002035 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002036 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2037 }
2038 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00002039 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00002040 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002041 }
2042 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002043 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002044 // unless the target has ctlz but not ctpop, in which case we use:
2045 // { return 32 - nlz(~x & (x-1)); }
2046 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00002047 MVT::ValueType VT = Tmp1.getValueType();
2048 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002049 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00002050 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2051 DAG.getNode(ISD::SUB, VT, Tmp1,
2052 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002053 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002054 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2055 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002056 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002057 DAG.getConstant(getSizeInBits(VT), VT),
2058 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2059 } else {
2060 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2061 }
Chris Lattner72473242005-05-11 05:27:09 +00002062 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002063 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002064 default:
2065 assert(0 && "Cannot expand this yet!");
2066 break;
2067 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002068 break;
2069 }
2070 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002071
Chris Lattner13fe99c2005-04-02 05:00:07 +00002072 // Unary operators
2073 case ISD::FABS:
2074 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002075 case ISD::FSQRT:
2076 case ISD::FSIN:
2077 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002078 Tmp1 = LegalizeOp(Node->getOperand(0));
2079 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2080 case TargetLowering::Legal:
2081 if (Tmp1 != Node->getOperand(0))
2082 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2083 break;
2084 case TargetLowering::Promote:
2085 case TargetLowering::Custom:
2086 assert(0 && "Cannot promote/custom handle this yet!");
2087 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00002088 switch(Node->getOpcode()) {
2089 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00002090 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2091 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00002092 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00002093 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00002094 break;
2095 }
2096 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002097 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2098 MVT::ValueType VT = Node->getValueType(0);
2099 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002100 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002101 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2102 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2103 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00002104 break;
2105 }
2106 case ISD::FSQRT:
2107 case ISD::FSIN:
2108 case ISD::FCOS: {
2109 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002110 const char *FnName = 0;
2111 switch(Node->getOpcode()) {
2112 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2113 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2114 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2115 default: assert(0 && "Unreachable!");
2116 }
Nate Begeman77558da2005-08-04 21:43:28 +00002117 SDOperand Dummy;
2118 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002119 break;
2120 }
2121 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002122 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002123 }
2124 break;
2125 }
2126 break;
2127
2128 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002129 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002130 case ISD::UINT_TO_FP: {
2131 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002132 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2133 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002134 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002135 Node->getOperand(0).getValueType())) {
2136 default: assert(0 && "Unknown operation action!");
2137 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002138 Result = ExpandLegalINT_TO_FP(isSigned,
2139 LegalizeOp(Node->getOperand(0)),
2140 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002141 AddLegalizedOperand(Op, Result);
2142 return Result;
2143 case TargetLowering::Promote:
2144 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2145 Node->getValueType(0),
2146 isSigned);
2147 AddLegalizedOperand(Op, Result);
2148 return Result;
2149 case TargetLowering::Legal:
2150 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002151 case TargetLowering::Custom: {
2152 Tmp1 = LegalizeOp(Node->getOperand(0));
2153 SDOperand Tmp =
2154 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2155 Tmp = TLI.LowerOperation(Tmp, DAG);
2156 if (Tmp.Val) {
Chris Lattner2af3ee42005-12-20 00:53:54 +00002157 Tmp = LegalizeOp(Tmp); // Relegalize input.
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002158 AddLegalizedOperand(Op, Tmp);
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002159 return Tmp;
2160 } else {
2161 assert(0 && "Target Must Lower this");
2162 }
2163 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002164 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002165
Chris Lattnerdc750592005-01-07 07:47:09 +00002166 Tmp1 = LegalizeOp(Node->getOperand(0));
2167 if (Tmp1 != Node->getOperand(0))
2168 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2169 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002170 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002171 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2172 Node->getValueType(0), Node->getOperand(0));
2173 break;
2174 case Promote:
2175 if (isSigned) {
2176 Result = PromoteOp(Node->getOperand(0));
2177 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2178 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2179 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2180 } else {
2181 Result = PromoteOp(Node->getOperand(0));
2182 Result = DAG.getZeroExtendInReg(Result,
2183 Node->getOperand(0).getValueType());
2184 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002185 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002186 break;
2187 }
2188 break;
2189 }
2190 case ISD::TRUNCATE:
2191 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2192 case Legal:
2193 Tmp1 = LegalizeOp(Node->getOperand(0));
2194 if (Tmp1 != Node->getOperand(0))
2195 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2196 break;
2197 case Expand:
2198 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2199
2200 // Since the result is legal, we should just be able to truncate the low
2201 // part of the source.
2202 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2203 break;
2204 case Promote:
2205 Result = PromoteOp(Node->getOperand(0));
2206 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2207 break;
2208 }
2209 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002210
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002211 case ISD::FP_TO_SINT:
2212 case ISD::FP_TO_UINT:
2213 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2214 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002215 Tmp1 = LegalizeOp(Node->getOperand(0));
2216
Chris Lattner44fe26f2005-07-29 00:11:56 +00002217 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2218 default: assert(0 && "Unknown operation action!");
2219 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002220 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2221 SDOperand True, False;
2222 MVT::ValueType VT = Node->getOperand(0).getValueType();
2223 MVT::ValueType NVT = Node->getValueType(0);
2224 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2225 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2226 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2227 Node->getOperand(0), Tmp2, ISD::SETLT);
2228 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2229 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002230 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002231 Tmp2));
2232 False = DAG.getNode(ISD::XOR, NVT, False,
2233 DAG.getConstant(1ULL << ShiftAmt, NVT));
2234 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Chris Lattner2af3ee42005-12-20 00:53:54 +00002235 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemand5e739d2005-08-14 18:38:32 +00002236 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002237 } else {
2238 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2239 }
2240 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002241 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002242 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002243 Node->getOpcode() == ISD::FP_TO_SINT);
2244 AddLegalizedOperand(Op, Result);
2245 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002246 case TargetLowering::Custom: {
2247 SDOperand Tmp =
2248 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2249 Tmp = TLI.LowerOperation(Tmp, DAG);
2250 if (Tmp.Val) {
Chris Lattner2af3ee42005-12-20 00:53:54 +00002251 Tmp = LegalizeOp(Tmp);
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002252 AddLegalizedOperand(Op, Tmp);
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002253 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002254 } else {
2255 // The target thinks this is legal afterall.
2256 break;
2257 }
2258 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002259 case TargetLowering::Legal:
2260 break;
2261 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002262
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002263 if (Tmp1 != Node->getOperand(0))
2264 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2265 break;
2266 case Expand:
2267 assert(0 && "Shouldn't need to expand other operators here!");
2268 case Promote:
2269 Result = PromoteOp(Node->getOperand(0));
2270 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2271 break;
2272 }
2273 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002274
Chris Lattner7753f172005-09-02 00:18:10 +00002275 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002276 case ISD::ZERO_EXTEND:
2277 case ISD::SIGN_EXTEND:
2278 case ISD::FP_EXTEND:
2279 case ISD::FP_ROUND:
2280 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2281 case Legal:
2282 Tmp1 = LegalizeOp(Node->getOperand(0));
2283 if (Tmp1 != Node->getOperand(0))
2284 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2285 break;
2286 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002287 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002288
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002289 case Promote:
2290 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002291 case ISD::ANY_EXTEND:
2292 Result = PromoteOp(Node->getOperand(0));
2293 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2294 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002295 case ISD::ZERO_EXTEND:
2296 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002297 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002298 Result = DAG.getZeroExtendInReg(Result,
2299 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002300 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002301 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002302 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002303 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002304 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002305 Result,
2306 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002307 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002308 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002309 Result = PromoteOp(Node->getOperand(0));
2310 if (Result.getValueType() != Op.getValueType())
2311 // Dynamically dead while we have only 2 FP types.
2312 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2313 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002314 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002315 Result = PromoteOp(Node->getOperand(0));
2316 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2317 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002318 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002319 }
2320 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002321 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002322 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002323 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002324 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002325
2326 // If this operation is not supported, convert it to a shl/shr or load/store
2327 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002328 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2329 default: assert(0 && "This action not supported for this op yet!");
2330 case TargetLowering::Legal:
2331 if (Tmp1 != Node->getOperand(0))
2332 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002333 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002334 break;
2335 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002336 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002337 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002338 // NOTE: we could fall back on load/store here too for targets without
2339 // SAR. However, it is doubtful that any exist.
2340 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2341 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002342 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002343 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2344 Node->getOperand(0), ShiftCst);
2345 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2346 Result, ShiftCst);
2347 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2348 // The only way we can lower this is to turn it into a STORETRUNC,
2349 // EXTLOAD pair, targetting a temporary location (a stack slot).
2350
2351 // NOTE: there is a choice here between constantly creating new stack
2352 // slots and always reusing the same one. We currently always create
2353 // new ones, as reuse may inhibit scheduling.
2354 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2355 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2356 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2357 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002358 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002359 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2360 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2361 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002362 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002363 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002364 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2365 Result, StackSlot, DAG.getSrcValue(NULL),
2366 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002367 } else {
2368 assert(0 && "Unknown op");
2369 }
2370 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002371 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002372 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002373 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002374 }
Chris Lattner99222f72005-01-15 07:15:18 +00002375 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002376
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002377 // Note that LegalizeOp may be reentered even from single-use nodes, which
2378 // means that we always must cache transformed nodes.
2379 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002380 return Result;
2381}
2382
Chris Lattner4d978642005-01-15 22:16:26 +00002383/// PromoteOp - Given an operation that produces a value in an invalid type,
2384/// promote it to compute the value into a larger type. The produced value will
2385/// have the correct bits for the low portion of the register, but no guarantee
2386/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002387SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2388 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002389 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002390 assert(getTypeAction(VT) == Promote &&
2391 "Caller should expand or legalize operands that are not promotable!");
2392 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2393 "Cannot promote to smaller type!");
2394
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002395 SDOperand Tmp1, Tmp2, Tmp3;
2396
2397 SDOperand Result;
2398 SDNode *Node = Op.Val;
2399
Chris Lattner1a570f12005-09-02 20:32:45 +00002400 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2401 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002402
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002403 // Promotion needs an optimization step to clean up after it, and is not
2404 // careful to avoid operations the target does not support. Make sure that
2405 // all generated operations are legalized in the next iteration.
2406 NeedsAnotherIteration = true;
2407
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002408 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002409 case ISD::CopyFromReg:
2410 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002411 default:
2412 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2413 assert(0 && "Do not know how to promote this operator!");
2414 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002415 case ISD::UNDEF:
2416 Result = DAG.getNode(ISD::UNDEF, NVT);
2417 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002418 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002419 if (VT != MVT::i1)
2420 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2421 else
2422 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002423 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2424 break;
2425 case ISD::ConstantFP:
2426 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2427 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2428 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002429
Chris Lattner2cb338d2005-01-18 02:59:52 +00002430 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002431 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002432 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2433 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002434 Result = LegalizeOp(Result);
2435 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002436
2437 case ISD::TRUNCATE:
2438 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2439 case Legal:
2440 Result = LegalizeOp(Node->getOperand(0));
2441 assert(Result.getValueType() >= NVT &&
2442 "This truncation doesn't make sense!");
2443 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2444 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2445 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002446 case Promote:
2447 // The truncation is not required, because we don't guarantee anything
2448 // about high bits anyway.
2449 Result = PromoteOp(Node->getOperand(0));
2450 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002451 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002452 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2453 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002454 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002455 }
2456 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002457 case ISD::SIGN_EXTEND:
2458 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002459 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002460 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2461 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2462 case Legal:
2463 // Input is legal? Just do extend all the way to the larger type.
2464 Result = LegalizeOp(Node->getOperand(0));
2465 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2466 break;
2467 case Promote:
2468 // Promote the reg if it's smaller.
2469 Result = PromoteOp(Node->getOperand(0));
2470 // The high bits are not guaranteed to be anything. Insert an extend.
2471 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002472 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002473 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002474 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002475 Result = DAG.getZeroExtendInReg(Result,
2476 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002477 break;
2478 }
2479 break;
2480
2481 case ISD::FP_EXTEND:
2482 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2483 case ISD::FP_ROUND:
2484 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2485 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2486 case Promote: assert(0 && "Unreachable with 2 FP types!");
2487 case Legal:
2488 // Input is legal? Do an FP_ROUND_INREG.
2489 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002490 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2491 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002492 break;
2493 }
2494 break;
2495
2496 case ISD::SINT_TO_FP:
2497 case ISD::UINT_TO_FP:
2498 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2499 case Legal:
2500 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002501 // No extra round required here.
2502 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002503 break;
2504
2505 case Promote:
2506 Result = PromoteOp(Node->getOperand(0));
2507 if (Node->getOpcode() == ISD::SINT_TO_FP)
2508 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002509 Result,
2510 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002511 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002512 Result = DAG.getZeroExtendInReg(Result,
2513 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002514 // No extra round required here.
2515 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002516 break;
2517 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002518 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2519 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002520 // Round if we cannot tolerate excess precision.
2521 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002522 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2523 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002524 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002525 }
Chris Lattner4d978642005-01-15 22:16:26 +00002526 break;
2527
Chris Lattner268d4572005-12-09 17:32:47 +00002528 case ISD::SIGN_EXTEND_INREG:
2529 Result = PromoteOp(Node->getOperand(0));
2530 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2531 Node->getOperand(1));
2532 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002533 case ISD::FP_TO_SINT:
2534 case ISD::FP_TO_UINT:
2535 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2536 case Legal:
2537 Tmp1 = LegalizeOp(Node->getOperand(0));
2538 break;
2539 case Promote:
2540 // The input result is prerounded, so we don't have to do anything
2541 // special.
2542 Tmp1 = PromoteOp(Node->getOperand(0));
2543 break;
2544 case Expand:
2545 assert(0 && "not implemented");
2546 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002547 // If we're promoting a UINT to a larger size, check to see if the new node
2548 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2549 // we can use that instead. This allows us to generate better code for
2550 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2551 // legal, such as PowerPC.
2552 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002553 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002554 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2555 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002556 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2557 } else {
2558 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2559 }
Chris Lattner4d978642005-01-15 22:16:26 +00002560 break;
2561
Chris Lattner13fe99c2005-04-02 05:00:07 +00002562 case ISD::FABS:
2563 case ISD::FNEG:
2564 Tmp1 = PromoteOp(Node->getOperand(0));
2565 assert(Tmp1.getValueType() == NVT);
2566 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2567 // NOTE: we do not have to do any extra rounding here for
2568 // NoExcessFPPrecision, because we know the input will have the appropriate
2569 // precision, and these operations don't modify precision at all.
2570 break;
2571
Chris Lattner9d6fa982005-04-28 21:44:33 +00002572 case ISD::FSQRT:
2573 case ISD::FSIN:
2574 case ISD::FCOS:
2575 Tmp1 = PromoteOp(Node->getOperand(0));
2576 assert(Tmp1.getValueType() == NVT);
2577 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2578 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002579 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2580 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002581 break;
2582
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002583 case ISD::AND:
2584 case ISD::OR:
2585 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002586 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002587 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002588 case ISD::MUL:
2589 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002590 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002591 // that too is okay if they are integer operations.
2592 Tmp1 = PromoteOp(Node->getOperand(0));
2593 Tmp2 = PromoteOp(Node->getOperand(1));
2594 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2595 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002596 break;
2597 case ISD::FADD:
2598 case ISD::FSUB:
2599 case ISD::FMUL:
2600 // The input may have strange things in the top bits of the registers, but
2601 // these operations don't care.
2602 Tmp1 = PromoteOp(Node->getOperand(0));
2603 Tmp2 = PromoteOp(Node->getOperand(1));
2604 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2605 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2606
2607 // Floating point operations will give excess precision that we may not be
2608 // able to tolerate. If we DO allow excess precision, just leave it,
2609 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002610 // FIXME: Why would we need to round FP ops more than integer ones?
2611 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002612 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002613 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2614 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002615 break;
2616
Chris Lattner4d978642005-01-15 22:16:26 +00002617 case ISD::SDIV:
2618 case ISD::SREM:
2619 // These operators require that their input be sign extended.
2620 Tmp1 = PromoteOp(Node->getOperand(0));
2621 Tmp2 = PromoteOp(Node->getOperand(1));
2622 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002623 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2624 DAG.getValueType(VT));
2625 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2626 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002627 }
2628 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2629
2630 // Perform FP_ROUND: this is probably overly pessimistic.
2631 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002632 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2633 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002634 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002635 case ISD::FDIV:
2636 case ISD::FREM:
2637 // These operators require that their input be fp extended.
2638 Tmp1 = PromoteOp(Node->getOperand(0));
2639 Tmp2 = PromoteOp(Node->getOperand(1));
2640 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2641
2642 // Perform FP_ROUND: this is probably overly pessimistic.
2643 if (NoExcessFPPrecision)
2644 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2645 DAG.getValueType(VT));
2646 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002647
2648 case ISD::UDIV:
2649 case ISD::UREM:
2650 // These operators require that their input be zero extended.
2651 Tmp1 = PromoteOp(Node->getOperand(0));
2652 Tmp2 = PromoteOp(Node->getOperand(1));
2653 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002654 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2655 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002656 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2657 break;
2658
2659 case ISD::SHL:
2660 Tmp1 = PromoteOp(Node->getOperand(0));
2661 Tmp2 = LegalizeOp(Node->getOperand(1));
2662 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2663 break;
2664 case ISD::SRA:
2665 // The input value must be properly sign extended.
2666 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002667 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2668 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002669 Tmp2 = LegalizeOp(Node->getOperand(1));
2670 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2671 break;
2672 case ISD::SRL:
2673 // The input value must be properly zero extended.
2674 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002675 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002676 Tmp2 = LegalizeOp(Node->getOperand(1));
2677 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2678 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002679 case ISD::LOAD:
2680 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2681 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002682 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2683 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002684 // Remember that we legalized the chain.
2685 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2686 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002687 case ISD::SEXTLOAD:
2688 case ISD::ZEXTLOAD:
2689 case ISD::EXTLOAD:
2690 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2691 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002692 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2693 Node->getOperand(2),
2694 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002695 // Remember that we legalized the chain.
2696 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2697 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002698 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002699 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2700 case Expand: assert(0 && "It's impossible to expand bools");
2701 case Legal:
2702 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2703 break;
2704 case Promote:
2705 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2706 break;
2707 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002708 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2709 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2710 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2711 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002712 case ISD::SELECT_CC:
2713 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2714 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2715 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2716 Node->getOperand(1), Tmp2, Tmp3,
2717 Node->getOperand(4));
2718 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002719 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002720 case ISD::CALL: {
2721 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2722 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2723
Chris Lattner3d95c142005-01-19 20:24:35 +00002724 std::vector<SDOperand> Ops;
2725 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2726 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2727
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002728 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2729 "Can only promote single result calls");
2730 std::vector<MVT::ValueType> RetTyVTs;
2731 RetTyVTs.reserve(2);
2732 RetTyVTs.push_back(NVT);
2733 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002734 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2735 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002736 Result = SDOperand(NC, 0);
2737
2738 // Insert the new chain mapping.
2739 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2740 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002741 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002742 case ISD::CTPOP:
2743 case ISD::CTTZ:
2744 case ISD::CTLZ:
2745 Tmp1 = Node->getOperand(0);
2746 //Zero extend the argument
2747 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2748 // Perform the larger operation, then subtract if needed.
2749 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2750 switch(Node->getOpcode())
2751 {
2752 case ISD::CTPOP:
2753 Result = Tmp1;
2754 break;
2755 case ISD::CTTZ:
2756 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002757 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002758 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002759 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002760 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2761 break;
2762 case ISD::CTLZ:
2763 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002764 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2765 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002766 getSizeInBits(VT), NVT));
2767 break;
2768 }
2769 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002770 }
2771
2772 assert(Result.Val && "Didn't set a result!");
2773 AddPromotedOperand(Op, Result);
2774 return Result;
2775}
Chris Lattnerdc750592005-01-07 07:47:09 +00002776
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002777/// ExpandAddSub - Find a clever way to expand this add operation into
2778/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002779void SelectionDAGLegalize::
2780ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2781 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002782 // Expand the subcomponents.
2783 SDOperand LHSL, LHSH, RHSL, RHSH;
2784 ExpandOp(LHS, LHSL, LHSH);
2785 ExpandOp(RHS, RHSL, RHSH);
2786
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002787 std::vector<SDOperand> Ops;
2788 Ops.push_back(LHSL);
2789 Ops.push_back(LHSH);
2790 Ops.push_back(RHSL);
2791 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002792 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2793 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002794 Hi = Lo.getValue(1);
2795}
2796
Chris Lattner4157c412005-04-02 04:00:59 +00002797void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2798 SDOperand Op, SDOperand Amt,
2799 SDOperand &Lo, SDOperand &Hi) {
2800 // Expand the subcomponents.
2801 SDOperand LHSL, LHSH;
2802 ExpandOp(Op, LHSL, LHSH);
2803
2804 std::vector<SDOperand> Ops;
2805 Ops.push_back(LHSL);
2806 Ops.push_back(LHSH);
2807 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002808 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002809 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002810 Hi = Lo.getValue(1);
2811}
2812
2813
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002814/// ExpandShift - Try to find a clever way to expand this shift operation out to
2815/// smaller elements. If we can't find a way that is more efficient than a
2816/// libcall on this target, return false. Otherwise, return true with the
2817/// low-parts expanded into Lo and Hi.
2818bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2819 SDOperand &Lo, SDOperand &Hi) {
2820 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2821 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002822
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002823 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002824 SDOperand ShAmt = LegalizeOp(Amt);
2825 MVT::ValueType ShTy = ShAmt.getValueType();
2826 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2827 unsigned NVTBits = MVT::getSizeInBits(NVT);
2828
2829 // Handle the case when Amt is an immediate. Other cases are currently broken
2830 // and are disabled.
2831 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2832 unsigned Cst = CN->getValue();
2833 // Expand the incoming operand to be shifted, so that we have its parts
2834 SDOperand InL, InH;
2835 ExpandOp(Op, InL, InH);
2836 switch(Opc) {
2837 case ISD::SHL:
2838 if (Cst > VTBits) {
2839 Lo = DAG.getConstant(0, NVT);
2840 Hi = DAG.getConstant(0, NVT);
2841 } else if (Cst > NVTBits) {
2842 Lo = DAG.getConstant(0, NVT);
2843 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002844 } else if (Cst == NVTBits) {
2845 Lo = DAG.getConstant(0, NVT);
2846 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002847 } else {
2848 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2849 Hi = DAG.getNode(ISD::OR, NVT,
2850 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2851 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2852 }
2853 return true;
2854 case ISD::SRL:
2855 if (Cst > VTBits) {
2856 Lo = DAG.getConstant(0, NVT);
2857 Hi = DAG.getConstant(0, NVT);
2858 } else if (Cst > NVTBits) {
2859 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2860 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002861 } else if (Cst == NVTBits) {
2862 Lo = InH;
2863 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002864 } else {
2865 Lo = DAG.getNode(ISD::OR, NVT,
2866 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2867 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2868 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2869 }
2870 return true;
2871 case ISD::SRA:
2872 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002873 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002874 DAG.getConstant(NVTBits-1, ShTy));
2875 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002876 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002877 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002878 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002879 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002880 } else if (Cst == NVTBits) {
2881 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002882 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002883 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002884 } else {
2885 Lo = DAG.getNode(ISD::OR, NVT,
2886 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2887 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2888 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2889 }
2890 return true;
2891 }
2892 }
2893 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2894 // so disable it for now. Currently targets are handling this via SHL_PARTS
2895 // and friends.
2896 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002897
2898 // If we have an efficient select operation (or if the selects will all fold
2899 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002900 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002901 return false;
2902
2903 SDOperand InL, InH;
2904 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002905 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2906 DAG.getConstant(NVTBits, ShTy), ShAmt);
2907
Chris Lattner4d25c042005-01-20 20:29:23 +00002908 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002909 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2910 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002911
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002912 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2913 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2914 DAG.getConstant(NVTBits-1, ShTy));
2915 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2916 DAG.getConstant(NVTBits-1, ShTy));
2917 }
2918
2919 if (Opc == ISD::SHL) {
2920 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2921 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2922 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002923 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002924
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002925 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2926 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2927 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002928 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002929 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2930 DAG.getConstant(32, ShTy),
2931 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002932 DAG.getConstant(0, NVT),
2933 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002934 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002935 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002936 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002937 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002938
2939 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002940 if (Opc == ISD::SRA)
2941 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2942 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002943 else
2944 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002945 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002946 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002947 }
2948 return true;
2949}
Chris Lattneraac464e2005-01-21 06:05:23 +00002950
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002951/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2952/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002953/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002954static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002955 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002956
Chris Lattner2dce7032005-05-12 23:24:06 +00002957 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002958 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002959 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002960 Found = Node;
2961 return;
2962 }
2963
2964 // Otherwise, scan the operands of Node to see if any of them is a call.
2965 assert(Node->getNumOperands() != 0 &&
2966 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002967 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002968 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002969
2970 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002971 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002972 Found);
2973}
2974
2975
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002976/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2977/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002978/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002979static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2980 std::set<SDNode*> &Visited) {
2981 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2982 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002983
Chris Lattner2dce7032005-05-12 23:24:06 +00002984 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002985 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002986 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002987 Found = Node;
2988 return;
2989 }
2990
2991 // Otherwise, scan the operands of Node to see if any of them is a call.
2992 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2993 if (UI == E) return;
2994 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002995 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002996
2997 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002998 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002999}
3000
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003001/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003002/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003003static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00003004 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00003005 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00003006 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003007 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00003008
Chris Lattner4add7e32005-01-23 04:42:50 +00003009 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00003010 if (TheChain.getValueType() != MVT::Other)
3011 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00003012 if (TheChain.getValueType() != MVT::Other)
3013 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00003014
3015 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00003016 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00003017
Chris Lattner4add7e32005-01-23 04:42:50 +00003018 // Make sure to only follow users of our token chain.
3019 SDNode *User = *UI;
3020 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3021 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00003022 if (SDNode *Result = FindCallSeqEnd(User))
3023 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00003024 }
Chris Lattnercabdc342005-08-05 16:23:57 +00003025 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00003026}
3027
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003028/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003029/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003030static SDNode *FindCallSeqStart(SDNode *Node) {
3031 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00003032 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003033
3034 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3035 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003036 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003037}
3038
3039
Chris Lattner4add7e32005-01-23 04:42:50 +00003040/// FindInputOutputChains - If we are replacing an operation with a call we need
3041/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00003042/// properly serialize the calls in the block. The returned operand is the
3043/// input chain value for the new call (e.g. the entry node or the previous
3044/// call), and OutChain is set to be the chain node to update to point to the
3045/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00003046static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3047 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003048 SDNode *LatestCallSeqStart = Entry.Val;
3049 SDNode *LatestCallSeqEnd = 0;
3050 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
3051 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00003052
Chris Lattner2dce7032005-05-12 23:24:06 +00003053 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00003054 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00003055 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3056 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00003057 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003058 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00003059 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3060 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003061 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00003062 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003063 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00003064
Chris Lattner06bbeb62005-05-11 19:02:11 +00003065 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003066 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003067 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00003068 std::set<SDNode*> Visited;
3069 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003070
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003071 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003072 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003073 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00003074
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003075 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00003076}
3077
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003078/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00003079void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3080 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00003081 // Nothing to splice it into?
3082 if (OutChain == 0) return;
3083
3084 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3085 //OutChain->dump();
3086
3087 // Form a token factor node merging the old inval and the new inval.
3088 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3089 OutChain->getOperand(0));
3090 // Change the node to refer to the new token.
3091 OutChain->setAdjCallChain(InToken);
3092}
Chris Lattner4add7e32005-01-23 04:42:50 +00003093
3094
Chris Lattneraac464e2005-01-21 06:05:23 +00003095// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3096// does not fit into a register, return the lo part and set the hi part to the
3097// by-reg argument. If it does fit into a single register, return the result
3098// and leave the Hi part unset.
3099SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3100 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003101 SDNode *OutChain;
3102 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3103 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00003104 if (InChain.Val == 0)
3105 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00003106
Chris Lattneraac464e2005-01-21 06:05:23 +00003107 TargetLowering::ArgListTy Args;
3108 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3109 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3110 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3111 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3112 }
3113 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003114
Chris Lattner06bbeb62005-05-11 19:02:11 +00003115 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003116 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003117 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003118 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3119 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003120
Chris Lattner63022662005-09-02 20:26:58 +00003121 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003122 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003123 default: assert(0 && "Unknown thing");
3124 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00003125 Result = CallInfo.first;
3126 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003127 case Promote:
3128 assert(0 && "Cannot promote this yet!");
3129 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003130 ExpandOp(CallInfo.first, Result, Hi);
3131 CallInfo.second = LegalizeOp(CallInfo.second);
3132 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003133 }
Chris Lattner63022662005-09-02 20:26:58 +00003134
3135 SpliceCallInto(CallInfo.second, OutChain);
3136 NeedsAnotherIteration = true;
3137 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003138}
3139
Chris Lattner4add7e32005-01-23 04:42:50 +00003140
Chris Lattneraac464e2005-01-21 06:05:23 +00003141/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3142/// destination type is legal.
3143SDOperand SelectionDAGLegalize::
3144ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003145 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003146 assert(getTypeAction(Source.getValueType()) == Expand &&
3147 "This is not an expansion!");
3148 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3149
Chris Lattner06bbeb62005-05-11 19:02:11 +00003150 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003151 assert(Source.getValueType() == MVT::i64 &&
3152 "This only works for 64-bit -> FP");
3153 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3154 // incoming integer is set. To handle this, we dynamically test to see if
3155 // it is set, and, if so, add a fudge factor.
3156 SDOperand Lo, Hi;
3157 ExpandOp(Source, Lo, Hi);
3158
Chris Lattner2a4f7312005-05-13 04:45:13 +00003159 // If this is unsigned, and not supported, first perform the conversion to
3160 // signed, then adjust the result if the sign bit is set.
3161 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3162 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3163
Chris Lattnerd47675e2005-08-09 20:20:18 +00003164 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3165 DAG.getConstant(0, Hi.getValueType()),
3166 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003167 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3168 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3169 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003170 uint64_t FF = 0x5f800000ULL;
3171 if (TLI.isLittleEndian()) FF <<= 32;
3172 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003173
Chris Lattnerc30405e2005-08-26 17:15:30 +00003174 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003175 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3176 SDOperand FudgeInReg;
3177 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003178 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3179 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003180 else {
3181 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003182 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3183 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003184 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003185 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003186 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003187
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003188 // Check to see if the target has a custom way to lower this. If so, use it.
3189 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3190 default: assert(0 && "This action not implemented for this operation!");
3191 case TargetLowering::Legal:
3192 case TargetLowering::Expand:
3193 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003194 case TargetLowering::Custom: {
3195 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3196 Source), DAG);
3197 if (NV.Val)
3198 return LegalizeOp(NV);
3199 break; // The target decided this was legal after all
3200 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003201 }
3202
Chris Lattner153587e2005-05-12 07:00:44 +00003203 // Expand the source, then glue it back together for the call. We must expand
3204 // the source in case it is shared (this pass of legalize must traverse it).
3205 SDOperand SrcLo, SrcHi;
3206 ExpandOp(Source, SrcLo, SrcHi);
3207 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3208
Chris Lattner06bbeb62005-05-11 19:02:11 +00003209 SDNode *OutChain = 0;
3210 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3211 DAG.getEntryNode());
3212 const char *FnName = 0;
3213 if (DestTy == MVT::f32)
3214 FnName = "__floatdisf";
3215 else {
3216 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3217 FnName = "__floatdidf";
3218 }
3219
Chris Lattneraac464e2005-01-21 06:05:23 +00003220 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3221
3222 TargetLowering::ArgListTy Args;
3223 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003224
Chris Lattneraac464e2005-01-21 06:05:23 +00003225 Args.push_back(std::make_pair(Source, ArgTy));
3226
3227 // We don't care about token chains for libcalls. We just use the entry
3228 // node as our input and ignore the output chain. This allows us to place
3229 // calls wherever we need them to satisfy data dependences.
3230 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003231
3232 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003233 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3234 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003235
Chris Lattnera5bf1032005-05-12 04:49:08 +00003236 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003237 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003238}
Misha Brukman835702a2005-04-21 22:36:52 +00003239
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003240
3241
Chris Lattnerdc750592005-01-07 07:47:09 +00003242/// ExpandOp - Expand the specified SDOperand into its two component pieces
3243/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3244/// LegalizeNodes map is filled in for any results that are not expanded, the
3245/// ExpandedNodes map is filled in for any results that are expanded, and the
3246/// Lo/Hi values are returned.
3247void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3248 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003249 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003250 SDNode *Node = Op.Val;
3251 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003252 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3253 "Cannot expand FP values!");
3254 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003255 "Cannot expand to FP value or to larger int value!");
3256
Chris Lattner1a570f12005-09-02 20:32:45 +00003257 // See if we already expanded it.
3258 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3259 = ExpandedNodes.find(Op);
3260 if (I != ExpandedNodes.end()) {
3261 Lo = I->second.first;
3262 Hi = I->second.second;
3263 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003264 }
3265
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003266 // Expanding to multiple registers needs to perform an optimization step, and
3267 // is not careful to avoid operations the target does not support. Make sure
3268 // that all generated operations are legalized in the next iteration.
3269 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003270
Chris Lattnerdc750592005-01-07 07:47:09 +00003271 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003272 case ISD::CopyFromReg:
3273 assert(0 && "CopyFromReg must be legal!");
3274 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003275 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3276 assert(0 && "Do not know how to expand this operator!");
3277 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003278 case ISD::UNDEF:
3279 Lo = DAG.getNode(ISD::UNDEF, NVT);
3280 Hi = DAG.getNode(ISD::UNDEF, NVT);
3281 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003282 case ISD::Constant: {
3283 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3284 Lo = DAG.getConstant(Cst, NVT);
3285 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3286 break;
3287 }
Nate Begemanae89d862005-12-07 19:48:11 +00003288 case ISD::ConstantVec: {
3289 unsigned NumElements = Node->getNumOperands();
3290 // If we only have two elements left in the constant vector, just break it
3291 // apart into the two scalar constants it contains. Otherwise, bisect the
3292 // ConstantVec, and return each half as a new ConstantVec.
3293 // FIXME: this is hard coded as big endian, it may have to change to support
3294 // SSE and Alpha MVI
3295 if (NumElements == 2) {
3296 Hi = Node->getOperand(0);
3297 Lo = Node->getOperand(1);
3298 } else {
3299 NumElements /= 2;
3300 std::vector<SDOperand> LoOps, HiOps;
3301 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3302 HiOps.push_back(Node->getOperand(I));
3303 LoOps.push_back(Node->getOperand(I+NumElements));
3304 }
3305 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3306 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3307 }
3308 break;
3309 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003310
Chris Lattner32e08b72005-03-28 22:03:13 +00003311 case ISD::BUILD_PAIR:
3312 // Legalize both operands. FIXME: in the future we should handle the case
3313 // where the two elements are not legal.
3314 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3315 Lo = LegalizeOp(Node->getOperand(0));
3316 Hi = LegalizeOp(Node->getOperand(1));
3317 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003318
3319 case ISD::SIGN_EXTEND_INREG:
3320 ExpandOp(Node->getOperand(0), Lo, Hi);
3321 // Sign extend the lo-part.
3322 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3323 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3324 TLI.getShiftAmountTy()));
3325 // sext_inreg the low part if needed.
3326 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3327 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003328
Chris Lattner55e9cde2005-05-11 04:51:16 +00003329 case ISD::CTPOP:
3330 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003331 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3332 DAG.getNode(ISD::CTPOP, NVT, Lo),
3333 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003334 Hi = DAG.getConstant(0, NVT);
3335 break;
3336
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003337 case ISD::CTLZ: {
3338 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003339 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003340 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3341 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003342 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3343 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003344 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3345 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3346
3347 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3348 Hi = DAG.getConstant(0, NVT);
3349 break;
3350 }
3351
3352 case ISD::CTTZ: {
3353 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003354 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003355 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3356 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003357 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3358 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003359 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3360 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3361
3362 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3363 Hi = DAG.getConstant(0, NVT);
3364 break;
3365 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003366
Chris Lattnerdc750592005-01-07 07:47:09 +00003367 case ISD::LOAD: {
3368 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3369 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003370 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003371
3372 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003373 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003374 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3375 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003376 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003377 //are from the same instruction?
3378 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003379
3380 // Build a factor node to remember that this load is independent of the
3381 // other one.
3382 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3383 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003384
Chris Lattnerdc750592005-01-07 07:47:09 +00003385 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003386 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003387 if (!TLI.isLittleEndian())
3388 std::swap(Lo, Hi);
3389 break;
3390 }
Nate Begemand37c1312005-11-22 18:16:00 +00003391 case ISD::VLOAD: {
3392 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3393 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3394 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3395 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3396
3397 // If we only have two elements, turn into a pair of scalar loads.
3398 // FIXME: handle case where a vector of two elements is fine, such as
3399 // 2 x double on SSE2.
3400 if (NumElements == 2) {
3401 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3402 // Increment the pointer to the other half.
3403 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3404 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3405 getIntPtrConstant(IncrementSize));
3406 //Is this safe? declaring that the two parts of the split load
3407 //are from the same instruction?
3408 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3409 } else {
3410 NumElements /= 2; // Split the vector in half
3411 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3412 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3413 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3414 getIntPtrConstant(IncrementSize));
3415 //Is this safe? declaring that the two parts of the split load
3416 //are from the same instruction?
3417 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3418 }
3419
3420 // Build a factor node to remember that this load is independent of the
3421 // other one.
3422 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3423 Hi.getValue(1));
3424
3425 // Remember that we legalized the chain.
3426 AddLegalizedOperand(Op.getValue(1), TF);
3427 if (!TLI.isLittleEndian())
3428 std::swap(Lo, Hi);
3429 break;
3430 }
3431 case ISD::VADD:
3432 case ISD::VSUB:
3433 case ISD::VMUL: {
3434 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3435 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3436 SDOperand LL, LH, RL, RH;
3437
3438 ExpandOp(Node->getOperand(0), LL, LH);
3439 ExpandOp(Node->getOperand(1), RL, RH);
3440
3441 // If we only have two elements, turn into a pair of scalar loads.
3442 // FIXME: handle case where a vector of two elements is fine, such as
3443 // 2 x double on SSE2.
3444 if (NumElements == 2) {
3445 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3446 Lo = DAG.getNode(Opc, EVT, LL, RL);
3447 Hi = DAG.getNode(Opc, EVT, LH, RH);
3448 } else {
3449 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3450 LL.getOperand(3));
3451 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3452 LH.getOperand(3));
3453 }
3454 break;
3455 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003456 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003457 case ISD::CALL: {
3458 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3459 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3460
Chris Lattner3d95c142005-01-19 20:24:35 +00003461 bool Changed = false;
3462 std::vector<SDOperand> Ops;
3463 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3464 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3465 Changed |= Ops.back() != Node->getOperand(i);
3466 }
3467
Chris Lattnerdc750592005-01-07 07:47:09 +00003468 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3469 "Can only expand a call once so far, not i64 -> i16!");
3470
3471 std::vector<MVT::ValueType> RetTyVTs;
3472 RetTyVTs.reserve(3);
3473 RetTyVTs.push_back(NVT);
3474 RetTyVTs.push_back(NVT);
3475 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003476 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3477 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003478 Lo = SDOperand(NC, 0);
3479 Hi = SDOperand(NC, 1);
3480
3481 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003482 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003483 break;
3484 }
3485 case ISD::AND:
3486 case ISD::OR:
3487 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3488 SDOperand LL, LH, RL, RH;
3489 ExpandOp(Node->getOperand(0), LL, LH);
3490 ExpandOp(Node->getOperand(1), RL, RH);
3491 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3492 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3493 break;
3494 }
3495 case ISD::SELECT: {
3496 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003497
3498 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3499 case Expand: assert(0 && "It's impossible to expand bools");
3500 case Legal:
3501 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3502 break;
3503 case Promote:
3504 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3505 break;
3506 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003507 ExpandOp(Node->getOperand(1), LL, LH);
3508 ExpandOp(Node->getOperand(2), RL, RH);
3509 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3510 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3511 break;
3512 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003513 case ISD::SELECT_CC: {
3514 SDOperand TL, TH, FL, FH;
3515 ExpandOp(Node->getOperand(2), TL, TH);
3516 ExpandOp(Node->getOperand(3), FL, FH);
3517 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3518 Node->getOperand(1), TL, FL, Node->getOperand(4));
3519 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3520 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003521 Lo = LegalizeOp(Lo);
3522 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003523 break;
3524 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003525 case ISD::SEXTLOAD: {
3526 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3527 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3528 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3529
3530 if (EVT == NVT)
3531 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3532 else
3533 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3534 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003535
3536 // Remember that we legalized the chain.
3537 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3538
Nate Begemanc3a89c52005-10-13 17:15:37 +00003539 // The high part is obtained by SRA'ing all but one of the bits of the lo
3540 // part.
3541 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3542 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3543 TLI.getShiftAmountTy()));
3544 Lo = LegalizeOp(Lo);
3545 Hi = LegalizeOp(Hi);
3546 break;
3547 }
3548 case ISD::ZEXTLOAD: {
3549 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3550 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3551 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3552
3553 if (EVT == NVT)
3554 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3555 else
3556 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3557 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003558
3559 // Remember that we legalized the chain.
3560 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3561
Nate Begemanc3a89c52005-10-13 17:15:37 +00003562 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003563 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003564 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003565 break;
3566 }
3567 case ISD::EXTLOAD: {
3568 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3569 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3570 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3571
3572 if (EVT == NVT)
3573 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3574 else
3575 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3576 EVT);
3577
3578 // Remember that we legalized the chain.
3579 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3580
3581 // The high part is undefined.
3582 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3583 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003584 break;
3585 }
Chris Lattner7753f172005-09-02 00:18:10 +00003586 case ISD::ANY_EXTEND: {
3587 SDOperand In;
3588 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3589 case Expand: assert(0 && "expand-expand not implemented yet!");
3590 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3591 case Promote:
3592 In = PromoteOp(Node->getOperand(0));
3593 break;
3594 }
3595
3596 // The low part is any extension of the input (which degenerates to a copy).
3597 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3598 // The high part is undefined.
3599 Hi = DAG.getNode(ISD::UNDEF, NVT);
3600 break;
3601 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003602 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003603 SDOperand In;
3604 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3605 case Expand: assert(0 && "expand-expand not implemented yet!");
3606 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3607 case Promote:
3608 In = PromoteOp(Node->getOperand(0));
3609 // Emit the appropriate sign_extend_inreg to get the value we want.
3610 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003611 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003612 break;
3613 }
3614
Chris Lattnerdc750592005-01-07 07:47:09 +00003615 // The low part is just a sign extension of the input (which degenerates to
3616 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003617 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003618
Chris Lattnerdc750592005-01-07 07:47:09 +00003619 // The high part is obtained by SRA'ing all but one of the bits of the lo
3620 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003621 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003622 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3623 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003624 break;
3625 }
Chris Lattner47844892005-04-03 23:41:52 +00003626 case ISD::ZERO_EXTEND: {
3627 SDOperand In;
3628 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3629 case Expand: assert(0 && "expand-expand not implemented yet!");
3630 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3631 case Promote:
3632 In = PromoteOp(Node->getOperand(0));
3633 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003634 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003635 break;
3636 }
3637
Chris Lattnerdc750592005-01-07 07:47:09 +00003638 // The low part is just a zero extension of the input (which degenerates to
3639 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003640 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003641
Chris Lattnerdc750592005-01-07 07:47:09 +00003642 // The high part is just a zero.
3643 Hi = DAG.getConstant(0, NVT);
3644 break;
Chris Lattner47844892005-04-03 23:41:52 +00003645 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003646
Chris Lattner44c28c22005-11-20 22:56:56 +00003647 case ISD::READCYCLECOUNTER: {
3648 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3649 TargetLowering::Custom &&
3650 "Must custom expand ReadCycleCounter");
3651 SDOperand T = TLI.LowerOperation(Op, DAG);
3652 assert(T.Val && "Node must be custom expanded!");
3653 Lo = LegalizeOp(T.getValue(0));
3654 Hi = LegalizeOp(T.getValue(1));
3655 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3656 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003657 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003658 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003659
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003660 // These operators cannot be expanded directly, emit them as calls to
3661 // library functions.
3662 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003663 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003664 SDOperand Op;
3665 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3666 case Expand: assert(0 && "cannot expand FP!");
3667 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3668 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3669 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003670
Chris Lattner941d84a2005-07-30 01:40:57 +00003671 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3672
Chris Lattnerfe68d752005-07-29 00:33:32 +00003673 // Now that the custom expander is done, expand the result, which is still
3674 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003675 if (Op.Val) {
3676 ExpandOp(Op, Lo, Hi);
3677 break;
3678 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003679 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003680
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003681 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003682 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003683 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003684 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003685 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003686
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003687 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003688 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3689 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3690 LegalizeOp(Node->getOperand(0)));
3691 // Now that the custom expander is done, expand the result, which is still
3692 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003693 Op = TLI.LowerOperation(Op, DAG);
3694 if (Op.Val) {
3695 ExpandOp(Op, Lo, Hi);
3696 break;
3697 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003698 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003699
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003700 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003701 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003702 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003703 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003704 break;
3705
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003706 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003707 // If the target wants custom lowering, do so.
3708 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3709 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3710 LegalizeOp(Node->getOperand(1)));
3711 Op = TLI.LowerOperation(Op, DAG);
3712 if (Op.Val) {
3713 // Now that the custom expander is done, expand the result, which is
3714 // still VT.
3715 ExpandOp(Op, Lo, Hi);
3716 break;
3717 }
3718 }
3719
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003720 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003721 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003722 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003723
3724 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003725 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003726 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3727 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003728 break;
3729 }
3730
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003731 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003732 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003733 break;
3734
3735 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003736 // If the target wants custom lowering, do so.
3737 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3738 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3739 LegalizeOp(Node->getOperand(1)));
3740 Op = TLI.LowerOperation(Op, DAG);
3741 if (Op.Val) {
3742 // Now that the custom expander is done, expand the result, which is
3743 // still VT.
3744 ExpandOp(Op, Lo, Hi);
3745 break;
3746 }
3747 }
3748
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003749 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003750 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003751 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003752
3753 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003754 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003755 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3756 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003757 break;
3758 }
3759
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003760 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003761 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003762 break;
3763 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003764 // If the target wants custom lowering, do so.
3765 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3766 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3767 LegalizeOp(Node->getOperand(1)));
3768 Op = TLI.LowerOperation(Op, DAG);
3769 if (Op.Val) {
3770 // Now that the custom expander is done, expand the result, which is
3771 // still VT.
3772 ExpandOp(Op, Lo, Hi);
3773 break;
3774 }
3775 }
3776
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003777 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003778 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003779 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003780
3781 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003782 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003783 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3784 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003785 break;
3786 }
3787
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003788 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003789 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003790 break;
3791
Misha Brukman835702a2005-04-21 22:36:52 +00003792 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003793 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3794 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003795 break;
3796 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003797 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3798 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003799 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003800 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003801 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003802 SDOperand LL, LH, RL, RH;
3803 ExpandOp(Node->getOperand(0), LL, LH);
3804 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003805 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3806 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3807 // extended the sign bit of the low half through the upper half, and if so
3808 // emit a MULHS instead of the alternate sequence that is valid for any
3809 // i64 x i64 multiply.
3810 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3811 // is RH an extension of the sign bit of RL?
3812 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3813 RH.getOperand(1).getOpcode() == ISD::Constant &&
3814 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3815 // is LH an extension of the sign bit of LL?
3816 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3817 LH.getOperand(1).getOpcode() == ISD::Constant &&
3818 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3819 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3820 } else {
3821 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3822 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3823 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3824 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3825 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3826 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003827 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3828 } else {
3829 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3830 }
3831 break;
3832 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003833 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3834 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3835 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3836 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003837 }
3838
3839 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003840 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3841 std::make_pair(Lo, Hi))).second;
3842 assert(isNew && "Value already expanded?!?");
Chris Lattner2af3ee42005-12-20 00:53:54 +00003843
Chris Lattnerac12f682005-12-21 18:02:52 +00003844 // Make sure the resultant values have been legalized themselves, unless this
3845 // is a type that requires multi-step expansion.
3846 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
3847 Lo = LegalizeOp(Lo);
3848 Hi = LegalizeOp(Hi);
3849 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003850}
3851
3852
3853// SelectionDAG::Legalize - This is the entry point for the file.
3854//
Chris Lattner4add7e32005-01-23 04:42:50 +00003855void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003856 /// run - This is the main entry point to this class.
3857 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003858 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003859}
3860