blob: 7903fa68d7cac16540776d849e54160c502d2a4d [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
Chris Lattner36e663d2005-12-23 00:16:34 +0000133 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000134 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
135 SDOperand LegalOp,
136 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000137 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
138 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000139 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
140 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000141
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000142 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
143 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000144 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
145 SDOperand &Lo, SDOperand &Hi);
146 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000147 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000148
Chris Lattnera5bf1032005-05-12 04:49:08 +0000149 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
150
Chris Lattnerdc750592005-01-07 07:47:09 +0000151 SDOperand getIntPtrConstant(uint64_t Val) {
152 return DAG.getConstant(Val, TLI.getPointerTy());
153 }
154};
155}
156
Chris Lattner301015a2005-11-19 05:51:46 +0000157static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000158 switch (VecOp) {
159 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-11-19 00:36:38 +0000160 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
161 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
162 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
163 }
164}
Chris Lattnerdc750592005-01-07 07:47:09 +0000165
Chris Lattner4add7e32005-01-23 04:42:50 +0000166SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
167 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
168 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000169 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000170 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000171}
172
Jim Laskeyf2516a92005-08-17 00:39:29 +0000173/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
174/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000175/// we expand it. At this point, we know that the result and operand types are
176/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000177SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
178 SDOperand Op0,
179 MVT::ValueType DestVT) {
180 if (Op0.getValueType() == MVT::i32) {
181 // simple 32-bit [signed|unsigned] integer to float/double expansion
182
183 // get the stack frame index of a 8 byte buffer
184 MachineFunction &MF = DAG.getMachineFunction();
185 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
186 // get address of 8 byte buffer
187 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
188 // word offset constant for Hi/Lo address computation
189 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
190 // set up Hi and Lo (into buffer) address based on endian
191 SDOperand Hi, Lo;
192 if (TLI.isLittleEndian()) {
193 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
194 Lo = StackSlot;
195 } else {
196 Hi = StackSlot;
197 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
198 }
199 // if signed map to unsigned space
200 SDOperand Op0Mapped;
201 if (isSigned) {
202 // constant used to invert sign bit (signed to unsigned mapping)
203 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
204 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
205 } else {
206 Op0Mapped = Op0;
207 }
208 // store the lo of the constructed double - based on integer input
209 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
210 Op0Mapped, Lo, DAG.getSrcValue(NULL));
211 // initial hi portion of constructed double
212 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
213 // store the hi of the constructed double - biased exponent
214 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
215 InitialHi, Hi, DAG.getSrcValue(NULL));
216 // load the constructed double
217 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
218 DAG.getSrcValue(NULL));
219 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000220 SDOperand Bias = DAG.getConstantFP(isSigned ?
221 BitsToDouble(0x4330000080000000ULL)
222 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000223 MVT::f64);
224 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000225 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000226 // final result
227 SDOperand Result;
228 // handle final rounding
229 if (DestVT == MVT::f64) {
230 // do nothing
231 Result = Sub;
232 } else {
233 // if f32 then cast to f32
234 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
235 }
Chris Lattner2af3ee42005-12-20 00:53:54 +0000236 return LegalizeOp(Result);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000237 }
238 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000239 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000240
Chris Lattnerd47675e2005-08-09 20:20:18 +0000241 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
242 DAG.getConstant(0, Op0.getValueType()),
243 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000244 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
245 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
246 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000247
Jim Laskeyf2516a92005-08-17 00:39:29 +0000248 // If the sign bit of the integer is set, the large number will be treated
249 // as a negative number. To counteract this, the dynamic code adds an
250 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000251 uint64_t FF;
252 switch (Op0.getValueType()) {
253 default: assert(0 && "Unsupported integer type!");
254 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
255 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
256 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
257 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
258 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000259 if (TLI.isLittleEndian()) FF <<= 32;
260 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000261
Chris Lattnerc30405e2005-08-26 17:15:30 +0000262 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-07-16 00:19:57 +0000263 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
264 SDOperand FudgeInReg;
265 if (DestVT == MVT::f32)
266 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
267 DAG.getSrcValue(NULL));
268 else {
269 assert(DestVT == MVT::f64 && "Unexpected conversion");
270 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
271 DAG.getEntryNode(), CPIdx,
272 DAG.getSrcValue(NULL), MVT::f32));
273 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000274
Chris Lattner2af3ee42005-12-20 00:53:54 +0000275 return LegalizeOp(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000276}
277
Chris Lattner19732782005-08-16 18:17:10 +0000278/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000279/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000280/// we promote it. At this point, we know that the result and operand types are
281/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
282/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000283SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
284 MVT::ValueType DestVT,
285 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000286 // First step, figure out the appropriate *INT_TO_FP operation to use.
287 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000288
Chris Lattnere3e847b2005-07-16 00:19:57 +0000289 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000290
Chris Lattnere3e847b2005-07-16 00:19:57 +0000291 // Scan for the appropriate larger type to use.
292 while (1) {
293 NewInTy = (MVT::ValueType)(NewInTy+1);
294 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000295
Chris Lattnere3e847b2005-07-16 00:19:57 +0000296 // If the target supports SINT_TO_FP of this type, use it.
297 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
298 default: break;
299 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000300 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000301 break; // Can't use this datatype.
302 // FALL THROUGH.
303 case TargetLowering::Custom:
304 OpToUse = ISD::SINT_TO_FP;
305 break;
306 }
307 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000308 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000309
Chris Lattnere3e847b2005-07-16 00:19:57 +0000310 // If the target supports UINT_TO_FP of this type, use it.
311 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
312 default: break;
313 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000314 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000315 break; // Can't use this datatype.
316 // FALL THROUGH.
317 case TargetLowering::Custom:
318 OpToUse = ISD::UINT_TO_FP;
319 break;
320 }
321 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000322
Chris Lattnere3e847b2005-07-16 00:19:57 +0000323 // Otherwise, try a larger type.
324 }
325
Chris Lattnere3e847b2005-07-16 00:19:57 +0000326 // Okay, we found the operation and type to use. Zero extend our input to the
327 // desired type then run the operation on it.
Chris Lattner2af3ee42005-12-20 00:53:54 +0000328 SDOperand N = DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattner2af3ee42005-12-20 00:53:54 +0000331 // Make sure to legalize any nodes we create here.
332 return LegalizeOp(N);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000333}
334
Chris Lattner44fe26f2005-07-29 00:11:56 +0000335/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
336/// FP_TO_*INT operation of the specified operand when the target requests that
337/// we promote it. At this point, we know that the result and operand types are
338/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
339/// operation that returns a larger result.
340SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
341 MVT::ValueType DestVT,
342 bool isSigned) {
343 // First step, figure out the appropriate FP_TO*INT operation to use.
344 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-07-29 00:11:56 +0000346 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000347
Chris Lattner44fe26f2005-07-29 00:11:56 +0000348 // Scan for the appropriate larger type to use.
349 while (1) {
350 NewOutTy = (MVT::ValueType)(NewOutTy+1);
351 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000352
Chris Lattner44fe26f2005-07-29 00:11:56 +0000353 // If the target supports FP_TO_SINT returning this type, use it.
354 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
355 default: break;
356 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000357 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000358 break; // Can't use this datatype.
359 // FALL THROUGH.
360 case TargetLowering::Custom:
361 OpToUse = ISD::FP_TO_SINT;
362 break;
363 }
364 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000365
Chris Lattner44fe26f2005-07-29 00:11:56 +0000366 // If the target supports FP_TO_UINT of this type, use it.
367 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
368 default: break;
369 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000370 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000371 break; // Can't use this datatype.
372 // FALL THROUGH.
373 case TargetLowering::Custom:
374 OpToUse = ISD::FP_TO_UINT;
375 break;
376 }
377 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000378
Chris Lattner44fe26f2005-07-29 00:11:56 +0000379 // Otherwise, try a larger type.
380 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000381
Chris Lattner44fe26f2005-07-29 00:11:56 +0000382 // Okay, we found the operation and type to use. Truncate the result of the
383 // extended FP_TO_*INT operation to the desired size.
Chris Lattner2af3ee42005-12-20 00:53:54 +0000384 SDOperand N = DAG.getNode(ISD::TRUNCATE, DestVT,
385 DAG.getNode(OpToUse, NewOutTy, LegalOp));
386 // Make sure to legalize any nodes we create here in the next pass.
387 return LegalizeOp(N);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000388}
389
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000390/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
391/// not been visited yet and if all of its operands have already been visited.
392static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
393 std::map<SDNode*, unsigned> &Visited) {
394 if (++Visited[N] != N->getNumOperands())
395 return; // Haven't visited all operands yet
396
397 Order.push_back(N);
398
399 if (N->hasOneUse()) { // Tail recurse in common case.
400 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
401 return;
402 }
403
404 // Now that we have N in, add anything that uses it if all of their operands
405 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000406 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
407 ComputeTopDownOrdering(*UI, Order, Visited);
408}
409
Chris Lattner44fe26f2005-07-29 00:11:56 +0000410
Chris Lattnerdc750592005-01-07 07:47:09 +0000411void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000412 // The legalize process is inherently a bottom-up recursive process (users
413 // legalize their uses before themselves). Given infinite stack space, we
414 // could just start legalizing on the root and traverse the whole graph. In
415 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000416 // blocks. To avoid this problem, compute an ordering of the nodes where each
417 // node is only legalized after all of its operands are legalized.
418 std::map<SDNode*, unsigned> Visited;
419 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000420
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000421 // Compute ordering from all of the leaves in the graphs, those (like the
422 // entry node) that have no operands.
423 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
424 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000425 if (I->getNumOperands() == 0) {
426 Visited[I] = 0 - 1U;
427 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000428 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000429 }
430
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000431 assert(Order.size() == Visited.size() &&
432 Order.size() ==
433 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000434 "Error: DAG is cyclic!");
435 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000436
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000437 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
438 SDNode *N = Order[i];
439 switch (getTypeAction(N->getValueType(0))) {
440 default: assert(0 && "Bad type action!");
441 case Legal:
442 LegalizeOp(SDOperand(N, 0));
443 break;
444 case Promote:
445 PromoteOp(SDOperand(N, 0));
446 break;
447 case Expand: {
448 SDOperand X, Y;
449 ExpandOp(SDOperand(N, 0), X, Y);
450 break;
451 }
452 }
453 }
454
455 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000456 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000457 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
458 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000459
460 ExpandedNodes.clear();
461 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000462 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000463
464 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000465 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000466}
467
468SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000469 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000470 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000471 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000472
Chris Lattnerdc750592005-01-07 07:47:09 +0000473 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000474 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000475 if (Node->getNumValues() > 1) {
476 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
477 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000478 case Legal: break; // Nothing to do.
479 case Expand: {
480 SDOperand T1, T2;
481 ExpandOp(Op.getValue(i), T1, T2);
482 assert(LegalizedNodes.count(Op) &&
483 "Expansion didn't add legal operands!");
484 return LegalizedNodes[Op];
485 }
486 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000487 PromoteOp(Op.getValue(i));
488 assert(LegalizedNodes.count(Op) &&
489 "Expansion didn't add legal operands!");
490 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000491 }
492 }
493
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000494 // Note that LegalizeOp may be reentered even from single-use nodes, which
495 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000496 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
497 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000498
Nate Begemane5b86d72005-08-10 20:51:12 +0000499 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000500
501 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000502
503 switch (Node->getOpcode()) {
504 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000505 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
506 // If this is a target node, legalize it by legalizing the operands then
507 // passing it through.
508 std::vector<SDOperand> Ops;
509 bool Changed = false;
510 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
511 Ops.push_back(LegalizeOp(Node->getOperand(i)));
512 Changed = Changed || Node->getOperand(i) != Ops.back();
513 }
514 if (Changed)
515 if (Node->getNumValues() == 1)
516 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
517 else {
518 std::vector<MVT::ValueType> VTs(Node->value_begin(),
519 Node->value_end());
520 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
521 }
522
523 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
524 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
525 return Result.getValue(Op.ResNo);
526 }
527 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000528 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
529 assert(0 && "Do not know how to legalize this operator!");
530 abort();
531 case ISD::EntryToken:
532 case ISD::FrameIndex:
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000533 case ISD::TargetFrameIndex:
534 case ISD::Register:
535 case ISD::TargetConstant:
Nate Begeman4e56db62005-12-10 02:36:00 +0000536 case ISD::TargetConstantPool:
Chris Lattnerdc750592005-01-07 07:47:09 +0000537 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000538 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000539 case ISD::ExternalSymbol:
Andrew Lenharth72594262005-12-24 23:42:32 +0000540 case ISD::TargetExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000541 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000542 case ISD::BasicBlock:
543 case ISD::CONDCODE:
544 case ISD::VALUETYPE:
545 case ISD::SRCVALUE:
Chris Lattner435b4022005-11-29 06:21:05 +0000546 case ISD::STRING:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000547 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
548 default: assert(0 && "This action is not supported yet!");
549 case TargetLowering::Custom: {
550 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
551 if (Tmp.Val) {
552 Result = LegalizeOp(Tmp);
553 break;
554 }
555 } // FALLTHROUGH if the target doesn't want to lower this op after all.
556 case TargetLowering::Legal:
557 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
558 break;
559 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000560 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000561 case ISD::AssertSext:
562 case ISD::AssertZext:
563 Tmp1 = LegalizeOp(Node->getOperand(0));
564 if (Tmp1 != Node->getOperand(0))
565 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
566 Node->getOperand(1));
567 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000568 case ISD::MERGE_VALUES:
569 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000570 case ISD::CopyFromReg:
571 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000572 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000573 if (Node->getNumValues() == 2) {
Chris Lattnere3c67e92005-12-18 15:27:43 +0000574 if (Tmp1 != Node->getOperand(0))
575 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattner33182322005-08-16 21:55:35 +0000576 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattnere3c67e92005-12-18 15:27:43 +0000577 Node->getValueType(0));
578 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000579 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
580 if (Node->getNumOperands() == 3)
581 Tmp2 = LegalizeOp(Node->getOperand(2));
582 if (Tmp1 != Node->getOperand(0) ||
583 (Node->getNumOperands() == 3 && Tmp2 != Node->getOperand(2)))
Chris Lattnere3c67e92005-12-18 15:27:43 +0000584 Result = DAG.getCopyFromReg(Tmp1,
585 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
586 Node->getValueType(0), Tmp2);
587 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
588 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000589 // Since CopyFromReg produces two values, make sure to remember that we
590 // legalized both of them.
591 AddLegalizedOperand(Op.getValue(0), Result);
592 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
593 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000594 case ISD::UNDEF: {
595 MVT::ValueType VT = Op.getValueType();
596 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000597 default: assert(0 && "This action is not supported yet!");
598 case TargetLowering::Expand:
599 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000600 if (MVT::isInteger(VT))
601 Result = DAG.getConstant(0, VT);
602 else if (MVT::isFloatingPoint(VT))
603 Result = DAG.getConstantFP(0, VT);
604 else
605 assert(0 && "Unknown value type!");
606 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000607 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000608 break;
609 }
610 break;
611 }
Chris Lattner435b4022005-11-29 06:21:05 +0000612
613 case ISD::LOCATION:
614 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
615 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
616
617 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
618 case TargetLowering::Promote:
619 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000620 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000621 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000622 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
623 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
624
625 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
626 const std::string &FName =
627 cast<StringSDNode>(Node->getOperand(3))->getValue();
628 const std::string &DirName =
629 cast<StringSDNode>(Node->getOperand(4))->getValue();
630 unsigned SrcFile = DebugInfo->getUniqueSourceID(FName, DirName);
631
Jim Laskey9e296be2005-12-21 20:51:37 +0000632 std::vector<SDOperand> Ops;
633 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000634 SDOperand LineOp = Node->getOperand(1);
635 SDOperand ColOp = Node->getOperand(2);
636
637 if (useDEBUG_LOC) {
638 Ops.push_back(LineOp); // line #
639 Ops.push_back(ColOp); // col #
640 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
641 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
642 } else {
643 unsigned Line = dyn_cast<ConstantSDNode>(LineOp)->getValue();
644 unsigned Col = dyn_cast<ConstantSDNode>(ColOp)->getValue();
645 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
646 Ops.push_back(DAG.getConstant(ID, MVT::i32));
647 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
648 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000649 } else {
650 Result = Tmp1; // chain
651 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000652 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner435b4022005-11-29 06:21:05 +0000653 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000654 }
Chris Lattner435b4022005-11-29 06:21:05 +0000655 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000656 if (Tmp1 != Node->getOperand(0) ||
657 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000658 std::vector<SDOperand> Ops;
659 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000660 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
661 Ops.push_back(Node->getOperand(1)); // line # must be legal.
662 Ops.push_back(Node->getOperand(2)); // col # must be legal.
663 } else {
664 // Otherwise promote them.
665 Ops.push_back(PromoteOp(Node->getOperand(1)));
666 Ops.push_back(PromoteOp(Node->getOperand(2)));
667 }
Chris Lattner435b4022005-11-29 06:21:05 +0000668 Ops.push_back(Node->getOperand(3)); // filename must be legal.
669 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
670 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
671 }
672 break;
673 }
674 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000675
676 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000677 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000678 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
679 case TargetLowering::Promote:
680 case TargetLowering::Expand:
681 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000682 case TargetLowering::Legal:
683 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
684 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
685 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
686 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
687
688 if (Tmp1 != Node->getOperand(0) ||
689 Tmp2 != Node->getOperand(1) ||
690 Tmp3 != Node->getOperand(2) ||
691 Tmp4 != Node->getOperand(3)) {
692 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
693 }
694 break;
695 }
696 break;
697
698 case ISD::DEBUG_LABEL:
699 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
700 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
701 case TargetLowering::Promote:
702 case TargetLowering::Expand:
703 default: assert(0 && "This action is not supported yet!");
704 case TargetLowering::Legal:
705 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
706 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
707
708 if (Tmp1 != Node->getOperand(0) ||
709 Tmp2 != Node->getOperand(1)) {
710 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000711 }
712 break;
713 }
714 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000715
Chris Lattnerdc750592005-01-07 07:47:09 +0000716 case ISD::Constant:
717 // We know we don't need to expand constants here, constants only have one
718 // value and we check that it is fine above.
719
720 // FIXME: Maybe we should handle things like targets that don't support full
721 // 32-bit immediates?
722 break;
723 case ISD::ConstantFP: {
724 // Spill FP immediates to the constant pool if the target cannot directly
725 // codegen them. Targets often have some immediate values that can be
726 // efficiently generated into an FP register without a load. We explicitly
727 // leave these constants as ConstantFP nodes for the target to deal with.
728
729 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
730
731 // Check to see if this FP immediate is already legal.
732 bool isLegal = false;
733 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
734 E = TLI.legal_fpimm_end(); I != E; ++I)
735 if (CFP->isExactlyValue(*I)) {
736 isLegal = true;
737 break;
738 }
739
740 if (!isLegal) {
741 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000742 bool Extend = false;
743
744 // If a FP immediate is precise when represented as a float, we put it
745 // into the constant pool as a float, even if it's is statically typed
746 // as a double.
747 MVT::ValueType VT = CFP->getValueType(0);
748 bool isDouble = VT == MVT::f64;
749 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
750 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000751 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
752 // Only do this if the target has a native EXTLOAD instruction from
753 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000754 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000755 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
756 VT = MVT::f32;
757 Extend = true;
758 }
Misha Brukman835702a2005-04-21 22:36:52 +0000759
Nate Begeman4e56db62005-12-10 02:36:00 +0000760 SDOperand CPIdx =
761 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000762 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000763 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
764 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000765 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000766 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
767 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000768 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000769 }
770 break;
771 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000772 case ISD::ConstantVec: {
773 // We assume that vector constants are not legal, and will be immediately
774 // spilled to the constant pool.
775 //
776 // FIXME: revisit this when we have some kind of mechanism by which targets
777 // can decided legality of vector constants, of which there may be very
778 // many.
779 //
780 // Create a ConstantPacked, and put it in the constant pool.
781 std::vector<Constant*> CV;
782 MVT::ValueType VT = Node->getValueType(0);
783 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
784 SDOperand OpN = Node->getOperand(I);
785 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
786 if (MVT::isFloatingPoint(VT))
787 CV.push_back(ConstantFP::get(OpNTy,
788 cast<ConstantFPSDNode>(OpN)->getValue()));
789 else
790 CV.push_back(ConstantUInt::get(OpNTy,
791 cast<ConstantSDNode>(OpN)->getValue()));
792 }
793 Constant *CP = ConstantPacked::get(CV);
Nate Begeman956aef42005-12-13 03:03:23 +0000794 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000795 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
796 break;
797 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000798 case ISD::TokenFactor:
799 if (Node->getNumOperands() == 2) {
800 bool Changed = false;
801 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
802 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
803 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
804 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
805 } else {
806 std::vector<SDOperand> Ops;
807 bool Changed = false;
808 // Legalize the operands.
809 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
810 SDOperand Op = Node->getOperand(i);
811 Ops.push_back(LegalizeOp(Op));
812 Changed |= Ops[i] != Op;
813 }
814 if (Changed)
815 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000816 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000817 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000818
Chris Lattner2dce7032005-05-12 23:24:06 +0000819 case ISD::CALLSEQ_START:
820 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000821 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000822 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000823 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000824 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000825 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000826
Chris Lattner2dce7032005-05-12 23:24:06 +0000827 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000828 // nodes are treated specially and are mutated in place. This makes the dag
829 // legalization process more efficient and also makes libcall insertion
830 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000831 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000832 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +0000833 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
834 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
835 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
836 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000837 Tmp3 != Node->getOperand(2)) {
838 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
839 std::vector<SDOperand> Ops;
840 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
841 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
842 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000843 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000844
Chris Lattner2d591422006-01-15 08:43:08 +0000845 Tmp1 = Result;
846 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +0000847 switch (TLI.getOperationAction(Node->getOpcode(),
848 Node->getValueType(0))) {
849 default: assert(0 && "This action is not supported yet!");
850 case TargetLowering::Custom: {
Chris Lattner2d591422006-01-15 08:43:08 +0000851 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
852 if (Tmp3.Val) {
853 Tmp1 = LegalizeOp(Tmp3);
854 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +0000855 }
856 // FALLTHROUGH if the target thinks it is legal.
857 }
858 case TargetLowering::Legal:
859 // Since this op produce two values, make sure to remember that we
860 // legalized both of them.
Chris Lattner2d591422006-01-15 08:43:08 +0000861 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
862 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
863 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000864 }
865 assert(0 && "Unreachable");
866 }
Chris Lattnerd0feb642005-05-13 18:43:43 +0000867 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000868 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000869 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
870 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000871
872 bool Changed = false;
873 std::vector<SDOperand> Ops;
874 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
875 Ops.push_back(LegalizeOp(Node->getOperand(i)));
876 Changed |= Ops.back() != Node->getOperand(i);
877 }
878
879 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000880 std::vector<MVT::ValueType> RetTyVTs;
881 RetTyVTs.reserve(Node->getNumValues());
882 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000883 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000884 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
885 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000886 } else {
887 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000888 }
Chris Lattner9242c502005-01-09 19:43:23 +0000889 // Since calls produce multiple values, make sure to remember that we
890 // legalized all of them.
891 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
892 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
893 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000894 }
Chris Lattner68a12142005-01-07 22:12:08 +0000895 case ISD::BR:
896 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
897 if (Tmp1 != Node->getOperand(0))
898 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
899 break;
900
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000901 case ISD::BRCOND:
902 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000903
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000904 switch (getTypeAction(Node->getOperand(1).getValueType())) {
905 case Expand: assert(0 && "It's impossible to expand bools");
906 case Legal:
907 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
908 break;
909 case Promote:
910 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
911 break;
912 }
Nate Begeman371e4952005-08-16 19:49:35 +0000913
914 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
915 default: assert(0 && "This action is not supported yet!");
916 case TargetLowering::Expand:
917 // Expand brcond's setcc into its constituent parts and create a BR_CC
918 // Node.
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(2));
923 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000924 // Make sure the condition is either zero or one. It may have been
925 // promoted from something else.
926 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
927
Nate Begeman371e4952005-08-16 19:49:35 +0000928 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
929 DAG.getCondCode(ISD::SETNE), Tmp2,
930 DAG.getConstant(0, Tmp2.getValueType()),
931 Node->getOperand(2));
932 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000933 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +0000934 break;
Evan Cheng6fc31042005-12-19 23:12:38 +0000935 case TargetLowering::Custom: {
936 SDOperand Tmp =
937 TLI.LowerOperation(DAG.getNode(ISD::BRCOND, Node->getValueType(0),
938 Tmp1, Tmp2, Node->getOperand(2)), DAG);
939 if (Tmp.Val) {
940 Result = LegalizeOp(Tmp);
941 break;
942 }
943 // FALLTHROUGH if the target thinks it is legal.
944 }
Nate Begeman371e4952005-08-16 19:49:35 +0000945 case TargetLowering::Legal:
946 // Basic block destination (Op#2) is always legal.
947 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
948 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
949 Node->getOperand(2));
950 break;
951 }
952 break;
953 case ISD::BR_CC:
954 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000955 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000956 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
957 Node->getOperand(2), // LHS
958 Node->getOperand(3), // RHS
959 Node->getOperand(1)));
960 // If we get a SETCC back from legalizing the SETCC node we just
961 // created, then use its LHS, RHS, and CC directly in creating a new
962 // node. Otherwise, select between the true and false value based on
963 // comparing the result of the legalized with zero.
964 if (Tmp2.getOpcode() == ISD::SETCC) {
965 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
966 Tmp2.getOperand(0), Tmp2.getOperand(1),
967 Node->getOperand(4));
968 } else {
969 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
970 DAG.getCondCode(ISD::SETNE),
971 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
972 Node->getOperand(4));
973 }
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000974 break;
975 }
976
977 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
978 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
979
980 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
981 default: assert(0 && "Unexpected action for BR_CC!");
982 case TargetLowering::Custom: {
983 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
984 Tmp2, Tmp3, Node->getOperand(4));
985 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
986 if (Tmp4.Val) {
987 Result = LegalizeOp(Tmp4);
988 break;
989 }
990 } // FALLTHROUGH if the target doesn't want to lower this op after all.
991 case TargetLowering::Legal:
992 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
993 Tmp3 != Node->getOperand(3)) {
994 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
995 Tmp2, Tmp3, Node->getOperand(4));
996 }
997 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000998 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000999 break;
Chris Lattnerfd986782005-04-09 03:30:19 +00001000 case ISD::BRCONDTWOWAY:
1001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1002 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1003 case Expand: assert(0 && "It's impossible to expand bools");
1004 case Legal:
1005 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1006 break;
1007 case Promote:
1008 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1009 break;
1010 }
1011 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
1012 // pair.
1013 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
1014 case TargetLowering::Promote:
1015 default: assert(0 && "This action is not supported yet!");
1016 case TargetLowering::Legal:
1017 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1018 std::vector<SDOperand> Ops;
1019 Ops.push_back(Tmp1);
1020 Ops.push_back(Tmp2);
1021 Ops.push_back(Node->getOperand(2));
1022 Ops.push_back(Node->getOperand(3));
1023 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
1024 }
1025 break;
1026 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +00001027 // If BRTWOWAY_CC is legal for this target, then simply expand this node
1028 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
1029 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001030 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +00001031 if (Tmp2.getOpcode() == ISD::SETCC) {
1032 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1033 Tmp2.getOperand(0), Tmp2.getOperand(1),
1034 Node->getOperand(2), Node->getOperand(3));
1035 } else {
1036 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1037 DAG.getConstant(0, Tmp2.getValueType()),
1038 Node->getOperand(2), Node->getOperand(3));
1039 }
1040 } else {
1041 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +00001042 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001043 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
1044 }
Chris Lattnerc06da622005-12-18 23:54:29 +00001045 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattnerfd986782005-04-09 03:30:19 +00001046 break;
1047 }
1048 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001049 case ISD::BRTWOWAY_CC:
1050 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001051 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +00001052 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1053 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1054 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1055 Tmp3 != Node->getOperand(3)) {
1056 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1057 Node->getOperand(4), Node->getOperand(5));
1058 }
1059 break;
1060 } else {
1061 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1062 Node->getOperand(2), // LHS
1063 Node->getOperand(3), // RHS
1064 Node->getOperand(1)));
1065 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1066 // pair.
1067 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1068 default: assert(0 && "This action is not supported yet!");
1069 case TargetLowering::Legal:
1070 // If we get a SETCC back from legalizing the SETCC node we just
1071 // created, then use its LHS, RHS, and CC directly in creating a new
1072 // node. Otherwise, select between the true and false value based on
1073 // comparing the result of the legalized with zero.
1074 if (Tmp2.getOpcode() == ISD::SETCC) {
1075 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1076 Tmp2.getOperand(0), Tmp2.getOperand(1),
1077 Node->getOperand(4), Node->getOperand(5));
1078 } else {
1079 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1080 DAG.getConstant(0, Tmp2.getValueType()),
1081 Node->getOperand(4), Node->getOperand(5));
1082 }
1083 break;
1084 case TargetLowering::Expand:
1085 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1086 Node->getOperand(4));
1087 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1088 break;
1089 }
Chris Lattner0fab4592005-12-21 19:40:42 +00001090 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +00001091 }
1092 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001093 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001094 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1095 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001096
Evan Cheng31d15fa2005-12-23 07:29:34 +00001097 MVT::ValueType VT = Node->getValueType(0);
1098 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1099 default: assert(0 && "This action is not supported yet!");
1100 case TargetLowering::Custom: {
1101 SDOperand Op = DAG.getLoad(Node->getValueType(0),
1102 Tmp1, Tmp2, Node->getOperand(2));
1103 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1104 if (Tmp.Val) {
1105 Result = LegalizeOp(Tmp);
1106 // Since loads produce two values, make sure to remember that we legalized
1107 // both of them.
1108 AddLegalizedOperand(SDOperand(Node, 0), Result);
1109 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1110 return Result.getValue(Op.ResNo);
1111 }
1112 // FALLTHROUGH if the target thinks it is legal.
1113 }
1114 case TargetLowering::Legal:
1115 if (Tmp1 != Node->getOperand(0) ||
1116 Tmp2 != Node->getOperand(1))
1117 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1118 Node->getOperand(2));
1119 else
1120 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +00001121
Evan Cheng31d15fa2005-12-23 07:29:34 +00001122 // Since loads produce two values, make sure to remember that we legalized
1123 // both of them.
1124 AddLegalizedOperand(SDOperand(Node, 0), Result);
1125 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1126 return Result.getValue(Op.ResNo);
1127 }
1128 assert(0 && "Unreachable");
1129 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001130 case ISD::EXTLOAD:
1131 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001132 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001133 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1134 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001135
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001136 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001137 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001138 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001139 case TargetLowering::Promote:
1140 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001141 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1142 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001143 // Since loads produce two values, make sure to remember that we legalized
1144 // both of them.
1145 AddLegalizedOperand(SDOperand(Node, 0), Result);
1146 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1147 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +00001148
Evan Cheng31d15fa2005-12-23 07:29:34 +00001149 case TargetLowering::Custom: {
1150 SDOperand Op = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1151 Tmp1, Tmp2, Node->getOperand(2),
1152 SrcVT);
1153 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1154 if (Tmp.Val) {
1155 Result = LegalizeOp(Tmp);
1156 // Since loads produce two values, make sure to remember that we legalized
1157 // both of them.
1158 AddLegalizedOperand(SDOperand(Node, 0), Result);
1159 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1160 return Result.getValue(Op.ResNo);
1161 }
1162 // FALLTHROUGH if the target thinks it is legal.
1163 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001164 case TargetLowering::Legal:
1165 if (Tmp1 != Node->getOperand(0) ||
1166 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001167 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1168 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001169 else
1170 Result = SDOperand(Node, 0);
1171
1172 // Since loads produce two values, make sure to remember that we legalized
1173 // both of them.
1174 AddLegalizedOperand(SDOperand(Node, 0), Result);
1175 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1176 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001177 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001178 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001179 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1180 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001181 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc06da622005-12-18 23:54:29 +00001182 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001183 Load = LegalizeOp(Load);
1184 AddLegalizedOperand(SDOperand(Node, 0), Result);
1185 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001186 if (Op.ResNo)
1187 return Load.getValue(1);
1188 return Result;
1189 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001190 assert(Node->getOpcode() != ISD::EXTLOAD &&
1191 "EXTLOAD should always be supported!");
1192 // Turn the unsupported load into an EXTLOAD followed by an explicit
1193 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001194 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1195 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001196 SDOperand ValRes;
1197 if (Node->getOpcode() == ISD::SEXTLOAD)
1198 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001199 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001200 else
1201 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc06da622005-12-18 23:54:29 +00001202 Result = LegalizeOp(Result); // Relegalize new nodes.
1203 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001204 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1205 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001206 if (Op.ResNo)
1207 return Result.getValue(1);
1208 return ValRes;
1209 }
1210 assert(0 && "Unreachable");
1211 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001212 case ISD::EXTRACT_ELEMENT: {
1213 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1214 switch (getTypeAction(OpTy)) {
1215 default:
1216 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1217 break;
1218 case Legal:
1219 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1220 // 1 -> Hi
1221 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1222 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1223 TLI.getShiftAmountTy()));
1224 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1225 } else {
1226 // 0 -> Lo
1227 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1228 Node->getOperand(0));
1229 }
1230 Result = LegalizeOp(Result);
1231 break;
1232 case Expand:
1233 // Get both the low and high parts.
1234 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1235 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1236 Result = Tmp2; // 1 -> Hi
1237 else
1238 Result = Tmp1; // 0 -> Lo
1239 break;
1240 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001241 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001242 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001243
1244 case ISD::CopyToReg:
1245 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001246
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001247 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001248 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001249 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001250 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001251 if (Node->getNumValues() == 1) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001252 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1253 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1254 Node->getOperand(1), Tmp2);
1255 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001256 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1257 if (Node->getNumOperands() == 4)
1258 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001259 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001260 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001261 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1262 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1263 }
1264
1265 // Since this produces two values, make sure to remember that we legalized
1266 // both of them.
1267 AddLegalizedOperand(SDOperand(Node, 0), Result);
1268 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1269 return Result.getValue(Op.ResNo);
1270 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001271 break;
1272
1273 case ISD::RET:
1274 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1275 switch (Node->getNumOperands()) {
1276 case 2: // ret val
1277 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1278 case Legal:
1279 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001280 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001281 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1282 break;
1283 case Expand: {
1284 SDOperand Lo, Hi;
1285 ExpandOp(Node->getOperand(1), Lo, Hi);
1286 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001287 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001288 }
1289 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001290 Tmp2 = PromoteOp(Node->getOperand(1));
1291 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1292 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001293 }
1294 break;
1295 case 1: // ret void
1296 if (Tmp1 != Node->getOperand(0))
1297 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1298 break;
1299 default: { // ret <values>
1300 std::vector<SDOperand> NewValues;
1301 NewValues.push_back(Tmp1);
1302 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1303 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1304 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001305 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001306 break;
1307 case Expand: {
1308 SDOperand Lo, Hi;
1309 ExpandOp(Node->getOperand(i), Lo, Hi);
1310 NewValues.push_back(Lo);
1311 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001312 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001313 }
1314 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001315 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001316 }
1317 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1318 break;
1319 }
1320 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001321
Chris Lattnerfae8afb2006-01-06 05:47:48 +00001322 switch (TLI.getOperationAction(Node->getOpcode(),
1323 Node->getValueType(0))) {
Evan Chengf35b1c82006-01-06 00:41:43 +00001324 default: assert(0 && "This action is not supported yet!");
1325 case TargetLowering::Custom: {
1326 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1327 if (Tmp.Val) {
1328 Result = LegalizeOp(Tmp);
1329 break;
1330 }
1331 // FALLTHROUGH if the target thinks it is legal.
1332 }
1333 case TargetLowering::Legal:
1334 // Nothing to do.
1335 break;
1336 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001337 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001338 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1340 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1341
Chris Lattnere69daaf2005-01-08 06:25:56 +00001342 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001343 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001344 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001345 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001346 DAG.getConstant(FloatToBits(CFP->getValue()),
1347 MVT::i32),
1348 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001349 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001350 } else {
1351 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001352 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001353 DAG.getConstant(DoubleToBits(CFP->getValue()),
1354 MVT::i64),
1355 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001356 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001357 }
Chris Lattnera4743132005-02-22 07:23:39 +00001358 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001359 }
1360
Chris Lattnerdc750592005-01-07 07:47:09 +00001361 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1362 case Legal: {
1363 SDOperand Val = LegalizeOp(Node->getOperand(1));
1364 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1365 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001366 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1367 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001368
1369 MVT::ValueType VT = Result.Val->getOperand(1).getValueType();
1370 switch (TLI.getOperationAction(Result.Val->getOpcode(), VT)) {
1371 default: assert(0 && "This action is not supported yet!");
1372 case TargetLowering::Custom: {
1373 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1374 if (Tmp.Val) {
1375 Result = LegalizeOp(Tmp);
1376 break;
1377 }
1378 // FALLTHROUGH if the target thinks it is legal.
1379 }
1380 case TargetLowering::Legal:
1381 // Nothing to do.
1382 break;
1383 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001384 break;
1385 }
1386 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001387 // Truncate the value and store the result.
1388 Tmp3 = PromoteOp(Node->getOperand(1));
1389 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001390 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001391 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001392 break;
1393
Chris Lattnerdc750592005-01-07 07:47:09 +00001394 case Expand:
1395 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001396 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001397 ExpandOp(Node->getOperand(1), Lo, Hi);
1398
1399 if (!TLI.isLittleEndian())
1400 std::swap(Lo, Hi);
1401
Chris Lattner55e9cde2005-05-11 04:51:16 +00001402 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1403 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001404 // If this is a vector type, then we have to calculate the increment as
1405 // the product of the element size in bytes, and the number of elements
1406 // in the high half of the vector.
1407 if (MVT::Vector == Hi.getValueType()) {
1408 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1409 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1410 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1411 } else {
1412 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1413 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001414 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1415 getIntPtrConstant(IncrementSize));
1416 assert(isTypeLegal(Tmp2.getValueType()) &&
1417 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001418 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001419 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1420 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001421 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1422 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001423 }
1424 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001425 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001426 case ISD::PCMARKER:
1427 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001428 if (Tmp1 != Node->getOperand(0))
1429 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001430 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001431 case ISD::STACKSAVE:
1432 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1433 if (Tmp1 != Node->getOperand(0)) {
1434 std::vector<MVT::ValueType> VTs;
1435 VTs.push_back(Node->getValueType(0));
1436 VTs.push_back(MVT::Other);
1437 std::vector<SDOperand> Ops;
1438 Ops.push_back(Tmp1);
1439 Result = DAG.getNode(ISD::STACKSAVE, VTs, Ops);
1440 }
1441
1442 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1443 default: assert(0 && "This action is not supported yet!");
1444 case TargetLowering::Custom: {
1445 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1446 if (Tmp.Val) {
1447 Result = LegalizeOp(Tmp);
1448 break;
1449 }
1450 // FALLTHROUGH if the target thinks it is legal.
1451 }
1452 case TargetLowering::Legal:
1453 // Since stacksave produce two values, make sure to remember that we
1454 // legalized both of them.
1455 AddLegalizedOperand(SDOperand(Node, 0), Result);
1456 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1457 return Result.getValue(Op.ResNo);
1458 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001459 // Expand to CopyFromReg if the target set
1460 // StackPointerRegisterToSaveRestore.
1461 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1462 Tmp1 = DAG.getCopyFromReg(Node->getOperand(0), SP,
1463 Node->getValueType(0));
1464 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1465 AddLegalizedOperand(SDOperand(Node, 1), Tmp1.getValue(1));
1466 return Tmp1.getValue(Op.ResNo);
1467 } else {
1468 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1469 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1470 AddLegalizedOperand(SDOperand(Node, 1), Node->getOperand(0));
1471 return Op.ResNo ? Node->getOperand(0) : Tmp1;
1472 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001473 }
1474
1475 case ISD::STACKRESTORE:
1476 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1477 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1478 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1479 Result = DAG.getNode(ISD::STACKRESTORE, MVT::Other, Tmp1, Tmp2);
1480
1481 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1482 default: assert(0 && "This action is not supported yet!");
1483 case TargetLowering::Custom: {
1484 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1485 if (Tmp.Val) {
1486 Result = LegalizeOp(Tmp);
1487 break;
1488 }
1489 // FALLTHROUGH if the target thinks it is legal.
1490 }
1491 case TargetLowering::Legal:
1492 break;
1493 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001494 // Expand to CopyToReg if the target set
1495 // StackPointerRegisterToSaveRestore.
1496 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1497 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1498 } else {
1499 Result = Tmp1;
1500 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001501 break;
1502 }
1503 break;
1504
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001505 case ISD::READCYCLECOUNTER:
1506 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthf9b27d72005-12-02 06:08:08 +00001507 if (Tmp1 != Node->getOperand(0)) {
1508 std::vector<MVT::ValueType> rtypes;
1509 std::vector<SDOperand> rvals;
1510 rtypes.push_back(MVT::i64);
1511 rtypes.push_back(MVT::Other);
1512 rvals.push_back(Tmp1);
1513 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1514 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001515
1516 // Since rdcc produce two values, make sure to remember that we legalized
1517 // both of them.
1518 AddLegalizedOperand(SDOperand(Node, 0), Result);
1519 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1520 return Result.getValue(Op.ResNo);
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001521
Evan Cheng31d15fa2005-12-23 07:29:34 +00001522 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001523 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1524 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1525
1526 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattnerc7037ab2005-12-23 16:12:20 +00001527 case Promote:
1528 case Expand:
1529 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001530 case Legal:
1531 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001532
1533 // The only promote case we handle is TRUNCSTORE:i1 X into
1534 // -> TRUNCSTORE:i8 (and X, 1)
1535 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1536 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1537 TargetLowering::Promote) {
1538 // Promote the bool to a mask then store.
1539 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1540 DAG.getConstant(1, Tmp2.getValueType()));
1541 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1542 Node->getOperand(3), DAG.getValueType(MVT::i8));
1543
1544 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1545 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001546 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001547 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001548 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001549
1550 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1551 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1552 default: assert(0 && "This action is not supported yet!");
1553 case TargetLowering::Custom: {
1554 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1555 if (Tmp.Val) {
1556 Result = LegalizeOp(Tmp);
1557 break;
1558 }
1559 // FALLTHROUGH if the target thinks it is legal.
1560 }
1561 case TargetLowering::Legal:
1562 // Nothing to do.
1563 break;
1564 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001565 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001566 }
1567 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001568 }
Chris Lattner39c67442005-01-14 22:08:15 +00001569 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001570 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1571 case Expand: assert(0 && "It's impossible to expand bools");
1572 case Legal:
1573 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1574 break;
1575 case Promote:
1576 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1577 break;
1578 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001579 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001580 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001581
Nate Begeman987121a2005-08-23 04:29:48 +00001582 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001583 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001584 case TargetLowering::Expand:
1585 if (Tmp1.getOpcode() == ISD::SETCC) {
1586 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1587 Tmp2, Tmp3,
1588 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1589 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001590 // Make sure the condition is either zero or one. It may have been
1591 // promoted from something else.
1592 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001593 Result = DAG.getSelectCC(Tmp1,
1594 DAG.getConstant(0, Tmp1.getValueType()),
1595 Tmp2, Tmp3, ISD::SETNE);
1596 }
Chris Lattnerc06da622005-12-18 23:54:29 +00001597 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begemane5b86d72005-08-10 20:51:12 +00001598 break;
Evan Cheng225a4d02005-12-17 01:21:05 +00001599 case TargetLowering::Custom: {
1600 SDOperand Tmp =
1601 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1602 Tmp1, Tmp2, Tmp3), DAG);
1603 if (Tmp.Val) {
1604 Result = LegalizeOp(Tmp);
1605 break;
1606 }
1607 // FALLTHROUGH if the target thinks it is legal.
1608 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001609 case TargetLowering::Legal:
1610 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1611 Tmp3 != Node->getOperand(2))
1612 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1613 Tmp1, Tmp2, Tmp3);
1614 break;
1615 case TargetLowering::Promote: {
1616 MVT::ValueType NVT =
1617 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1618 unsigned ExtOp, TruncOp;
1619 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001620 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001621 TruncOp = ISD::TRUNCATE;
1622 } else {
1623 ExtOp = ISD::FP_EXTEND;
1624 TruncOp = ISD::FP_ROUND;
1625 }
1626 // Promote each of the values to the new type.
1627 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1628 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1629 // Perform the larger operation, then round down.
1630 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1631 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1632 break;
1633 }
1634 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001635 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001636 case ISD::SELECT_CC:
1637 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1638 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1639
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001640 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001641 // Everything is legal, see if we should expand this op or something.
1642 switch (TLI.getOperationAction(ISD::SELECT_CC,
1643 Node->getOperand(0).getValueType())) {
1644 default: assert(0 && "This action is not supported yet!");
1645 case TargetLowering::Custom: {
1646 SDOperand Tmp =
1647 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1648 Node->getOperand(0),
1649 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001650 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001651 if (Tmp.Val) {
1652 Result = LegalizeOp(Tmp);
1653 break;
1654 }
1655 } // FALLTHROUGH if the target can't lower this operation after all.
1656 case TargetLowering::Legal:
1657 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1658 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1659 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1660 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
Chris Lattner1408c052005-12-22 05:23:45 +00001661 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1,Tmp2,
Chris Lattner5f573412005-08-26 00:23:59 +00001662 Tmp3, Tmp4, Node->getOperand(4));
1663 }
1664 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001665 }
1666 break;
1667 } else {
1668 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1669 Node->getOperand(0), // LHS
1670 Node->getOperand(1), // RHS
1671 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001672 // If we get a SETCC back from legalizing the SETCC node we just
1673 // created, then use its LHS, RHS, and CC directly in creating a new
1674 // node. Otherwise, select between the true and false value based on
1675 // comparing the result of the legalized with zero.
1676 if (Tmp1.getOpcode() == ISD::SETCC) {
1677 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1678 Tmp1.getOperand(0), Tmp1.getOperand(1),
1679 Tmp3, Tmp4, Tmp1.getOperand(2));
1680 } else {
1681 Result = DAG.getSelectCC(Tmp1,
1682 DAG.getConstant(0, Tmp1.getValueType()),
1683 Tmp3, Tmp4, ISD::SETNE);
1684 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001685 }
1686 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001687 case ISD::SETCC:
1688 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1689 case Legal:
1690 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1691 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001692 break;
1693 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001694 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1695 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1696
1697 // If this is an FP compare, the operands have already been extended.
1698 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1699 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001700 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001701
1702 // Otherwise, we have to insert explicit sign or zero extends. Note
1703 // that we could insert sign extends for ALL conditions, but zero extend
1704 // is cheaper on many machines (an AND instead of two shifts), so prefer
1705 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001706 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001707 default: assert(0 && "Unknown integer comparison!");
1708 case ISD::SETEQ:
1709 case ISD::SETNE:
1710 case ISD::SETUGE:
1711 case ISD::SETUGT:
1712 case ISD::SETULE:
1713 case ISD::SETULT:
1714 // ALL of these operations will work if we either sign or zero extend
1715 // the operands (including the unsigned comparisons!). Zero extend is
1716 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001717 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1718 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001719 break;
1720 case ISD::SETGE:
1721 case ISD::SETGT:
1722 case ISD::SETLT:
1723 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001724 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1725 DAG.getValueType(VT));
1726 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1727 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001728 break;
1729 }
Chris Lattner4d978642005-01-15 22:16:26 +00001730 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001731 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001732 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001733 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1734 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1735 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001736 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001737 case ISD::SETEQ:
1738 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001739 if (RHSLo == RHSHi)
1740 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1741 if (RHSCST->isAllOnesValue()) {
1742 // Comparison to -1.
1743 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001744 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001745 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001746 }
1747
Chris Lattnerdc750592005-01-07 07:47:09 +00001748 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1749 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1750 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001751 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001752 break;
1753 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001754 // If this is a comparison of the sign bit, just look at the top part.
1755 // X > -1, x < 0
1756 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001757 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001758 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001759 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001760 (CST->isAllOnesValue()))) { // X > -1
1761 Tmp1 = LHSHi;
1762 Tmp2 = RHSHi;
1763 break;
1764 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001765
Chris Lattnerdc750592005-01-07 07:47:09 +00001766 // FIXME: This generated code sucks.
1767 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001768 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001769 default: assert(0 && "Unknown integer setcc!");
1770 case ISD::SETLT:
1771 case ISD::SETULT: LowCC = ISD::SETULT; break;
1772 case ISD::SETGT:
1773 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1774 case ISD::SETLE:
1775 case ISD::SETULE: LowCC = ISD::SETULE; break;
1776 case ISD::SETGE:
1777 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1778 }
Misha Brukman835702a2005-04-21 22:36:52 +00001779
Chris Lattnerdc750592005-01-07 07:47:09 +00001780 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1781 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1782 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1783
1784 // NOTE: on targets without efficient SELECT of bools, we can always use
1785 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001786 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1787 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1788 Node->getOperand(2));
1789 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001790 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1791 Result, Tmp1, Tmp2));
Chris Lattner2af3ee42005-12-20 00:53:54 +00001792 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman987121a2005-08-23 04:29:48 +00001793 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001794 }
1795 }
Nate Begeman987121a2005-08-23 04:29:48 +00001796
Chris Lattner1408c052005-12-22 05:23:45 +00001797 switch(TLI.getOperationAction(ISD::SETCC,
1798 Node->getOperand(0).getValueType())) {
Nate Begeman987121a2005-08-23 04:29:48 +00001799 default:
1800 assert(0 && "Cannot handle this action for SETCC yet!");
1801 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001802 case TargetLowering::Promote: {
1803 // First step, figure out the appropriate operation to use.
1804 // Allow SETCC to not be supported for all legal data types
1805 // Mostly this targets FP
1806 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1807 MVT::ValueType OldVT = NewInTy;
1808
1809 // Scan for the appropriate larger type to use.
1810 while (1) {
1811 NewInTy = (MVT::ValueType)(NewInTy+1);
1812
1813 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1814 "Fell off of the edge of the integer world");
1815 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1816 "Fell off of the edge of the floating point world");
1817
1818 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001819 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001820 break;
1821 }
1822 if (MVT::isInteger(NewInTy))
1823 assert(0 && "Cannot promote Legal Integer SETCC yet");
1824 else {
1825 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1826 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1827 }
1828
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001829 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1830 Node->getOperand(2));
1831 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001832 }
Evan Chengc1583db2005-12-21 20:21:51 +00001833 case TargetLowering::Custom: {
1834 SDOperand Tmp =
1835 TLI.LowerOperation(DAG.getNode(ISD::SETCC, Node->getValueType(0),
1836 Tmp1, Tmp2, Node->getOperand(2)), DAG);
1837 if (Tmp.Val) {
1838 Result = LegalizeOp(Tmp);
1839 break;
1840 }
1841 // FALLTHROUGH if the target thinks it is legal.
1842 }
Nate Begeman987121a2005-08-23 04:29:48 +00001843 case TargetLowering::Legal:
1844 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1845 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1846 Node->getOperand(2));
1847 break;
1848 case TargetLowering::Expand:
1849 // Expand a setcc node into a select_cc of the same condition, lhs, and
1850 // rhs that selects between const 1 (true) and const 0 (false).
1851 MVT::ValueType VT = Node->getValueType(0);
1852 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1853 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1854 Node->getOperand(2));
1855 Result = LegalizeOp(Result);
1856 break;
1857 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001858 break;
1859
Chris Lattner85d70c62005-01-11 05:57:22 +00001860 case ISD::MEMSET:
1861 case ISD::MEMCPY:
1862 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001863 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001864 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1865
1866 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1867 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1868 case Expand: assert(0 && "Cannot expand a byte!");
1869 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001870 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001871 break;
1872 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001873 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001874 break;
1875 }
1876 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001877 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001878 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001879
1880 SDOperand Tmp4;
1881 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001882 case Expand: {
1883 // Length is too big, just take the lo-part of the length.
1884 SDOperand HiPart;
1885 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1886 break;
1887 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001888 case Legal:
1889 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001890 break;
1891 case Promote:
1892 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001893 break;
1894 }
1895
1896 SDOperand Tmp5;
1897 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1898 case Expand: assert(0 && "Cannot expand this yet!");
1899 case Legal:
1900 Tmp5 = LegalizeOp(Node->getOperand(4));
1901 break;
1902 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001903 Tmp5 = PromoteOp(Node->getOperand(4));
1904 break;
1905 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001906
1907 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1908 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001909 case TargetLowering::Custom: {
1910 SDOperand Tmp =
1911 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1912 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1913 if (Tmp.Val) {
1914 Result = LegalizeOp(Tmp);
1915 break;
1916 }
1917 // FALLTHROUGH if the target thinks it is legal.
1918 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001919 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001920 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1921 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1922 Tmp5 != Node->getOperand(4)) {
1923 std::vector<SDOperand> Ops;
1924 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1925 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1926 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1927 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001928 break;
1929 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001930 // Otherwise, the target does not support this operation. Lower the
1931 // operation to an explicit libcall as appropriate.
1932 MVT::ValueType IntPtr = TLI.getPointerTy();
1933 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1934 std::vector<std::pair<SDOperand, const Type*> > Args;
1935
Reid Spencer6dced922005-01-12 14:53:45 +00001936 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001937 if (Node->getOpcode() == ISD::MEMSET) {
1938 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1939 // Extend the ubyte argument to be an int value for the call.
1940 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1941 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1942 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1943
1944 FnName = "memset";
1945 } else if (Node->getOpcode() == ISD::MEMCPY ||
1946 Node->getOpcode() == ISD::MEMMOVE) {
1947 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1948 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1949 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1950 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1951 } else {
1952 assert(0 && "Unknown op!");
1953 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001954
Chris Lattner85d70c62005-01-11 05:57:22 +00001955 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001956 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001957 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner2af3ee42005-12-20 00:53:54 +00001958 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001959 break;
1960 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001961 }
1962 break;
1963 }
Chris Lattner5385db52005-05-09 20:23:03 +00001964
1965 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001966 Tmp1 = LegalizeOp(Node->getOperand(0));
1967 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001968
Chris Lattner86535992005-05-14 07:45:46 +00001969 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1970 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1971 std::vector<SDOperand> Ops;
1972 Ops.push_back(Tmp1);
1973 Ops.push_back(Tmp2);
1974 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1975 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001976 Result = SDOperand(Node, 0);
1977 // Since these produce two values, make sure to remember that we legalized
1978 // both of them.
1979 AddLegalizedOperand(SDOperand(Node, 0), Result);
1980 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1981 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001982 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001983 Tmp1 = LegalizeOp(Node->getOperand(0));
1984 Tmp2 = LegalizeOp(Node->getOperand(1));
1985 Tmp3 = LegalizeOp(Node->getOperand(2));
1986 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1987 Tmp3 != Node->getOperand(2))
1988 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1989 break;
1990
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001991 case ISD::READIO:
1992 Tmp1 = LegalizeOp(Node->getOperand(0));
1993 Tmp2 = LegalizeOp(Node->getOperand(1));
1994
1995 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1996 case TargetLowering::Custom:
1997 default: assert(0 && "This action not implemented for this operation!");
1998 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001999 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
2000 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
2001 std::vector<SDOperand> Ops;
2002 Ops.push_back(Tmp1);
2003 Ops.push_back(Tmp2);
2004 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
2005 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00002006 Result = SDOperand(Node, 0);
2007 break;
2008 case TargetLowering::Expand:
2009 // Replace this with a load from memory.
2010 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
2011 Node->getOperand(1), DAG.getSrcValue(NULL));
2012 Result = LegalizeOp(Result);
2013 break;
2014 }
2015
2016 // Since these produce two values, make sure to remember that we legalized
2017 // both of them.
2018 AddLegalizedOperand(SDOperand(Node, 0), Result);
2019 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2020 return Result.getValue(Op.ResNo);
2021
2022 case ISD::WRITEIO:
2023 Tmp1 = LegalizeOp(Node->getOperand(0));
2024 Tmp2 = LegalizeOp(Node->getOperand(1));
2025 Tmp3 = LegalizeOp(Node->getOperand(2));
2026
2027 switch (TLI.getOperationAction(Node->getOpcode(),
2028 Node->getOperand(1).getValueType())) {
2029 case TargetLowering::Custom:
2030 default: assert(0 && "This action not implemented for this operation!");
2031 case TargetLowering::Legal:
2032 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
2033 Tmp3 != Node->getOperand(2))
2034 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
2035 break;
2036 case TargetLowering::Expand:
2037 // Replace this with a store to memory.
2038 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
2039 Node->getOperand(1), Node->getOperand(2),
2040 DAG.getSrcValue(NULL));
2041 Result = LegalizeOp(Result);
2042 break;
2043 }
2044 break;
2045
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002046 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00002047 case ISD::SUB_PARTS:
2048 case ISD::SHL_PARTS:
2049 case ISD::SRA_PARTS:
2050 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002051 std::vector<SDOperand> Ops;
2052 bool Changed = false;
2053 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2054 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2055 Changed |= Ops.back() != Node->getOperand(i);
2056 }
Chris Lattner669e8c22005-05-14 07:25:05 +00002057 if (Changed) {
2058 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
2059 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
2060 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002061
Evan Cheng870e4f82006-01-09 18:31:59 +00002062 switch (TLI.getOperationAction(Node->getOpcode(),
2063 Node->getValueType(0))) {
2064 default: assert(0 && "This action is not supported yet!");
2065 case TargetLowering::Custom: {
2066 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2067 if (Tmp.Val) {
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002068 SDOperand Tmp2, RetVal(0,0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002069 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2070 Tmp2 = LegalizeOp(Tmp.getValue(i));
2071 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2072 if (i == Op.ResNo)
2073 RetVal = Tmp;
2074 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002075 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002076 return RetVal;
2077 }
2078 // FALLTHROUGH if the target thinks it is legal.
2079 }
2080 case TargetLowering::Legal:
2081 // Nothing to do.
2082 break;
2083 }
2084
Chris Lattner13fe99c2005-04-02 05:00:07 +00002085 // Since these produce multiple values, make sure to remember that we
2086 // legalized all of them.
2087 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2088 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2089 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002090 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002091
2092 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002093 case ISD::ADD:
2094 case ISD::SUB:
2095 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002096 case ISD::MULHS:
2097 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002098 case ISD::UDIV:
2099 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002100 case ISD::AND:
2101 case ISD::OR:
2102 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002103 case ISD::SHL:
2104 case ISD::SRL:
2105 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002106 case ISD::FADD:
2107 case ISD::FSUB:
2108 case ISD::FMUL:
2109 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002110 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002111 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2112 case Expand: assert(0 && "Not possible");
2113 case Legal:
2114 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2115 break;
2116 case Promote:
2117 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2118 break;
2119 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002120 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth72594262005-12-24 23:42:32 +00002121 case TargetLowering::Custom: {
2122 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2);
2123 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2124 if (Tmp.Val) {
2125 Tmp = LegalizeOp(Tmp); // Relegalize input.
2126 AddLegalizedOperand(Op, Tmp);
2127 return Tmp;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002128 } //else it was considered legal and we fall through
Andrew Lenharth72594262005-12-24 23:42:32 +00002129 }
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002130 case TargetLowering::Legal:
2131 if (Tmp1 != Node->getOperand(0) ||
2132 Tmp2 != Node->getOperand(1))
2133 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
2134 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00002135 default:
2136 assert(0 && "Operation not supported");
2137 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002138 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002139
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002140 case ISD::BUILD_PAIR: {
2141 MVT::ValueType PairTy = Node->getValueType(0);
2142 // TODO: handle the case where the Lo and Hi operands are not of legal type
2143 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2144 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2145 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
2146 case TargetLowering::Legal:
2147 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2148 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2149 break;
2150 case TargetLowering::Promote:
2151 case TargetLowering::Custom:
2152 assert(0 && "Cannot promote/custom this yet!");
2153 case TargetLowering::Expand:
2154 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2155 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2156 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2157 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2158 TLI.getShiftAmountTy()));
2159 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
2160 break;
2161 }
2162 break;
2163 }
2164
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002165 case ISD::UREM:
2166 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002167 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002168 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2169 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2170 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002171 case TargetLowering::Custom: {
2172 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2);
2173 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2174 if (Tmp.Val) {
2175 Tmp = LegalizeOp(Tmp); // Relegalize input.
2176 AddLegalizedOperand(Op, Tmp);
2177 return Tmp;
2178 } //else it was considered legal and we fall through
2179 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002180 case TargetLowering::Legal:
2181 if (Tmp1 != Node->getOperand(0) ||
2182 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00002183 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002184 Tmp2);
2185 break;
2186 case TargetLowering::Promote:
Andrew Lenharth72594262005-12-24 23:42:32 +00002187 assert(0 && "Cannot promote handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00002188 case TargetLowering::Expand:
2189 if (MVT::isInteger(Node->getValueType(0))) {
2190 MVT::ValueType VT = Node->getValueType(0);
2191 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
2192 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2193 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2194 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2195 } else {
2196 // Floating point mod -> fmod libcall.
2197 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2198 SDOperand Dummy;
2199 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002200 }
2201 break;
2202 }
2203 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002204
Nate Begeman1b8121b2006-01-11 21:21:00 +00002205 case ISD::ROTL:
2206 case ISD::ROTR:
2207 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2208 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2209 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2210 case TargetLowering::Custom:
2211 case TargetLowering::Promote:
2212 case TargetLowering::Expand:
2213 assert(0 && "Cannot handle this yet!");
2214 case TargetLowering::Legal:
2215 if (Tmp1 != Node->getOperand(0) ||
2216 Tmp2 != Node->getOperand(1))
2217 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
2218 Tmp2);
2219 break;
2220 }
2221 break;
2222
Nate Begeman2fba8a32006-01-14 03:14:10 +00002223 case ISD::BSWAP:
2224 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2225 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2226 case TargetLowering::Legal:
2227 if (Tmp1 != Node->getOperand(0))
2228 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2229 break;
2230 case TargetLowering::Promote: {
2231 MVT::ValueType OVT = Tmp1.getValueType();
2232 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2233 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
2234
2235 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2236 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2237 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2238 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2239 break;
2240 }
2241 case TargetLowering::Custom:
2242 assert(0 && "Cannot custom legalize this yet!");
2243 case TargetLowering::Expand: {
2244 MVT::ValueType VT = Tmp1.getValueType();
2245 switch (VT) {
2246 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
2247 case MVT::i16:
2248 Tmp2 = DAG.getNode(ISD::SHL, VT, Tmp1,
2249 DAG.getConstant(8, TLI.getShiftAmountTy()));
2250 Tmp1 = DAG.getNode(ISD::SRL, VT, Tmp1,
2251 DAG.getConstant(8, TLI.getShiftAmountTy()));
2252 Result = DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
2253 break;
2254 case MVT::i32:
2255 Tmp4 = DAG.getNode(ISD::SHL, VT, Tmp1,
2256 DAG.getConstant(24, TLI.getShiftAmountTy()));
2257 Tmp3 = DAG.getNode(ISD::SHL, VT, Tmp1,
2258 DAG.getConstant(8, TLI.getShiftAmountTy()));
2259 Tmp2 = DAG.getNode(ISD::SRL, VT, Tmp1,
2260 DAG.getConstant(8, TLI.getShiftAmountTy()));
2261 Tmp1 = DAG.getNode(ISD::SRL, VT, Tmp1,
2262 DAG.getConstant(24, TLI.getShiftAmountTy()));
2263 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2264 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2265 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
2266 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
2267 Result = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
2268 break;
2269 }
2270 break;
2271 }
2272 }
2273 break;
2274
Andrew Lenharth5e177822005-05-03 17:19:30 +00002275 case ISD::CTPOP:
2276 case ISD::CTTZ:
2277 case ISD::CTLZ:
2278 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2279 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2280 case TargetLowering::Legal:
2281 if (Tmp1 != Node->getOperand(0))
2282 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2283 break;
2284 case TargetLowering::Promote: {
2285 MVT::ValueType OVT = Tmp1.getValueType();
2286 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002287
2288 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002289 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2290 // Perform the larger operation, then subtract if needed.
2291 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2292 switch(Node->getOpcode())
2293 {
2294 case ISD::CTPOP:
2295 Result = Tmp1;
2296 break;
2297 case ISD::CTTZ:
2298 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002299 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2300 DAG.getConstant(getSizeInBits(NVT), NVT),
2301 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002302 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002303 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2304 break;
2305 case ISD::CTLZ:
2306 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002307 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2308 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002309 getSizeInBits(OVT), NVT));
2310 break;
2311 }
2312 break;
2313 }
2314 case TargetLowering::Custom:
2315 assert(0 && "Cannot custom handle this yet!");
2316 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002317 switch(Node->getOpcode())
2318 {
2319 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00002320 static const uint64_t mask[6] = {
2321 0x5555555555555555ULL, 0x3333333333333333ULL,
2322 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2323 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2324 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002325 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00002326 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2327 unsigned len = getSizeInBits(VT);
2328 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002329 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00002330 Tmp2 = DAG.getConstant(mask[i], VT);
2331 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002332 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002333 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
2334 DAG.getNode(ISD::AND, VT,
2335 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
2336 Tmp2));
2337 }
2338 Result = Tmp1;
2339 break;
2340 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002341 case ISD::CTLZ: {
2342 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00002343 x = x | (x >> 1);
2344 x = x | (x >> 2);
2345 ...
2346 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002347 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00002348 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002349
Chris Lattner56add052005-05-11 18:35:21 +00002350 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2351 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002352 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2353 unsigned len = getSizeInBits(VT);
2354 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2355 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002356 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002357 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2358 }
2359 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00002360 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00002361 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002362 }
2363 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002364 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002365 // unless the target has ctlz but not ctpop, in which case we use:
2366 // { return 32 - nlz(~x & (x-1)); }
2367 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00002368 MVT::ValueType VT = Tmp1.getValueType();
2369 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002370 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00002371 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2372 DAG.getNode(ISD::SUB, VT, Tmp1,
2373 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002374 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002375 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2376 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002377 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002378 DAG.getConstant(getSizeInBits(VT), VT),
2379 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2380 } else {
2381 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2382 }
Chris Lattner72473242005-05-11 05:27:09 +00002383 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002384 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002385 default:
2386 assert(0 && "Cannot expand this yet!");
2387 break;
2388 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002389 break;
2390 }
2391 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002392
Chris Lattner13fe99c2005-04-02 05:00:07 +00002393 // Unary operators
2394 case ISD::FABS:
2395 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002396 case ISD::FSQRT:
2397 case ISD::FSIN:
2398 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002399 Tmp1 = LegalizeOp(Node->getOperand(0));
2400 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2401 case TargetLowering::Legal:
2402 if (Tmp1 != Node->getOperand(0))
2403 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2404 break;
2405 case TargetLowering::Promote:
2406 case TargetLowering::Custom:
2407 assert(0 && "Cannot promote/custom handle this yet!");
2408 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00002409 switch(Node->getOpcode()) {
2410 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00002411 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2412 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00002413 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00002414 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00002415 break;
2416 }
2417 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002418 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2419 MVT::ValueType VT = Node->getValueType(0);
2420 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002421 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002422 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2423 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2424 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00002425 break;
2426 }
2427 case ISD::FSQRT:
2428 case ISD::FSIN:
2429 case ISD::FCOS: {
2430 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002431 const char *FnName = 0;
2432 switch(Node->getOpcode()) {
2433 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2434 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2435 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2436 default: assert(0 && "Unreachable!");
2437 }
Nate Begeman77558da2005-08-04 21:43:28 +00002438 SDOperand Dummy;
2439 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002440 break;
2441 }
2442 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002443 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002444 }
2445 break;
2446 }
2447 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002448
2449 case ISD::BIT_CONVERT:
2450 if (!isTypeLegal(Node->getOperand(0).getValueType()))
2451 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2452 else {
2453 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2454 Node->getOperand(0).getValueType())) {
2455 default: assert(0 && "Unknown operation action!");
2456 case TargetLowering::Expand:
2457 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2458 break;
2459 case TargetLowering::Legal:
2460 Tmp1 = LegalizeOp(Node->getOperand(0));
2461 if (Tmp1 != Node->getOperand(0))
2462 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Tmp1);
2463 break;
2464 }
2465 }
2466 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002467 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002468 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002469 case ISD::UINT_TO_FP: {
2470 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002471 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2472 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002473 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002474 Node->getOperand(0).getValueType())) {
2475 default: assert(0 && "Unknown operation action!");
2476 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002477 Result = ExpandLegalINT_TO_FP(isSigned,
2478 LegalizeOp(Node->getOperand(0)),
2479 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002480 AddLegalizedOperand(Op, Result);
2481 return Result;
2482 case TargetLowering::Promote:
2483 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2484 Node->getValueType(0),
2485 isSigned);
2486 AddLegalizedOperand(Op, Result);
2487 return Result;
2488 case TargetLowering::Legal:
2489 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002490 case TargetLowering::Custom: {
2491 Tmp1 = LegalizeOp(Node->getOperand(0));
2492 SDOperand Tmp =
2493 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2494 Tmp = TLI.LowerOperation(Tmp, DAG);
2495 if (Tmp.Val) {
Chris Lattner2af3ee42005-12-20 00:53:54 +00002496 Tmp = LegalizeOp(Tmp); // Relegalize input.
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002497 AddLegalizedOperand(Op, Tmp);
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002498 return Tmp;
2499 } else {
2500 assert(0 && "Target Must Lower this");
2501 }
2502 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002503 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002504
Chris Lattnerdc750592005-01-07 07:47:09 +00002505 Tmp1 = LegalizeOp(Node->getOperand(0));
2506 if (Tmp1 != Node->getOperand(0))
2507 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2508 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002509 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002510 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2511 Node->getValueType(0), Node->getOperand(0));
2512 break;
2513 case Promote:
2514 if (isSigned) {
2515 Result = PromoteOp(Node->getOperand(0));
2516 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2517 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2518 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2519 } else {
2520 Result = PromoteOp(Node->getOperand(0));
2521 Result = DAG.getZeroExtendInReg(Result,
2522 Node->getOperand(0).getValueType());
2523 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002524 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002525 break;
2526 }
2527 break;
2528 }
2529 case ISD::TRUNCATE:
2530 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2531 case Legal:
2532 Tmp1 = LegalizeOp(Node->getOperand(0));
2533 if (Tmp1 != Node->getOperand(0))
2534 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2535 break;
2536 case Expand:
2537 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2538
2539 // Since the result is legal, we should just be able to truncate the low
2540 // part of the source.
2541 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2542 break;
2543 case Promote:
2544 Result = PromoteOp(Node->getOperand(0));
2545 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2546 break;
2547 }
2548 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002549
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002550 case ISD::FP_TO_SINT:
2551 case ISD::FP_TO_UINT:
2552 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2553 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002554 Tmp1 = LegalizeOp(Node->getOperand(0));
2555
Chris Lattner44fe26f2005-07-29 00:11:56 +00002556 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2557 default: assert(0 && "Unknown operation action!");
2558 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002559 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2560 SDOperand True, False;
2561 MVT::ValueType VT = Node->getOperand(0).getValueType();
2562 MVT::ValueType NVT = Node->getValueType(0);
2563 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2564 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2565 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2566 Node->getOperand(0), Tmp2, ISD::SETLT);
2567 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2568 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002569 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002570 Tmp2));
2571 False = DAG.getNode(ISD::XOR, NVT, False,
2572 DAG.getConstant(1ULL << ShiftAmt, NVT));
2573 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Chris Lattner2af3ee42005-12-20 00:53:54 +00002574 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemand5e739d2005-08-14 18:38:32 +00002575 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002576 } else {
2577 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2578 }
2579 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002580 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002581 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002582 Node->getOpcode() == ISD::FP_TO_SINT);
2583 AddLegalizedOperand(Op, Result);
2584 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002585 case TargetLowering::Custom: {
2586 SDOperand Tmp =
2587 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2588 Tmp = TLI.LowerOperation(Tmp, DAG);
2589 if (Tmp.Val) {
Chris Lattner2af3ee42005-12-20 00:53:54 +00002590 Tmp = LegalizeOp(Tmp);
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002591 AddLegalizedOperand(Op, Tmp);
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002592 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002593 } else {
2594 // The target thinks this is legal afterall.
2595 break;
2596 }
2597 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002598 case TargetLowering::Legal:
2599 break;
2600 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002601
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002602 if (Tmp1 != Node->getOperand(0))
2603 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2604 break;
2605 case Expand:
2606 assert(0 && "Shouldn't need to expand other operators here!");
2607 case Promote:
2608 Result = PromoteOp(Node->getOperand(0));
2609 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2610 break;
2611 }
2612 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002613
Chris Lattner7753f172005-09-02 00:18:10 +00002614 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002615 case ISD::ZERO_EXTEND:
2616 case ISD::SIGN_EXTEND:
2617 case ISD::FP_EXTEND:
2618 case ISD::FP_ROUND:
2619 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2620 case Legal:
2621 Tmp1 = LegalizeOp(Node->getOperand(0));
2622 if (Tmp1 != Node->getOperand(0))
2623 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2624 break;
2625 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002626 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002627
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002628 case Promote:
2629 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002630 case ISD::ANY_EXTEND:
2631 Result = PromoteOp(Node->getOperand(0));
2632 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2633 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002634 case ISD::ZERO_EXTEND:
2635 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002636 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002637 Result = DAG.getZeroExtendInReg(Result,
2638 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002639 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002640 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002641 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002642 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002643 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002644 Result,
2645 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002646 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002647 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002648 Result = PromoteOp(Node->getOperand(0));
2649 if (Result.getValueType() != Op.getValueType())
2650 // Dynamically dead while we have only 2 FP types.
2651 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2652 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002653 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002654 Result = PromoteOp(Node->getOperand(0));
2655 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2656 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002657 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002658 }
2659 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002660 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002661 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002662 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002663 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002664
2665 // If this operation is not supported, convert it to a shl/shr or load/store
2666 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002667 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2668 default: assert(0 && "This action not supported for this op yet!");
2669 case TargetLowering::Legal:
2670 if (Tmp1 != Node->getOperand(0))
2671 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002672 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002673 break;
2674 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002675 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002676 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002677 // NOTE: we could fall back on load/store here too for targets without
2678 // SAR. However, it is doubtful that any exist.
2679 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2680 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002681 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002682 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2683 Node->getOperand(0), ShiftCst);
2684 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2685 Result, ShiftCst);
2686 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2687 // The only way we can lower this is to turn it into a STORETRUNC,
2688 // EXTLOAD pair, targetting a temporary location (a stack slot).
2689
2690 // NOTE: there is a choice here between constantly creating new stack
2691 // slots and always reusing the same one. We currently always create
2692 // new ones, as reuse may inhibit scheduling.
2693 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2694 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2695 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2696 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002697 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002698 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2699 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2700 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002701 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002702 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002703 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2704 Result, StackSlot, DAG.getSrcValue(NULL),
2705 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002706 } else {
2707 assert(0 && "Unknown op");
2708 }
2709 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002710 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002711 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002712 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002713 }
Chris Lattner99222f72005-01-15 07:15:18 +00002714 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002715
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002716 // Note that LegalizeOp may be reentered even from single-use nodes, which
2717 // means that we always must cache transformed nodes.
2718 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002719 return Result;
2720}
2721
Chris Lattner4d978642005-01-15 22:16:26 +00002722/// PromoteOp - Given an operation that produces a value in an invalid type,
2723/// promote it to compute the value into a larger type. The produced value will
2724/// have the correct bits for the low portion of the register, but no guarantee
2725/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002726SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2727 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002728 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002729 assert(getTypeAction(VT) == Promote &&
2730 "Caller should expand or legalize operands that are not promotable!");
2731 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2732 "Cannot promote to smaller type!");
2733
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002734 SDOperand Tmp1, Tmp2, Tmp3;
2735
2736 SDOperand Result;
2737 SDNode *Node = Op.Val;
2738
Chris Lattner1a570f12005-09-02 20:32:45 +00002739 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2740 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002741
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002742 // Promotion needs an optimization step to clean up after it, and is not
2743 // careful to avoid operations the target does not support. Make sure that
2744 // all generated operations are legalized in the next iteration.
2745 NeedsAnotherIteration = true;
2746
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002747 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002748 case ISD::CopyFromReg:
2749 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002750 default:
2751 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2752 assert(0 && "Do not know how to promote this operator!");
2753 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002754 case ISD::UNDEF:
2755 Result = DAG.getNode(ISD::UNDEF, NVT);
2756 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002757 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002758 if (VT != MVT::i1)
2759 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2760 else
2761 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002762 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2763 break;
2764 case ISD::ConstantFP:
2765 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2766 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2767 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002768
Chris Lattner2cb338d2005-01-18 02:59:52 +00002769 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002770 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002771 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2772 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002773 Result = LegalizeOp(Result);
2774 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002775
2776 case ISD::TRUNCATE:
2777 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2778 case Legal:
2779 Result = LegalizeOp(Node->getOperand(0));
2780 assert(Result.getValueType() >= NVT &&
2781 "This truncation doesn't make sense!");
2782 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2783 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2784 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002785 case Promote:
2786 // The truncation is not required, because we don't guarantee anything
2787 // about high bits anyway.
2788 Result = PromoteOp(Node->getOperand(0));
2789 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002790 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002791 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2792 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002793 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002794 }
2795 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002796 case ISD::SIGN_EXTEND:
2797 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002798 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002799 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2800 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2801 case Legal:
2802 // Input is legal? Just do extend all the way to the larger type.
2803 Result = LegalizeOp(Node->getOperand(0));
2804 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2805 break;
2806 case Promote:
2807 // Promote the reg if it's smaller.
2808 Result = PromoteOp(Node->getOperand(0));
2809 // The high bits are not guaranteed to be anything. Insert an extend.
2810 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002811 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002812 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002813 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002814 Result = DAG.getZeroExtendInReg(Result,
2815 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002816 break;
2817 }
2818 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002819 case ISD::BIT_CONVERT:
2820 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2821 Result = PromoteOp(Result);
2822 break;
2823
Chris Lattner4d978642005-01-15 22:16:26 +00002824 case ISD::FP_EXTEND:
2825 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2826 case ISD::FP_ROUND:
2827 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2828 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2829 case Promote: assert(0 && "Unreachable with 2 FP types!");
2830 case Legal:
2831 // Input is legal? Do an FP_ROUND_INREG.
2832 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002833 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2834 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002835 break;
2836 }
2837 break;
2838
2839 case ISD::SINT_TO_FP:
2840 case ISD::UINT_TO_FP:
2841 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2842 case Legal:
2843 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002844 // No extra round required here.
2845 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002846 break;
2847
2848 case Promote:
2849 Result = PromoteOp(Node->getOperand(0));
2850 if (Node->getOpcode() == ISD::SINT_TO_FP)
2851 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002852 Result,
2853 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002854 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002855 Result = DAG.getZeroExtendInReg(Result,
2856 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002857 // No extra round required here.
2858 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002859 break;
2860 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002861 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2862 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002863 // Round if we cannot tolerate excess precision.
2864 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002865 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2866 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002867 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002868 }
Chris Lattner4d978642005-01-15 22:16:26 +00002869 break;
2870
Chris Lattner268d4572005-12-09 17:32:47 +00002871 case ISD::SIGN_EXTEND_INREG:
2872 Result = PromoteOp(Node->getOperand(0));
2873 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2874 Node->getOperand(1));
2875 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002876 case ISD::FP_TO_SINT:
2877 case ISD::FP_TO_UINT:
2878 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2879 case Legal:
2880 Tmp1 = LegalizeOp(Node->getOperand(0));
2881 break;
2882 case Promote:
2883 // The input result is prerounded, so we don't have to do anything
2884 // special.
2885 Tmp1 = PromoteOp(Node->getOperand(0));
2886 break;
2887 case Expand:
2888 assert(0 && "not implemented");
2889 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002890 // If we're promoting a UINT to a larger size, check to see if the new node
2891 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2892 // we can use that instead. This allows us to generate better code for
2893 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2894 // legal, such as PowerPC.
2895 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002896 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002897 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2898 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002899 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2900 } else {
2901 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2902 }
Chris Lattner4d978642005-01-15 22:16:26 +00002903 break;
2904
Chris Lattner13fe99c2005-04-02 05:00:07 +00002905 case ISD::FABS:
2906 case ISD::FNEG:
2907 Tmp1 = PromoteOp(Node->getOperand(0));
2908 assert(Tmp1.getValueType() == NVT);
2909 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2910 // NOTE: we do not have to do any extra rounding here for
2911 // NoExcessFPPrecision, because we know the input will have the appropriate
2912 // precision, and these operations don't modify precision at all.
2913 break;
2914
Chris Lattner9d6fa982005-04-28 21:44:33 +00002915 case ISD::FSQRT:
2916 case ISD::FSIN:
2917 case ISD::FCOS:
2918 Tmp1 = PromoteOp(Node->getOperand(0));
2919 assert(Tmp1.getValueType() == NVT);
2920 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2921 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002922 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2923 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002924 break;
2925
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002926 case ISD::AND:
2927 case ISD::OR:
2928 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002929 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002930 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002931 case ISD::MUL:
2932 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002933 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002934 // that too is okay if they are integer operations.
2935 Tmp1 = PromoteOp(Node->getOperand(0));
2936 Tmp2 = PromoteOp(Node->getOperand(1));
2937 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2938 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002939 break;
2940 case ISD::FADD:
2941 case ISD::FSUB:
2942 case ISD::FMUL:
2943 // The input may have strange things in the top bits of the registers, but
2944 // these operations don't care.
2945 Tmp1 = PromoteOp(Node->getOperand(0));
2946 Tmp2 = PromoteOp(Node->getOperand(1));
2947 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2948 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2949
2950 // Floating point operations will give excess precision that we may not be
2951 // able to tolerate. If we DO allow excess precision, just leave it,
2952 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002953 // FIXME: Why would we need to round FP ops more than integer ones?
2954 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002955 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002956 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2957 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002958 break;
2959
Chris Lattner4d978642005-01-15 22:16:26 +00002960 case ISD::SDIV:
2961 case ISD::SREM:
2962 // These operators require that their input be sign extended.
2963 Tmp1 = PromoteOp(Node->getOperand(0));
2964 Tmp2 = PromoteOp(Node->getOperand(1));
2965 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002966 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2967 DAG.getValueType(VT));
2968 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2969 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002970 }
2971 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2972
2973 // Perform FP_ROUND: this is probably overly pessimistic.
2974 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002975 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2976 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002977 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002978 case ISD::FDIV:
2979 case ISD::FREM:
2980 // These operators require that their input be fp extended.
2981 Tmp1 = PromoteOp(Node->getOperand(0));
2982 Tmp2 = PromoteOp(Node->getOperand(1));
2983 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2984
2985 // Perform FP_ROUND: this is probably overly pessimistic.
2986 if (NoExcessFPPrecision)
2987 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2988 DAG.getValueType(VT));
2989 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002990
2991 case ISD::UDIV:
2992 case ISD::UREM:
2993 // These operators require that their input be zero extended.
2994 Tmp1 = PromoteOp(Node->getOperand(0));
2995 Tmp2 = PromoteOp(Node->getOperand(1));
2996 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002997 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2998 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002999 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3000 break;
3001
3002 case ISD::SHL:
3003 Tmp1 = PromoteOp(Node->getOperand(0));
3004 Tmp2 = LegalizeOp(Node->getOperand(1));
3005 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
3006 break;
3007 case ISD::SRA:
3008 // The input value must be properly sign extended.
3009 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003010 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3011 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003012 Tmp2 = LegalizeOp(Node->getOperand(1));
3013 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
3014 break;
3015 case ISD::SRL:
3016 // The input value must be properly zero extended.
3017 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003018 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003019 Tmp2 = LegalizeOp(Node->getOperand(1));
3020 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
3021 break;
Chris Lattner02011c92006-01-14 22:41:46 +00003022
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003023 case ISD::LOAD:
3024 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3025 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00003026 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
3027 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003028 // Remember that we legalized the chain.
3029 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3030 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00003031 case ISD::SEXTLOAD:
3032 case ISD::ZEXTLOAD:
3033 case ISD::EXTLOAD:
3034 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3035 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00003036 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
3037 Node->getOperand(2),
3038 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00003039 // Remember that we legalized the chain.
3040 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3041 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003042 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003043 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3044 case Expand: assert(0 && "It's impossible to expand bools");
3045 case Legal:
3046 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
3047 break;
3048 case Promote:
3049 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
3050 break;
3051 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003052 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3053 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
3054 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
3055 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003056 case ISD::SELECT_CC:
3057 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3058 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3059 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3060 Node->getOperand(1), Tmp2, Tmp3,
3061 Node->getOperand(4));
3062 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00003063 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00003064 case ISD::CALL: {
3065 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3066 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3067
Chris Lattner3d95c142005-01-19 20:24:35 +00003068 std::vector<SDOperand> Ops;
3069 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
3070 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3071
Chris Lattner5c8a85e2005-01-16 19:46:48 +00003072 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3073 "Can only promote single result calls");
3074 std::vector<MVT::ValueType> RetTyVTs;
3075 RetTyVTs.reserve(2);
3076 RetTyVTs.push_back(NVT);
3077 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003078 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
3079 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00003080 Result = SDOperand(NC, 0);
3081
3082 // Insert the new chain mapping.
3083 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3084 break;
Misha Brukman835702a2005-04-21 22:36:52 +00003085 }
Nate Begeman2fba8a32006-01-14 03:14:10 +00003086 case ISD::BSWAP:
3087 Tmp1 = Node->getOperand(0);
3088 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3089 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3090 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3091 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3092 TLI.getShiftAmountTy()));
3093 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003094 case ISD::CTPOP:
3095 case ISD::CTTZ:
3096 case ISD::CTLZ:
3097 Tmp1 = Node->getOperand(0);
3098 //Zero extend the argument
3099 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3100 // Perform the larger operation, then subtract if needed.
3101 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3102 switch(Node->getOpcode())
3103 {
3104 case ISD::CTPOP:
3105 Result = Tmp1;
3106 break;
3107 case ISD::CTTZ:
3108 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003109 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00003110 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003111 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003112 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
3113 break;
3114 case ISD::CTLZ:
3115 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003116 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3117 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003118 getSizeInBits(VT), NVT));
3119 break;
3120 }
3121 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003122 }
3123
3124 assert(Result.Val && "Didn't set a result!");
3125 AddPromotedOperand(Op, Result);
3126 return Result;
3127}
Chris Lattnerdc750592005-01-07 07:47:09 +00003128
Chris Lattner36e663d2005-12-23 00:16:34 +00003129/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003130/// The resultant code need not be legal. Note that SrcOp is the input operand
3131/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003132SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3133 SDOperand SrcOp) {
3134 // Create the stack frame object.
3135 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3136 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003137 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner36e663d2005-12-23 00:16:34 +00003138 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3139
3140 // Emit a store to the stack slot.
3141 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00003142 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00003143 // Result is a load from the stack slot.
3144 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3145}
3146
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003147/// ExpandAddSub - Find a clever way to expand this add operation into
3148/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00003149void SelectionDAGLegalize::
3150ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
3151 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003152 // Expand the subcomponents.
3153 SDOperand LHSL, LHSH, RHSL, RHSH;
3154 ExpandOp(LHS, LHSL, LHSH);
3155 ExpandOp(RHS, RHSL, RHSH);
3156
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003157 std::vector<SDOperand> Ops;
3158 Ops.push_back(LHSL);
3159 Ops.push_back(LHSH);
3160 Ops.push_back(RHSL);
3161 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00003162 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
3163 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003164 Hi = Lo.getValue(1);
3165}
3166
Chris Lattner4157c412005-04-02 04:00:59 +00003167void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3168 SDOperand Op, SDOperand Amt,
3169 SDOperand &Lo, SDOperand &Hi) {
3170 // Expand the subcomponents.
3171 SDOperand LHSL, LHSH;
3172 ExpandOp(Op, LHSL, LHSH);
3173
3174 std::vector<SDOperand> Ops;
3175 Ops.push_back(LHSL);
3176 Ops.push_back(LHSH);
3177 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00003178 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00003179 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00003180 Hi = Lo.getValue(1);
3181}
3182
3183
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003184/// ExpandShift - Try to find a clever way to expand this shift operation out to
3185/// smaller elements. If we can't find a way that is more efficient than a
3186/// libcall on this target, return false. Otherwise, return true with the
3187/// low-parts expanded into Lo and Hi.
3188bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3189 SDOperand &Lo, SDOperand &Hi) {
3190 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3191 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003192
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003193 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003194 SDOperand ShAmt = LegalizeOp(Amt);
3195 MVT::ValueType ShTy = ShAmt.getValueType();
3196 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3197 unsigned NVTBits = MVT::getSizeInBits(NVT);
3198
3199 // Handle the case when Amt is an immediate. Other cases are currently broken
3200 // and are disabled.
3201 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3202 unsigned Cst = CN->getValue();
3203 // Expand the incoming operand to be shifted, so that we have its parts
3204 SDOperand InL, InH;
3205 ExpandOp(Op, InL, InH);
3206 switch(Opc) {
3207 case ISD::SHL:
3208 if (Cst > VTBits) {
3209 Lo = DAG.getConstant(0, NVT);
3210 Hi = DAG.getConstant(0, NVT);
3211 } else if (Cst > NVTBits) {
3212 Lo = DAG.getConstant(0, NVT);
3213 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003214 } else if (Cst == NVTBits) {
3215 Lo = DAG.getConstant(0, NVT);
3216 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00003217 } else {
3218 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3219 Hi = DAG.getNode(ISD::OR, NVT,
3220 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3221 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3222 }
3223 return true;
3224 case ISD::SRL:
3225 if (Cst > VTBits) {
3226 Lo = DAG.getConstant(0, NVT);
3227 Hi = DAG.getConstant(0, NVT);
3228 } else if (Cst > NVTBits) {
3229 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3230 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003231 } else if (Cst == NVTBits) {
3232 Lo = InH;
3233 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003234 } else {
3235 Lo = DAG.getNode(ISD::OR, NVT,
3236 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3237 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3238 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3239 }
3240 return true;
3241 case ISD::SRA:
3242 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003243 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003244 DAG.getConstant(NVTBits-1, ShTy));
3245 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003246 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003247 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003248 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003249 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003250 } else if (Cst == NVTBits) {
3251 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003252 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003253 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003254 } else {
3255 Lo = DAG.getNode(ISD::OR, NVT,
3256 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3257 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3258 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3259 }
3260 return true;
3261 }
3262 }
3263 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
3264 // so disable it for now. Currently targets are handling this via SHL_PARTS
3265 // and friends.
3266 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003267
3268 // If we have an efficient select operation (or if the selects will all fold
3269 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003270 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003271 return false;
3272
3273 SDOperand InL, InH;
3274 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003275 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
3276 DAG.getConstant(NVTBits, ShTy), ShAmt);
3277
Chris Lattner4d25c042005-01-20 20:29:23 +00003278 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00003279 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
3280 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00003281
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003282 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
3283 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
3284 DAG.getConstant(NVTBits-1, ShTy));
3285 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
3286 DAG.getConstant(NVTBits-1, ShTy));
3287 }
3288
3289 if (Opc == ISD::SHL) {
3290 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
3291 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
3292 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00003293 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00003294
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003295 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
3296 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
3297 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00003298 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00003299 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
3300 DAG.getConstant(32, ShTy),
3301 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00003302 DAG.getConstant(0, NVT),
3303 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003304 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00003305 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003306 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00003307 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003308
3309 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00003310 if (Opc == ISD::SRA)
3311 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
3312 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003313 else
3314 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003315 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00003316 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003317 }
3318 return true;
3319}
Chris Lattneraac464e2005-01-21 06:05:23 +00003320
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003321/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
3322/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00003323/// Found.
Chris Lattner90ba5442006-01-09 23:21:49 +00003324static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found,
3325 std::set<SDNode*> &Visited) {
3326 if (Node->getNodeDepth() <= Found->getNodeDepth() ||
3327 !Visited.insert(Node).second) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00003328
Chris Lattner2dce7032005-05-12 23:24:06 +00003329 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00003330 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00003331 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003332 Found = Node;
3333 return;
3334 }
3335
3336 // Otherwise, scan the operands of Node to see if any of them is a call.
3337 assert(Node->getNumOperands() != 0 &&
3338 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00003339 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner90ba5442006-01-09 23:21:49 +00003340 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003341
3342 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003343 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner90ba5442006-01-09 23:21:49 +00003344 Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003345}
3346
3347
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003348/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
3349/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00003350/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00003351static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
3352 std::set<SDNode*> &Visited) {
3353 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
3354 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00003355
Chris Lattner2dce7032005-05-12 23:24:06 +00003356 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00003357 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00003358 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003359 Found = Node;
3360 return;
3361 }
3362
3363 // Otherwise, scan the operands of Node to see if any of them is a call.
3364 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
3365 if (UI == E) return;
3366 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00003367 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003368
3369 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00003370 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003371}
3372
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003373/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003374/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003375static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00003376 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00003377 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00003378 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003379 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00003380
Chris Lattner02011c92006-01-14 22:41:46 +00003381 // The chain is usually at the end.
Chris Lattner4add7e32005-01-23 04:42:50 +00003382 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner02011c92006-01-14 22:41:46 +00003383 if (TheChain.getValueType() != MVT::Other) {
3384 // Sometimes it's at the beginning.
Chris Lattner3268f242005-05-14 08:34:53 +00003385 TheChain = SDOperand(Node, 0);
Chris Lattner02011c92006-01-14 22:41:46 +00003386 if (TheChain.getValueType() != MVT::Other) {
3387 // Otherwise, hunt for it.
3388 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
3389 if (Node->getValueType(i) == MVT::Other) {
3390 TheChain = SDOperand(Node, i);
3391 break;
3392 }
3393
3394 // Otherwise, we walked into a node without a chain.
3395 if (TheChain.getValueType() != MVT::Other)
3396 return 0;
3397 }
3398 }
Misha Brukman835702a2005-04-21 22:36:52 +00003399
3400 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00003401 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00003402
Chris Lattner4add7e32005-01-23 04:42:50 +00003403 // Make sure to only follow users of our token chain.
3404 SDNode *User = *UI;
3405 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3406 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00003407 if (SDNode *Result = FindCallSeqEnd(User))
3408 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00003409 }
Chris Lattnercabdc342005-08-05 16:23:57 +00003410 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00003411}
3412
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003413/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003414/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003415static SDNode *FindCallSeqStart(SDNode *Node) {
3416 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00003417 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003418
3419 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3420 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003421 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003422}
3423
3424
Chris Lattner4add7e32005-01-23 04:42:50 +00003425/// FindInputOutputChains - If we are replacing an operation with a call we need
3426/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00003427/// properly serialize the calls in the block. The returned operand is the
3428/// input chain value for the new call (e.g. the entry node or the previous
3429/// call), and OutChain is set to be the chain node to update to point to the
3430/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00003431static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3432 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003433 SDNode *LatestCallSeqStart = Entry.Val;
3434 SDNode *LatestCallSeqEnd = 0;
Chris Lattner90ba5442006-01-09 23:21:49 +00003435 std::set<SDNode*> Visited;
3436 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited);
3437 Visited.clear();
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003438 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00003439
Chris Lattner2dce7032005-05-12 23:24:06 +00003440 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00003441 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00003442 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3443 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00003444 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003445 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00003446 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3447 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003448 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00003449 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003450 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00003451
Chris Lattner06bbeb62005-05-11 19:02:11 +00003452 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003453 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003454 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00003455 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner90ba5442006-01-09 23:21:49 +00003456 Visited.clear();
Chris Lattner4add7e32005-01-23 04:42:50 +00003457
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003458 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003459 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003460 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00003461
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003462 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00003463}
3464
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003465/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00003466void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3467 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00003468 // Nothing to splice it into?
3469 if (OutChain == 0) return;
3470
3471 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3472 //OutChain->dump();
3473
3474 // Form a token factor node merging the old inval and the new inval.
3475 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3476 OutChain->getOperand(0));
3477 // Change the node to refer to the new token.
3478 OutChain->setAdjCallChain(InToken);
3479}
Chris Lattner4add7e32005-01-23 04:42:50 +00003480
3481
Chris Lattneraac464e2005-01-21 06:05:23 +00003482// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3483// does not fit into a register, return the lo part and set the hi part to the
3484// by-reg argument. If it does fit into a single register, return the result
3485// and leave the Hi part unset.
3486SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3487 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003488 SDNode *OutChain;
3489 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3490 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00003491 if (InChain.Val == 0)
3492 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00003493
Chris Lattneraac464e2005-01-21 06:05:23 +00003494 TargetLowering::ArgListTy Args;
3495 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3496 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3497 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3498 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3499 }
3500 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003501
Chris Lattner06bbeb62005-05-11 19:02:11 +00003502 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003503 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003504 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003505 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3506 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003507
Chris Lattner63022662005-09-02 20:26:58 +00003508 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003509 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003510 default: assert(0 && "Unknown thing");
3511 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00003512 Result = CallInfo.first;
3513 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003514 case Promote:
3515 assert(0 && "Cannot promote this yet!");
3516 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003517 ExpandOp(CallInfo.first, Result, Hi);
3518 CallInfo.second = LegalizeOp(CallInfo.second);
3519 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003520 }
Chris Lattner63022662005-09-02 20:26:58 +00003521
3522 SpliceCallInto(CallInfo.second, OutChain);
3523 NeedsAnotherIteration = true;
3524 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003525}
3526
Chris Lattner4add7e32005-01-23 04:42:50 +00003527
Chris Lattneraac464e2005-01-21 06:05:23 +00003528/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3529/// destination type is legal.
3530SDOperand SelectionDAGLegalize::
3531ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003532 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003533 assert(getTypeAction(Source.getValueType()) == Expand &&
3534 "This is not an expansion!");
3535 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3536
Chris Lattner06bbeb62005-05-11 19:02:11 +00003537 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003538 assert(Source.getValueType() == MVT::i64 &&
3539 "This only works for 64-bit -> FP");
3540 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3541 // incoming integer is set. To handle this, we dynamically test to see if
3542 // it is set, and, if so, add a fudge factor.
3543 SDOperand Lo, Hi;
3544 ExpandOp(Source, Lo, Hi);
3545
Chris Lattner2a4f7312005-05-13 04:45:13 +00003546 // If this is unsigned, and not supported, first perform the conversion to
3547 // signed, then adjust the result if the sign bit is set.
3548 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3549 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3550
Chris Lattnerd47675e2005-08-09 20:20:18 +00003551 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3552 DAG.getConstant(0, Hi.getValueType()),
3553 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003554 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3555 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3556 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003557 uint64_t FF = 0x5f800000ULL;
3558 if (TLI.isLittleEndian()) FF <<= 32;
3559 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003560
Chris Lattnerc30405e2005-08-26 17:15:30 +00003561 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003562 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3563 SDOperand FudgeInReg;
3564 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003565 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3566 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003567 else {
3568 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003569 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3570 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003571 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003572 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003573 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003574
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003575 // Check to see if the target has a custom way to lower this. If so, use it.
3576 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3577 default: assert(0 && "This action not implemented for this operation!");
3578 case TargetLowering::Legal:
3579 case TargetLowering::Expand:
3580 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003581 case TargetLowering::Custom: {
3582 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3583 Source), DAG);
3584 if (NV.Val)
3585 return LegalizeOp(NV);
3586 break; // The target decided this was legal after all
3587 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003588 }
3589
Chris Lattner153587e2005-05-12 07:00:44 +00003590 // Expand the source, then glue it back together for the call. We must expand
3591 // the source in case it is shared (this pass of legalize must traverse it).
3592 SDOperand SrcLo, SrcHi;
3593 ExpandOp(Source, SrcLo, SrcHi);
3594 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3595
Chris Lattner06bbeb62005-05-11 19:02:11 +00003596 SDNode *OutChain = 0;
3597 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3598 DAG.getEntryNode());
3599 const char *FnName = 0;
3600 if (DestTy == MVT::f32)
3601 FnName = "__floatdisf";
3602 else {
3603 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3604 FnName = "__floatdidf";
3605 }
3606
Chris Lattneraac464e2005-01-21 06:05:23 +00003607 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3608
3609 TargetLowering::ArgListTy Args;
3610 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003611
Chris Lattneraac464e2005-01-21 06:05:23 +00003612 Args.push_back(std::make_pair(Source, ArgTy));
3613
3614 // We don't care about token chains for libcalls. We just use the entry
3615 // node as our input and ignore the output chain. This allows us to place
3616 // calls wherever we need them to satisfy data dependences.
3617 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003618
3619 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003620 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3621 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003622
Chris Lattnera5bf1032005-05-12 04:49:08 +00003623 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003624 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003625}
Misha Brukman835702a2005-04-21 22:36:52 +00003626
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003627
3628
Chris Lattnerdc750592005-01-07 07:47:09 +00003629/// ExpandOp - Expand the specified SDOperand into its two component pieces
3630/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3631/// LegalizeNodes map is filled in for any results that are not expanded, the
3632/// ExpandedNodes map is filled in for any results that are expanded, and the
3633/// Lo/Hi values are returned.
3634void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3635 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003636 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003637 SDNode *Node = Op.Val;
3638 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003639 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3640 "Cannot expand FP values!");
3641 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003642 "Cannot expand to FP value or to larger int value!");
3643
Chris Lattner1a570f12005-09-02 20:32:45 +00003644 // See if we already expanded it.
3645 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3646 = ExpandedNodes.find(Op);
3647 if (I != ExpandedNodes.end()) {
3648 Lo = I->second.first;
3649 Hi = I->second.second;
3650 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003651 }
3652
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003653 // Expanding to multiple registers needs to perform an optimization step, and
3654 // is not careful to avoid operations the target does not support. Make sure
3655 // that all generated operations are legalized in the next iteration.
3656 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003657
Chris Lattnerdc750592005-01-07 07:47:09 +00003658 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003659 case ISD::CopyFromReg:
3660 assert(0 && "CopyFromReg must be legal!");
3661 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003662 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3663 assert(0 && "Do not know how to expand this operator!");
3664 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003665 case ISD::UNDEF:
3666 Lo = DAG.getNode(ISD::UNDEF, NVT);
3667 Hi = DAG.getNode(ISD::UNDEF, NVT);
3668 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003669 case ISD::Constant: {
3670 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3671 Lo = DAG.getConstant(Cst, NVT);
3672 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3673 break;
3674 }
Nate Begemanae89d862005-12-07 19:48:11 +00003675 case ISD::ConstantVec: {
3676 unsigned NumElements = Node->getNumOperands();
3677 // If we only have two elements left in the constant vector, just break it
3678 // apart into the two scalar constants it contains. Otherwise, bisect the
3679 // ConstantVec, and return each half as a new ConstantVec.
3680 // FIXME: this is hard coded as big endian, it may have to change to support
3681 // SSE and Alpha MVI
3682 if (NumElements == 2) {
3683 Hi = Node->getOperand(0);
3684 Lo = Node->getOperand(1);
3685 } else {
3686 NumElements /= 2;
3687 std::vector<SDOperand> LoOps, HiOps;
3688 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3689 HiOps.push_back(Node->getOperand(I));
3690 LoOps.push_back(Node->getOperand(I+NumElements));
3691 }
3692 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3693 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3694 }
3695 break;
3696 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003697
Chris Lattner32e08b72005-03-28 22:03:13 +00003698 case ISD::BUILD_PAIR:
3699 // Legalize both operands. FIXME: in the future we should handle the case
3700 // where the two elements are not legal.
3701 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3702 Lo = LegalizeOp(Node->getOperand(0));
3703 Hi = LegalizeOp(Node->getOperand(1));
3704 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003705
3706 case ISD::SIGN_EXTEND_INREG:
3707 ExpandOp(Node->getOperand(0), Lo, Hi);
3708 // Sign extend the lo-part.
3709 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3710 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3711 TLI.getShiftAmountTy()));
3712 // sext_inreg the low part if needed.
3713 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3714 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003715
Nate Begeman2fba8a32006-01-14 03:14:10 +00003716 case ISD::BSWAP: {
3717 ExpandOp(Node->getOperand(0), Lo, Hi);
3718 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3719 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3720 Lo = TempLo;
3721 break;
3722 }
3723
Chris Lattner55e9cde2005-05-11 04:51:16 +00003724 case ISD::CTPOP:
3725 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003726 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3727 DAG.getNode(ISD::CTPOP, NVT, Lo),
3728 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003729 Hi = DAG.getConstant(0, NVT);
3730 break;
3731
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003732 case ISD::CTLZ: {
3733 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003734 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003735 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3736 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003737 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3738 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003739 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3740 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3741
3742 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3743 Hi = DAG.getConstant(0, NVT);
3744 break;
3745 }
3746
3747 case ISD::CTTZ: {
3748 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003749 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003750 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3751 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003752 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3753 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003754 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3755 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3756
3757 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3758 Hi = DAG.getConstant(0, NVT);
3759 break;
3760 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003761
Chris Lattnerdc750592005-01-07 07:47:09 +00003762 case ISD::LOAD: {
3763 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3764 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003765 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003766
3767 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003768 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003769 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3770 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003771 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003772 //are from the same instruction?
3773 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003774
3775 // Build a factor node to remember that this load is independent of the
3776 // other one.
3777 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3778 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003779
Chris Lattnerdc750592005-01-07 07:47:09 +00003780 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003781 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003782 if (!TLI.isLittleEndian())
3783 std::swap(Lo, Hi);
3784 break;
3785 }
Nate Begemand37c1312005-11-22 18:16:00 +00003786 case ISD::VLOAD: {
3787 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3788 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3789 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3790 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3791
3792 // If we only have two elements, turn into a pair of scalar loads.
3793 // FIXME: handle case where a vector of two elements is fine, such as
3794 // 2 x double on SSE2.
3795 if (NumElements == 2) {
3796 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3797 // Increment the pointer to the other half.
3798 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3799 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3800 getIntPtrConstant(IncrementSize));
3801 //Is this safe? declaring that the two parts of the split load
3802 //are from the same instruction?
3803 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3804 } else {
3805 NumElements /= 2; // Split the vector in half
3806 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3807 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3808 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3809 getIntPtrConstant(IncrementSize));
3810 //Is this safe? declaring that the two parts of the split load
3811 //are from the same instruction?
3812 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3813 }
3814
3815 // Build a factor node to remember that this load is independent of the
3816 // other one.
3817 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3818 Hi.getValue(1));
3819
3820 // Remember that we legalized the chain.
3821 AddLegalizedOperand(Op.getValue(1), TF);
3822 if (!TLI.isLittleEndian())
3823 std::swap(Lo, Hi);
3824 break;
3825 }
3826 case ISD::VADD:
3827 case ISD::VSUB:
3828 case ISD::VMUL: {
3829 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3830 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3831 SDOperand LL, LH, RL, RH;
3832
3833 ExpandOp(Node->getOperand(0), LL, LH);
3834 ExpandOp(Node->getOperand(1), RL, RH);
3835
3836 // If we only have two elements, turn into a pair of scalar loads.
3837 // FIXME: handle case where a vector of two elements is fine, such as
3838 // 2 x double on SSE2.
3839 if (NumElements == 2) {
3840 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3841 Lo = DAG.getNode(Opc, EVT, LL, RL);
3842 Hi = DAG.getNode(Opc, EVT, LH, RH);
3843 } else {
3844 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3845 LL.getOperand(3));
3846 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3847 LH.getOperand(3));
3848 }
3849 break;
3850 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003851 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003852 case ISD::CALL: {
3853 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3854 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3855
Chris Lattner3d95c142005-01-19 20:24:35 +00003856 bool Changed = false;
3857 std::vector<SDOperand> Ops;
3858 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3859 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3860 Changed |= Ops.back() != Node->getOperand(i);
3861 }
3862
Chris Lattnerdc750592005-01-07 07:47:09 +00003863 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3864 "Can only expand a call once so far, not i64 -> i16!");
3865
3866 std::vector<MVT::ValueType> RetTyVTs;
3867 RetTyVTs.reserve(3);
3868 RetTyVTs.push_back(NVT);
3869 RetTyVTs.push_back(NVT);
3870 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003871 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3872 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003873 Lo = SDOperand(NC, 0);
3874 Hi = SDOperand(NC, 1);
3875
3876 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003877 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003878 break;
3879 }
3880 case ISD::AND:
3881 case ISD::OR:
3882 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3883 SDOperand LL, LH, RL, RH;
3884 ExpandOp(Node->getOperand(0), LL, LH);
3885 ExpandOp(Node->getOperand(1), RL, RH);
3886 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3887 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3888 break;
3889 }
3890 case ISD::SELECT: {
3891 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003892
3893 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3894 case Expand: assert(0 && "It's impossible to expand bools");
3895 case Legal:
3896 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3897 break;
3898 case Promote:
3899 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3900 break;
3901 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003902 ExpandOp(Node->getOperand(1), LL, LH);
3903 ExpandOp(Node->getOperand(2), RL, RH);
3904 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3905 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3906 break;
3907 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003908 case ISD::SELECT_CC: {
3909 SDOperand TL, TH, FL, FH;
3910 ExpandOp(Node->getOperand(2), TL, TH);
3911 ExpandOp(Node->getOperand(3), FL, FH);
3912 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3913 Node->getOperand(1), TL, FL, Node->getOperand(4));
3914 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3915 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003916 Lo = LegalizeOp(Lo);
3917 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003918 break;
3919 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003920 case ISD::SEXTLOAD: {
3921 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3922 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3923 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3924
3925 if (EVT == NVT)
3926 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3927 else
3928 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3929 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003930
3931 // Remember that we legalized the chain.
3932 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3933
Nate Begemanc3a89c52005-10-13 17:15:37 +00003934 // The high part is obtained by SRA'ing all but one of the bits of the lo
3935 // part.
3936 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3937 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3938 TLI.getShiftAmountTy()));
3939 Lo = LegalizeOp(Lo);
3940 Hi = LegalizeOp(Hi);
3941 break;
3942 }
3943 case ISD::ZEXTLOAD: {
3944 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3945 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3946 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3947
3948 if (EVT == NVT)
3949 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3950 else
3951 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3952 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003953
3954 // Remember that we legalized the chain.
3955 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3956
Nate Begemanc3a89c52005-10-13 17:15:37 +00003957 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003958 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003959 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003960 break;
3961 }
3962 case ISD::EXTLOAD: {
3963 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3964 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3965 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3966
3967 if (EVT == NVT)
3968 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3969 else
3970 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3971 EVT);
3972
3973 // Remember that we legalized the chain.
3974 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3975
3976 // The high part is undefined.
3977 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3978 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003979 break;
3980 }
Chris Lattner7753f172005-09-02 00:18:10 +00003981 case ISD::ANY_EXTEND: {
3982 SDOperand In;
3983 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3984 case Expand: assert(0 && "expand-expand not implemented yet!");
3985 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3986 case Promote:
3987 In = PromoteOp(Node->getOperand(0));
3988 break;
3989 }
3990
3991 // The low part is any extension of the input (which degenerates to a copy).
3992 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3993 // The high part is undefined.
3994 Hi = DAG.getNode(ISD::UNDEF, NVT);
3995 break;
3996 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003997 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003998 SDOperand In;
3999 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4000 case Expand: assert(0 && "expand-expand not implemented yet!");
4001 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
4002 case Promote:
4003 In = PromoteOp(Node->getOperand(0));
4004 // Emit the appropriate sign_extend_inreg to get the value we want.
4005 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00004006 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00004007 break;
4008 }
4009
Chris Lattnerdc750592005-01-07 07:47:09 +00004010 // The low part is just a sign extension of the input (which degenerates to
4011 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00004012 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00004013
Chris Lattnerdc750592005-01-07 07:47:09 +00004014 // The high part is obtained by SRA'ing all but one of the bits of the lo
4015 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004016 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00004017 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4018 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004019 break;
4020 }
Chris Lattner47844892005-04-03 23:41:52 +00004021 case ISD::ZERO_EXTEND: {
4022 SDOperand In;
4023 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4024 case Expand: assert(0 && "expand-expand not implemented yet!");
4025 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
4026 case Promote:
4027 In = PromoteOp(Node->getOperand(0));
4028 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00004029 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00004030 break;
4031 }
4032
Chris Lattnerdc750592005-01-07 07:47:09 +00004033 // The low part is just a zero extension of the input (which degenerates to
4034 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00004035 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00004036
Chris Lattnerdc750592005-01-07 07:47:09 +00004037 // The high part is just a zero.
4038 Hi = DAG.getConstant(0, NVT);
4039 break;
Chris Lattner47844892005-04-03 23:41:52 +00004040 }
Chris Lattner36e663d2005-12-23 00:16:34 +00004041
4042 case ISD::BIT_CONVERT: {
4043 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4044 Node->getOperand(0));
4045 ExpandOp(Tmp, Lo, Hi);
4046 break;
4047 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004048
Chris Lattner44c28c22005-11-20 22:56:56 +00004049 case ISD::READCYCLECOUNTER: {
4050 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4051 TargetLowering::Custom &&
4052 "Must custom expand ReadCycleCounter");
4053 SDOperand T = TLI.LowerOperation(Op, DAG);
4054 assert(T.Val && "Node must be custom expanded!");
4055 Lo = LegalizeOp(T.getValue(0));
4056 Hi = LegalizeOp(T.getValue(1));
4057 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
4058 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004059 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00004060 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004061
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004062 // These operators cannot be expanded directly, emit them as calls to
4063 // library functions.
4064 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004065 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004066 SDOperand Op;
4067 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4068 case Expand: assert(0 && "cannot expand FP!");
4069 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4070 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
4071 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004072
Chris Lattner941d84a2005-07-30 01:40:57 +00004073 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4074
Chris Lattnerfe68d752005-07-29 00:33:32 +00004075 // Now that the custom expander is done, expand the result, which is still
4076 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004077 if (Op.Val) {
4078 ExpandOp(Op, Lo, Hi);
4079 break;
4080 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004081 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004082
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004083 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004084 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004085 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004086 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004087 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004088
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004089 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004090 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
4091 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
4092 LegalizeOp(Node->getOperand(0)));
4093 // Now that the custom expander is done, expand the result, which is still
4094 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004095 Op = TLI.LowerOperation(Op, DAG);
4096 if (Op.Val) {
4097 ExpandOp(Op, Lo, Hi);
4098 break;
4099 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004100 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004101
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004102 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004103 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004104 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004105 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004106 break;
4107
Evan Cheng870e4f82006-01-09 18:31:59 +00004108 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004109 // If the target wants custom lowering, do so.
4110 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
4111 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
4112 LegalizeOp(Node->getOperand(1)));
4113 Op = TLI.LowerOperation(Op, DAG);
4114 if (Op.Val) {
4115 // Now that the custom expander is done, expand the result, which is
4116 // still VT.
4117 ExpandOp(Op, Lo, Hi);
4118 break;
4119 }
4120 }
4121
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004122 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00004123 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004124 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004125
4126 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004127 TargetLowering::LegalizeAction Action =
4128 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4129 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4130 Action == TargetLowering::Custom) {
Chris Lattner4157c412005-04-02 04:00:59 +00004131 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
4132 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004133 break;
4134 }
4135
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004136 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004137 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004138 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004139 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004140
Evan Cheng870e4f82006-01-09 18:31:59 +00004141 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004142 // If the target wants custom lowering, do so.
4143 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
4144 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
4145 LegalizeOp(Node->getOperand(1)));
4146 Op = TLI.LowerOperation(Op, DAG);
4147 if (Op.Val) {
4148 // Now that the custom expander is done, expand the result, which is
4149 // still VT.
4150 ExpandOp(Op, Lo, Hi);
4151 break;
4152 }
4153 }
4154
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004155 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00004156 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004157 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004158
4159 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004160 TargetLowering::LegalizeAction Action =
4161 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4162 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4163 Action == TargetLowering::Custom) {
Chris Lattner4157c412005-04-02 04:00:59 +00004164 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
4165 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004166 break;
4167 }
4168
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004169 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004170 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004171 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004172 }
4173
4174 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004175 // If the target wants custom lowering, do so.
4176 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
4177 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
4178 LegalizeOp(Node->getOperand(1)));
4179 Op = TLI.LowerOperation(Op, DAG);
4180 if (Op.Val) {
4181 // Now that the custom expander is done, expand the result, which is
4182 // still VT.
4183 ExpandOp(Op, Lo, Hi);
4184 break;
4185 }
4186 }
4187
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004188 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00004189 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004190 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004191
4192 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004193 TargetLowering::LegalizeAction Action =
4194 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4195 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4196 Action == TargetLowering::Custom) {
Chris Lattner4157c412005-04-02 04:00:59 +00004197 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
4198 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004199 break;
4200 }
4201
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004202 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004203 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004204 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004205 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004206
Misha Brukman835702a2005-04-21 22:36:52 +00004207 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00004208 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
4209 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004210 break;
4211 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00004212 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
4213 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004214 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00004215 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004216 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004217 SDOperand LL, LH, RL, RH;
4218 ExpandOp(Node->getOperand(0), LL, LH);
4219 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004220 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4221 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4222 // extended the sign bit of the low half through the upper half, and if so
4223 // emit a MULHS instead of the alternate sequence that is valid for any
4224 // i64 x i64 multiply.
4225 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4226 // is RH an extension of the sign bit of RL?
4227 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4228 RH.getOperand(1).getOpcode() == ISD::Constant &&
4229 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4230 // is LH an extension of the sign bit of LL?
4231 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4232 LH.getOperand(1).getOpcode() == ISD::Constant &&
4233 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4234 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4235 } else {
4236 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4237 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4238 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4239 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4240 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4241 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004242 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4243 } else {
4244 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
4245 }
4246 break;
4247 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004248 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4249 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4250 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4251 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004252 }
4253
Chris Lattnerac12f682005-12-21 18:02:52 +00004254 // Make sure the resultant values have been legalized themselves, unless this
4255 // is a type that requires multi-step expansion.
4256 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4257 Lo = LegalizeOp(Lo);
4258 Hi = LegalizeOp(Hi);
4259 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004260
4261 // Remember in a map if the values will be reused later.
4262 bool isNew =
4263 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4264 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004265}
4266
4267
4268// SelectionDAG::Legalize - This is the entry point for the file.
4269//
Chris Lattner4add7e32005-01-23 04:42:50 +00004270void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004271 /// run - This is the main entry point to this class.
4272 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00004273 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00004274}
4275