blob: 1f67e650e54e9acc3dfe505ed2f3b3fe6322008e [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman6a648612005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000056
Chris Lattner3e928bb2005-01-07 07:47:09 +000057 /// LegalizedNodes - For nodes that are of legal width, and that have more
58 /// than one use, this map indicates what regularized operand to use. This
59 /// allows us to avoid legalizing the same thing more than once.
60 std::map<SDOperand, SDOperand> LegalizedNodes;
61
Chris Lattner03c85462005-01-15 05:21:40 +000062 /// PromotedNodes - For nodes that are below legal width, and that have more
63 /// than one use, this map indicates what promoted value to use. This allows
64 /// us to avoid promoting the same thing more than once.
65 std::map<SDOperand, SDOperand> PromotedNodes;
66
Chris Lattner3e928bb2005-01-07 07:47:09 +000067 /// ExpandedNodes - For nodes that need to be expanded, and which have more
68 /// than one use, this map indicates which which operands are the expanded
69 /// version of the input. This allows us to avoid expanding the same node
70 /// more than once.
71 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
72
Chris Lattner8afc48e2005-01-07 22:28:47 +000073 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000074 LegalizedNodes.insert(std::make_pair(From, To));
75 // If someone requests legalization of the new node, return itself.
76 if (From != To)
77 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000078 }
Chris Lattner03c85462005-01-15 05:21:40 +000079 void AddPromotedOperand(SDOperand From, SDOperand To) {
80 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +000082 // If someone requests legalization of the new node, return itself.
83 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +000084 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000085
Chris Lattner3e928bb2005-01-07 07:47:09 +000086public:
87
Chris Lattner9c32d3b2005-01-23 04:42:50 +000088 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000089
Chris Lattner3e928bb2005-01-07 07:47:09 +000090 /// getTypeAction - Return how we should legalize values of this type, either
91 /// it is already legal or we need to expand it into multiple registers of
92 /// smaller integer type, or we need to promote it to a larger type.
93 LegalizeAction getTypeAction(MVT::ValueType VT) const {
94 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
95 }
96
97 /// isTypeLegal - Return true if this type is legal on this target.
98 ///
99 bool isTypeLegal(MVT::ValueType VT) const {
100 return getTypeAction(VT) == Legal;
101 }
102
Chris Lattner3e928bb2005-01-07 07:47:09 +0000103 void LegalizeDAG();
104
Chris Lattner456a93a2006-01-28 07:39:30 +0000105private:
106
Chris Lattner3e928bb2005-01-07 07:47:09 +0000107 SDOperand LegalizeOp(SDOperand O);
108 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000109 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000110
Chris Lattner77e77a62005-01-21 06:05:23 +0000111 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
112 SDOperand &Hi);
113 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
114 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000115
Chris Lattner35481892005-12-23 00:16:34 +0000116 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskey6269ed12005-08-17 00:39:29 +0000117 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
118 SDOperand LegalOp,
119 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000120 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
121 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000122 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
123 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000124
Chris Lattner456a93a2006-01-28 07:39:30 +0000125 SDOperand ExpandBSWAP(SDOperand Op);
126 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000127 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
128 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000129 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
130 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000131 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
132
Chris Lattner3e928bb2005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
Chris Lattnerb89175f2005-11-19 05:51:46 +0000139static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000140 switch (VecOp) {
141 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000142 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
143 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
144 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
145 }
146}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000147
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000148SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
149 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
150 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000151 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000152 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000153}
154
Jim Laskey6269ed12005-08-17 00:39:29 +0000155/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
156/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000157/// we expand it. At this point, we know that the result and operand types are
158/// legal for the target.
Jim Laskey6269ed12005-08-17 00:39:29 +0000159SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
160 SDOperand Op0,
161 MVT::ValueType DestVT) {
162 if (Op0.getValueType() == MVT::i32) {
163 // simple 32-bit [signed|unsigned] integer to float/double expansion
164
165 // get the stack frame index of a 8 byte buffer
166 MachineFunction &MF = DAG.getMachineFunction();
167 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
168 // get address of 8 byte buffer
169 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
170 // word offset constant for Hi/Lo address computation
171 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
172 // set up Hi and Lo (into buffer) address based on endian
173 SDOperand Hi, Lo;
174 if (TLI.isLittleEndian()) {
175 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
176 Lo = StackSlot;
177 } else {
178 Hi = StackSlot;
179 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
180 }
181 // if signed map to unsigned space
182 SDOperand Op0Mapped;
183 if (isSigned) {
184 // constant used to invert sign bit (signed to unsigned mapping)
185 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
186 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
187 } else {
188 Op0Mapped = Op0;
189 }
190 // store the lo of the constructed double - based on integer input
191 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
192 Op0Mapped, Lo, DAG.getSrcValue(NULL));
193 // initial hi portion of constructed double
194 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
195 // store the hi of the constructed double - biased exponent
196 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
197 InitialHi, Hi, DAG.getSrcValue(NULL));
198 // load the constructed double
199 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
200 DAG.getSrcValue(NULL));
201 // FP constant to bias correct the final result
Jim Laskey02659d22005-08-17 17:42:52 +0000202 SDOperand Bias = DAG.getConstantFP(isSigned ?
203 BitsToDouble(0x4330000080000000ULL)
204 : BitsToDouble(0x4330000000000000ULL),
Jim Laskey6269ed12005-08-17 00:39:29 +0000205 MVT::f64);
206 // subtract the bias
Chris Lattner01b3d732005-09-28 22:28:18 +0000207 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskey6269ed12005-08-17 00:39:29 +0000208 // final result
209 SDOperand Result;
210 // handle final rounding
211 if (DestVT == MVT::f64) {
212 // do nothing
213 Result = Sub;
214 } else {
215 // if f32 then cast to f32
216 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
217 }
Chris Lattner69a889e2005-12-20 00:53:54 +0000218 return LegalizeOp(Result);
Jim Laskey6269ed12005-08-17 00:39:29 +0000219 }
220 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnercad063f2005-07-16 00:19:57 +0000221 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000222
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000223 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
224 DAG.getConstant(0, Op0.getValueType()),
225 ISD::SETLT);
Chris Lattnercad063f2005-07-16 00:19:57 +0000226 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
227 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
228 SignSet, Four, Zero);
Jeff Cohen00b168892005-07-27 06:12:32 +0000229
Jim Laskey6269ed12005-08-17 00:39:29 +0000230 // If the sign bit of the integer is set, the large number will be treated
231 // as a negative number. To counteract this, the dynamic code adds an
232 // offset depending on the data type.
Chris Lattnerf4d32722005-07-18 04:31:14 +0000233 uint64_t FF;
234 switch (Op0.getValueType()) {
235 default: assert(0 && "Unsupported integer type!");
236 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
237 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
238 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
239 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
240 }
Chris Lattnercad063f2005-07-16 00:19:57 +0000241 if (TLI.isLittleEndian()) FF <<= 32;
242 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000243
Chris Lattner5839bf22005-08-26 17:15:30 +0000244 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnercad063f2005-07-16 00:19:57 +0000245 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
246 SDOperand FudgeInReg;
247 if (DestVT == MVT::f32)
248 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
249 DAG.getSrcValue(NULL));
250 else {
251 assert(DestVT == MVT::f64 && "Unexpected conversion");
252 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
253 DAG.getEntryNode(), CPIdx,
254 DAG.getSrcValue(NULL), MVT::f32));
255 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000256
Chris Lattner69a889e2005-12-20 00:53:54 +0000257 return LegalizeOp(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
Chris Lattnercad063f2005-07-16 00:19:57 +0000258}
259
Chris Lattner149c58c2005-08-16 18:17:10 +0000260/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner1618beb2005-07-29 00:11:56 +0000261/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000262/// we promote it. At this point, we know that the result and operand types are
263/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
264/// operation that takes a larger input.
Nate Begeman5a8441e2005-07-16 02:02:34 +0000265SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
266 MVT::ValueType DestVT,
267 bool isSigned) {
Chris Lattnercad063f2005-07-16 00:19:57 +0000268 // First step, figure out the appropriate *INT_TO_FP operation to use.
269 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen00b168892005-07-27 06:12:32 +0000270
Chris Lattnercad063f2005-07-16 00:19:57 +0000271 unsigned OpToUse = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000272
Chris Lattnercad063f2005-07-16 00:19:57 +0000273 // Scan for the appropriate larger type to use.
274 while (1) {
275 NewInTy = (MVT::ValueType)(NewInTy+1);
276 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000277
Chris Lattnercad063f2005-07-16 00:19:57 +0000278 // If the target supports SINT_TO_FP of this type, use it.
279 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
280 default: break;
281 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000282 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000283 break; // Can't use this datatype.
284 // FALL THROUGH.
285 case TargetLowering::Custom:
286 OpToUse = ISD::SINT_TO_FP;
287 break;
288 }
289 if (OpToUse) break;
Nate Begeman5a8441e2005-07-16 02:02:34 +0000290 if (isSigned) continue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000291
Chris Lattnercad063f2005-07-16 00:19:57 +0000292 // If the target supports UINT_TO_FP of this type, use it.
293 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
294 default: break;
295 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000296 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000297 break; // Can't use this datatype.
298 // FALL THROUGH.
299 case TargetLowering::Custom:
300 OpToUse = ISD::UINT_TO_FP;
301 break;
302 }
303 if (OpToUse) break;
Jeff Cohen00b168892005-07-27 06:12:32 +0000304
Chris Lattnercad063f2005-07-16 00:19:57 +0000305 // Otherwise, try a larger type.
306 }
307
Chris Lattnercad063f2005-07-16 00:19:57 +0000308 // Okay, we found the operation and type to use. Zero extend our input to the
309 // desired type then run the operation on it.
Chris Lattner69a889e2005-12-20 00:53:54 +0000310 SDOperand N = DAG.getNode(OpToUse, DestVT,
Nate Begeman5a8441e2005-07-16 02:02:34 +0000311 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
312 NewInTy, LegalOp));
Chris Lattner69a889e2005-12-20 00:53:54 +0000313 // Make sure to legalize any nodes we create here.
314 return LegalizeOp(N);
Chris Lattnercad063f2005-07-16 00:19:57 +0000315}
316
Chris Lattner1618beb2005-07-29 00:11:56 +0000317/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
318/// FP_TO_*INT operation of the specified operand when the target requests that
319/// we promote it. At this point, we know that the result and operand types are
320/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
321/// operation that returns a larger result.
322SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
323 MVT::ValueType DestVT,
324 bool isSigned) {
325 // First step, figure out the appropriate FP_TO*INT operation to use.
326 MVT::ValueType NewOutTy = DestVT;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000327
Chris Lattner1618beb2005-07-29 00:11:56 +0000328 unsigned OpToUse = 0;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000329
Chris Lattner1618beb2005-07-29 00:11:56 +0000330 // Scan for the appropriate larger type to use.
331 while (1) {
332 NewOutTy = (MVT::ValueType)(NewOutTy+1);
333 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000334
Chris Lattner1618beb2005-07-29 00:11:56 +0000335 // If the target supports FP_TO_SINT returning this type, use it.
336 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
337 default: break;
338 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000339 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000340 break; // Can't use this datatype.
341 // FALL THROUGH.
342 case TargetLowering::Custom:
343 OpToUse = ISD::FP_TO_SINT;
344 break;
345 }
346 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000347
Chris Lattner1618beb2005-07-29 00:11:56 +0000348 // If the target supports FP_TO_UINT of this type, use it.
349 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
350 default: break;
351 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000352 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000353 break; // Can't use this datatype.
354 // FALL THROUGH.
355 case TargetLowering::Custom:
356 OpToUse = ISD::FP_TO_UINT;
357 break;
358 }
359 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000360
Chris Lattner1618beb2005-07-29 00:11:56 +0000361 // Otherwise, try a larger type.
362 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000363
Chris Lattner1618beb2005-07-29 00:11:56 +0000364 // Okay, we found the operation and type to use. Truncate the result of the
365 // extended FP_TO_*INT operation to the desired size.
Chris Lattner69a889e2005-12-20 00:53:54 +0000366 SDOperand N = DAG.getNode(ISD::TRUNCATE, DestVT,
367 DAG.getNode(OpToUse, NewOutTy, LegalOp));
368 // Make sure to legalize any nodes we create here in the next pass.
369 return LegalizeOp(N);
Chris Lattner1618beb2005-07-29 00:11:56 +0000370}
371
Chris Lattner456a93a2006-01-28 07:39:30 +0000372/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
373///
374SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
375 MVT::ValueType VT = Op.getValueType();
376 MVT::ValueType SHVT = TLI.getShiftAmountTy();
377 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
378 switch (VT) {
379 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
380 case MVT::i16:
381 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
382 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
383 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
384 case MVT::i32:
385 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
386 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
387 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
388 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
389 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
390 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
391 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
392 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
393 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
394 case MVT::i64:
395 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
396 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
397 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
398 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
399 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
400 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
401 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
402 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
403 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
404 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
405 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
406 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
407 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
408 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
409 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
410 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
411 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
412 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
413 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
414 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
415 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
416 }
417}
418
419/// ExpandBitCount - Expand the specified bitcount instruction into operations.
420///
421SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
422 switch (Opc) {
423 default: assert(0 && "Cannot expand this yet!");
424 case ISD::CTPOP: {
425 static const uint64_t mask[6] = {
426 0x5555555555555555ULL, 0x3333333333333333ULL,
427 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
428 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
429 };
430 MVT::ValueType VT = Op.getValueType();
431 MVT::ValueType ShVT = TLI.getShiftAmountTy();
432 unsigned len = getSizeInBits(VT);
433 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
434 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
435 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
436 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
437 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
438 DAG.getNode(ISD::AND, VT,
439 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
440 }
441 return Op;
442 }
443 case ISD::CTLZ: {
444 // for now, we do this:
445 // x = x | (x >> 1);
446 // x = x | (x >> 2);
447 // ...
448 // x = x | (x >>16);
449 // x = x | (x >>32); // for 64-bit input
450 // return popcount(~x);
451 //
452 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
453 MVT::ValueType VT = Op.getValueType();
454 MVT::ValueType ShVT = TLI.getShiftAmountTy();
455 unsigned len = getSizeInBits(VT);
456 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
457 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
458 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
459 }
460 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
461 return DAG.getNode(ISD::CTPOP, VT, Op);
462 }
463 case ISD::CTTZ: {
464 // for now, we use: { return popcount(~x & (x - 1)); }
465 // unless the target has ctlz but not ctpop, in which case we use:
466 // { return 32 - nlz(~x & (x-1)); }
467 // see also http://www.hackersdelight.org/HDcode/ntz.cc
468 MVT::ValueType VT = Op.getValueType();
469 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
470 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
471 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
472 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
473 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
474 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
475 TLI.isOperationLegal(ISD::CTLZ, VT))
476 return DAG.getNode(ISD::SUB, VT,
477 DAG.getConstant(getSizeInBits(VT), VT),
478 DAG.getNode(ISD::CTLZ, VT, Tmp3));
479 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
480 }
481 }
482}
483
484
Chris Lattner32fca002005-10-06 01:20:27 +0000485/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
486/// not been visited yet and if all of its operands have already been visited.
487static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
488 std::map<SDNode*, unsigned> &Visited) {
489 if (++Visited[N] != N->getNumOperands())
490 return; // Haven't visited all operands yet
491
492 Order.push_back(N);
493
494 if (N->hasOneUse()) { // Tail recurse in common case.
495 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
496 return;
497 }
498
499 // Now that we have N in, add anything that uses it if all of their operands
500 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000501 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
502 ComputeTopDownOrdering(*UI, Order, Visited);
503}
504
Chris Lattner1618beb2005-07-29 00:11:56 +0000505
Chris Lattner3e928bb2005-01-07 07:47:09 +0000506void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattnerab510a72005-10-02 17:49:46 +0000507 // The legalize process is inherently a bottom-up recursive process (users
508 // legalize their uses before themselves). Given infinite stack space, we
509 // could just start legalizing on the root and traverse the whole graph. In
510 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000511 // blocks. To avoid this problem, compute an ordering of the nodes where each
512 // node is only legalized after all of its operands are legalized.
513 std::map<SDNode*, unsigned> Visited;
514 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000515
Chris Lattner32fca002005-10-06 01:20:27 +0000516 // Compute ordering from all of the leaves in the graphs, those (like the
517 // entry node) that have no operands.
518 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
519 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000520 if (I->getNumOperands() == 0) {
521 Visited[I] = 0 - 1U;
522 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000523 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000524 }
525
Chris Lattnerde202b32005-11-09 23:47:37 +0000526 assert(Order.size() == Visited.size() &&
527 Order.size() ==
528 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000529 "Error: DAG is cyclic!");
530 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000531
Chris Lattner32fca002005-10-06 01:20:27 +0000532 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
533 SDNode *N = Order[i];
534 switch (getTypeAction(N->getValueType(0))) {
535 default: assert(0 && "Bad type action!");
536 case Legal:
537 LegalizeOp(SDOperand(N, 0));
538 break;
539 case Promote:
540 PromoteOp(SDOperand(N, 0));
541 break;
542 case Expand: {
543 SDOperand X, Y;
544 ExpandOp(SDOperand(N, 0), X, Y);
545 break;
546 }
547 }
548 }
549
550 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000551 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000552 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
553 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000554
555 ExpandedNodes.clear();
556 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000557 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000558
559 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000560 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000561}
562
563SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000564 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000565 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000566 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000567
Chris Lattner3e928bb2005-01-07 07:47:09 +0000568 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000569 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000570 if (Node->getNumValues() > 1) {
571 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
572 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000573 case Legal: break; // Nothing to do.
574 case Expand: {
575 SDOperand T1, T2;
576 ExpandOp(Op.getValue(i), T1, T2);
577 assert(LegalizedNodes.count(Op) &&
578 "Expansion didn't add legal operands!");
579 return LegalizedNodes[Op];
580 }
581 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000582 PromoteOp(Op.getValue(i));
583 assert(LegalizedNodes.count(Op) &&
Chris Lattner456a93a2006-01-28 07:39:30 +0000584 "Promotion didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000585 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000586 }
587 }
588
Chris Lattner45982da2005-05-12 16:53:42 +0000589 // Note that LegalizeOp may be reentered even from single-use nodes, which
590 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000591 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
592 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000593
Nate Begeman9373a812005-08-10 20:51:12 +0000594 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000595 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000596 bool isCustom = false;
597
Chris Lattner3e928bb2005-01-07 07:47:09 +0000598 switch (Node->getOpcode()) {
599 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000600 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
601 // If this is a target node, legalize it by legalizing the operands then
602 // passing it through.
603 std::vector<SDOperand> Ops;
604 bool Changed = false;
605 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
606 Ops.push_back(LegalizeOp(Node->getOperand(i)));
607 Changed = Changed || Node->getOperand(i) != Ops.back();
608 }
609 if (Changed)
610 if (Node->getNumValues() == 1)
611 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
612 else {
613 std::vector<MVT::ValueType> VTs(Node->value_begin(),
614 Node->value_end());
615 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
616 }
617
618 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
619 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
620 return Result.getValue(Op.ResNo);
621 }
622 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000623 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
624 assert(0 && "Do not know how to legalize this operator!");
625 abort();
626 case ISD::EntryToken:
627 case ISD::FrameIndex:
Chris Lattner32fca002005-10-06 01:20:27 +0000628 case ISD::TargetFrameIndex:
629 case ISD::Register:
630 case ISD::TargetConstant:
Nate Begeman28a6b022005-12-10 02:36:00 +0000631 case ISD::TargetConstantPool:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000632 case ISD::GlobalAddress:
Chris Lattnerb9debbf2005-11-17 05:52:24 +0000633 case ISD::TargetGlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000634 case ISD::ExternalSymbol:
Andrew Lenharthe8f65f12005-12-24 23:42:32 +0000635 case ISD::TargetExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000636 case ISD::ConstantPool: // Nothing to do.
Chris Lattner32fca002005-10-06 01:20:27 +0000637 case ISD::BasicBlock:
638 case ISD::CONDCODE:
639 case ISD::VALUETYPE:
640 case ISD::SRCVALUE:
Chris Lattner36ce6912005-11-29 06:21:05 +0000641 case ISD::STRING:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000642 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
643 default: assert(0 && "This action is not supported yet!");
644 case TargetLowering::Custom: {
645 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
646 if (Tmp.Val) {
Chris Lattner456a93a2006-01-28 07:39:30 +0000647 Result = Tmp;
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000648 break;
649 }
650 } // FALLTHROUGH if the target doesn't want to lower this op after all.
651 case TargetLowering::Legal:
652 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
653 break;
654 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000655 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000656 case ISD::AssertSext:
657 case ISD::AssertZext:
658 Tmp1 = LegalizeOp(Node->getOperand(0));
659 if (Tmp1 != Node->getOperand(0))
660 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
661 Node->getOperand(1));
662 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000663 case ISD::MERGE_VALUES:
Chris Lattner456a93a2006-01-28 07:39:30 +0000664 Result = Node->getOperand(Op.ResNo);
665 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000666 case ISD::CopyFromReg:
667 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000668 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000669 if (Node->getNumValues() == 2) {
Chris Lattner7310fb12005-12-18 15:27:43 +0000670 if (Tmp1 != Node->getOperand(0))
671 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000672 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattner7310fb12005-12-18 15:27:43 +0000673 Node->getValueType(0));
674 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000675 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
676 if (Node->getNumOperands() == 3)
677 Tmp2 = LegalizeOp(Node->getOperand(2));
678 if (Tmp1 != Node->getOperand(0) ||
679 (Node->getNumOperands() == 3 && Tmp2 != Node->getOperand(2)))
Chris Lattner7310fb12005-12-18 15:27:43 +0000680 Result = DAG.getCopyFromReg(Tmp1,
681 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
682 Node->getValueType(0), Tmp2);
683 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
684 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000685 // Since CopyFromReg produces two values, make sure to remember that we
686 // legalized both of them.
687 AddLegalizedOperand(Op.getValue(0), Result);
688 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
689 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000690 case ISD::UNDEF: {
691 MVT::ValueType VT = Op.getValueType();
692 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000693 default: assert(0 && "This action is not supported yet!");
694 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000695 if (MVT::isInteger(VT))
696 Result = DAG.getConstant(0, VT);
697 else if (MVT::isFloatingPoint(VT))
698 Result = DAG.getConstantFP(0, VT);
699 else
700 assert(0 && "Unknown value type!");
701 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000702 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000703 break;
704 }
705 break;
706 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000707
708 case ISD::LOCATION:
709 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
710 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
711
712 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
713 case TargetLowering::Promote:
714 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000715 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000716 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000717 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
718 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
719
720 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
721 const std::string &FName =
722 cast<StringSDNode>(Node->getOperand(3))->getValue();
723 const std::string &DirName =
724 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000725 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000726
Jim Laskeye81aecb2005-12-21 20:51:37 +0000727 std::vector<SDOperand> Ops;
728 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000729 SDOperand LineOp = Node->getOperand(1);
730 SDOperand ColOp = Node->getOperand(2);
731
732 if (useDEBUG_LOC) {
733 Ops.push_back(LineOp); // line #
734 Ops.push_back(ColOp); // col #
735 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
736 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
737 } else {
738 unsigned Line = dyn_cast<ConstantSDNode>(LineOp)->getValue();
739 unsigned Col = dyn_cast<ConstantSDNode>(ColOp)->getValue();
740 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
741 Ops.push_back(DAG.getConstant(ID, MVT::i32));
742 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
743 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000744 } else {
745 Result = Tmp1; // chain
746 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000747 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000748 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000749 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000750 if (Tmp1 != Node->getOperand(0) ||
751 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000752 std::vector<SDOperand> Ops;
753 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000754 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
755 Ops.push_back(Node->getOperand(1)); // line # must be legal.
756 Ops.push_back(Node->getOperand(2)); // col # must be legal.
757 } else {
758 // Otherwise promote them.
759 Ops.push_back(PromoteOp(Node->getOperand(1)));
760 Ops.push_back(PromoteOp(Node->getOperand(2)));
761 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000762 Ops.push_back(Node->getOperand(3)); // filename must be legal.
763 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
764 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
765 }
766 break;
767 }
768 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000769
770 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000771 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000772 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000773 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000774 case TargetLowering::Legal:
775 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
776 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
777 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
778 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
779
780 if (Tmp1 != Node->getOperand(0) ||
781 Tmp2 != Node->getOperand(1) ||
782 Tmp3 != Node->getOperand(2) ||
783 Tmp4 != Node->getOperand(3)) {
784 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
785 }
786 break;
787 }
788 break;
789
790 case ISD::DEBUG_LABEL:
791 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
792 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000793 default: assert(0 && "This action is not supported yet!");
794 case TargetLowering::Legal:
795 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
796 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
797
798 if (Tmp1 != Node->getOperand(0) ||
799 Tmp2 != Node->getOperand(1)) {
800 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000801 }
802 break;
803 }
804 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000805
Chris Lattner3e928bb2005-01-07 07:47:09 +0000806 case ISD::Constant:
807 // We know we don't need to expand constants here, constants only have one
808 // value and we check that it is fine above.
809
810 // FIXME: Maybe we should handle things like targets that don't support full
811 // 32-bit immediates?
812 break;
813 case ISD::ConstantFP: {
814 // Spill FP immediates to the constant pool if the target cannot directly
815 // codegen them. Targets often have some immediate values that can be
816 // efficiently generated into an FP register without a load. We explicitly
817 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000818 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
819
820 // Check to see if this FP immediate is already legal.
821 bool isLegal = false;
822 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
823 E = TLI.legal_fpimm_end(); I != E; ++I)
824 if (CFP->isExactlyValue(*I)) {
825 isLegal = true;
826 break;
827 }
828
829 if (!isLegal) {
830 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000831 bool Extend = false;
832
Chris Lattner456a93a2006-01-28 07:39:30 +0000833 // If a FP immediate is precise when represented as a float and if the
834 // target can do an extending load from float to double, we put it into
835 // the constant pool as a float, even if it's is statically typed as a
836 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000837 MVT::ValueType VT = CFP->getValueType(0);
838 bool isDouble = VT == MVT::f64;
839 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
840 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000841 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
842 // Only do this if the target has a native EXTLOAD instruction from
843 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000844 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000845 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
846 VT = MVT::f32;
847 Extend = true;
848 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000849
Chris Lattner456a93a2006-01-28 07:39:30 +0000850 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000851 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000852 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
853 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000854 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000855 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
856 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000857 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000858 }
859 break;
860 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000861 case ISD::ConstantVec: {
862 // We assume that vector constants are not legal, and will be immediately
863 // spilled to the constant pool.
864 //
Chris Lattner456a93a2006-01-28 07:39:30 +0000865 // FIXME: Allow custom lowering to TargetConstantVec's.
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000866 //
867 // Create a ConstantPacked, and put it in the constant pool.
868 std::vector<Constant*> CV;
869 MVT::ValueType VT = Node->getValueType(0);
870 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
871 SDOperand OpN = Node->getOperand(I);
872 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
873 if (MVT::isFloatingPoint(VT))
874 CV.push_back(ConstantFP::get(OpNTy,
875 cast<ConstantFPSDNode>(OpN)->getValue()));
876 else
877 CV.push_back(ConstantUInt::get(OpNTy,
878 cast<ConstantSDNode>(OpN)->getValue()));
879 }
880 Constant *CP = ConstantPacked::get(CV);
Chris Lattner456a93a2006-01-28 07:39:30 +0000881 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000882 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
883 break;
884 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000885 case ISD::TokenFactor:
886 if (Node->getNumOperands() == 2) {
887 bool Changed = false;
888 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
889 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
890 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
891 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
892 } else {
893 std::vector<SDOperand> Ops;
894 bool Changed = false;
895 // Legalize the operands.
896 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
897 SDOperand Op = Node->getOperand(i);
898 Ops.push_back(LegalizeOp(Op));
899 Changed |= Ops[i] != Op;
900 }
901 if (Changed)
902 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000903 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000904 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000905
Chris Lattner16cd04d2005-05-12 23:24:06 +0000906 case ISD::CALLSEQ_START:
907 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000908 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000909 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000910 Tmp2 = Node->getOperand(0);
Nate Begeman1aa19722005-10-04 02:10:55 +0000911 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000912 Node->setAdjCallChain(Tmp1);
Chris Lattner6a542892006-01-24 05:48:21 +0000913
914 // If this has a flag input, do legalize it.
915 if (Node->getOperand(Node->getNumOperands()-1).getValueType() == MVT::Flag){
916 Tmp1 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
917 if (Tmp1 != Node->getOperand(Node->getNumOperands()-1))
918 Node->setAdjCallFlag(Tmp1);
919 }
Nate Begeman27d404c2005-10-04 00:37:37 +0000920
Chris Lattner16cd04d2005-05-12 23:24:06 +0000921 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000922 // nodes are treated specially and are mutated in place. This makes the dag
923 // legalization process more efficient and also makes libcall insertion
924 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000925 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000926 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000927 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
928 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
929 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
930 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000931 Tmp3 != Node->getOperand(2)) {
932 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
933 std::vector<SDOperand> Ops;
934 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
935 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
936 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000937 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000938
Chris Lattner5f652292006-01-15 08:43:08 +0000939 Tmp1 = Result;
940 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000941 switch (TLI.getOperationAction(Node->getOpcode(),
942 Node->getValueType(0))) {
943 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +0000944 case TargetLowering::Expand: {
945 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
946 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
947 " not tell us which reg is the stack pointer!");
948 SDOperand Chain = Tmp1.getOperand(0);
949 SDOperand Size = Tmp2.getOperand(1);
950 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
951 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
952 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
953 break;
954 }
955 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +0000956 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
957 if (Tmp3.Val) {
Chris Lattner456a93a2006-01-28 07:39:30 +0000958 Tmp1 = Tmp3;
959 Tmp2 = Tmp3.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000960 }
Chris Lattner903d2782006-01-15 08:54:32 +0000961 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000962 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +0000963 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000964 }
Chris Lattner903d2782006-01-15 08:54:32 +0000965 // Since this op produce two values, make sure to remember that we
966 // legalized both of them.
Chris Lattner456a93a2006-01-28 07:39:30 +0000967 AddLegalizedOperand(SDOperand(Node, 0), LegalizeOp(Tmp1));
968 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Tmp2));
Chris Lattner903d2782006-01-15 08:54:32 +0000969 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000970 }
Chris Lattnerce7518c2006-01-26 22:24:51 +0000971 case ISD::INLINEASM:
972 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
973 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
974 if (Tmp2.getValueType() != MVT::Flag) // Legalize Flag if it exists.
975 Tmp2 = Tmp3 = SDOperand(0, 0);
976 else
977 Tmp3 = LegalizeOp(Tmp2);
978
979 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
980 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
981 Ops[0] = Tmp1;
982 Ops.back() = Tmp3;
983 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
984 Result = DAG.getNode(ISD::INLINEASM, VTs, Ops);
985 } else {
986 Result = SDOperand(Node, 0);
987 }
988
989 // INLINE asm returns a chain and flag, make sure to add both to the map.
990 AddLegalizedOperand(SDOperand(Node, 0), Result);
991 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
992 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +0000993 case ISD::BR:
994 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
995 if (Tmp1 != Node->getOperand(0))
996 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
997 break;
998
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000999 case ISD::BRCOND:
1000 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +00001001
Chris Lattner47e92232005-01-18 19:27:06 +00001002 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 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001011
1012 // Basic block destination (Op#2) is always legal.
1013 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1014 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1015 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001016
1017 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1018 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001019 case TargetLowering::Legal: break;
1020 case TargetLowering::Custom:
1021 Tmp1 = TLI.LowerOperation(Result, DAG);
1022 if (Tmp1.Val) Result = Tmp1;
1023 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001024 case TargetLowering::Expand:
1025 // Expand brcond's setcc into its constituent parts and create a BR_CC
1026 // Node.
1027 if (Tmp2.getOpcode() == ISD::SETCC) {
1028 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1029 Tmp2.getOperand(0), Tmp2.getOperand(1),
1030 Node->getOperand(2));
1031 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001032 // Make sure the condition is either zero or one. It may have been
1033 // promoted from something else.
1034 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1035
Nate Begeman7cbd5252005-08-16 19:49:35 +00001036 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1037 DAG.getCondCode(ISD::SETNE), Tmp2,
1038 DAG.getConstant(0, Tmp2.getValueType()),
1039 Node->getOperand(2));
1040 }
1041 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001042 }
1043 break;
1044 case ISD::BR_CC:
1045 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner181b7a32005-12-17 23:46:46 +00001046 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001047 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1048 Node->getOperand(2), // LHS
1049 Node->getOperand(3), // RHS
1050 Node->getOperand(1)));
1051 // If we get a SETCC back from legalizing the SETCC node we just
1052 // created, then use its LHS, RHS, and CC directly in creating a new
1053 // node. Otherwise, select between the true and false value based on
1054 // comparing the result of the legalized with zero.
1055 if (Tmp2.getOpcode() == ISD::SETCC) {
1056 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1057 Tmp2.getOperand(0), Tmp2.getOperand(1),
1058 Node->getOperand(4));
1059 } else {
1060 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1061 DAG.getCondCode(ISD::SETNE),
1062 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
1063 Node->getOperand(4));
1064 }
Chris Lattner181b7a32005-12-17 23:46:46 +00001065 break;
1066 }
1067
1068 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1069 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1070
Chris Lattner456a93a2006-01-28 07:39:30 +00001071 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1072 Tmp3 != Node->getOperand(3)) {
1073 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
1074 Tmp2, Tmp3, Node->getOperand(4));
1075 }
1076
Chris Lattner181b7a32005-12-17 23:46:46 +00001077 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1078 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001079 case TargetLowering::Legal: break;
1080 case TargetLowering::Custom:
1081 Tmp4 = TLI.LowerOperation(Result, DAG);
1082 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001083 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001084 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001085 break;
Chris Lattner411e8882005-04-09 03:30:19 +00001086 case ISD::BRCONDTWOWAY:
1087 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1088 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1089 case Expand: assert(0 && "It's impossible to expand bools");
1090 case Legal:
1091 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1092 break;
1093 case Promote:
1094 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1095 break;
1096 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001097
1098
Chris Lattner411e8882005-04-09 03:30:19 +00001099 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
1100 // pair.
1101 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
1102 case TargetLowering::Promote:
1103 default: assert(0 && "This action is not supported yet!");
1104 case TargetLowering::Legal:
1105 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1106 std::vector<SDOperand> Ops;
1107 Ops.push_back(Tmp1);
1108 Ops.push_back(Tmp2);
1109 Ops.push_back(Node->getOperand(2));
1110 Ops.push_back(Node->getOperand(3));
1111 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
1112 }
1113 break;
1114 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +00001115 // If BRTWOWAY_CC is legal for this target, then simply expand this node
1116 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
1117 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001118 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001119 if (Tmp2.getOpcode() == ISD::SETCC) {
1120 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1121 Tmp2.getOperand(0), Tmp2.getOperand(1),
1122 Node->getOperand(2), Node->getOperand(3));
1123 } else {
1124 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1125 DAG.getConstant(0, Tmp2.getValueType()),
1126 Node->getOperand(2), Node->getOperand(3));
1127 }
1128 } else {
1129 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00001130 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001131 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
1132 }
Chris Lattner411e8882005-04-09 03:30:19 +00001133 break;
1134 }
1135 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001136 case ISD::BRTWOWAY_CC:
1137 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001138 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001139 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1140 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1141 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1142 Tmp3 != Node->getOperand(3)) {
1143 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1144 Node->getOperand(4), Node->getOperand(5));
1145 }
1146 break;
1147 } else {
1148 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1149 Node->getOperand(2), // LHS
1150 Node->getOperand(3), // RHS
1151 Node->getOperand(1)));
1152 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1153 // pair.
1154 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1155 default: assert(0 && "This action is not supported yet!");
1156 case TargetLowering::Legal:
1157 // If we get a SETCC back from legalizing the SETCC node we just
1158 // created, then use its LHS, RHS, and CC directly in creating a new
1159 // node. Otherwise, select between the true and false value based on
1160 // comparing the result of the legalized with zero.
1161 if (Tmp2.getOpcode() == ISD::SETCC) {
1162 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1163 Tmp2.getOperand(0), Tmp2.getOperand(1),
1164 Node->getOperand(4), Node->getOperand(5));
1165 } else {
1166 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1167 DAG.getConstant(0, Tmp2.getValueType()),
1168 Node->getOperand(4), Node->getOperand(5));
1169 }
1170 break;
1171 case TargetLowering::Expand:
1172 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1173 Node->getOperand(4));
1174 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1175 break;
1176 }
1177 }
1178 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001179 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001180 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1181 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001182
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001183 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001184 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1185 Result = DAG.getLoad(VT, Tmp1, Tmp2, Node->getOperand(2));
1186 else
1187 Result = SDOperand(Node, 0);
1188
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001189 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1190 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001191 case TargetLowering::Custom:
1192 Tmp1 = TLI.LowerOperation(Result, DAG);
1193 if (Tmp1.Val) {
Nate Begemanacc398c2006-01-25 18:21:52 +00001194 // Since loads produce two values, make sure to remember that we
1195 // legalized both of them.
Chris Lattner456a93a2006-01-28 07:39:30 +00001196 AddLegalizedOperand(SDOperand(Node, 0), LegalizeOp(Tmp1));
1197 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Tmp1.getValue(1)));
1198 return Tmp1.getValue(Op.ResNo);
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001199 }
1200 // FALLTHROUGH if the target thinks it is legal.
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001201 case TargetLowering::Legal:
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001202 // Since loads produce two values, make sure to remember that we legalized
1203 // both of them.
1204 AddLegalizedOperand(SDOperand(Node, 0), Result);
1205 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1206 return Result.getValue(Op.ResNo);
1207 }
1208 assert(0 && "Unreachable");
1209 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001210 case ISD::EXTLOAD:
1211 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001212 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001213 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1214 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001215
Chris Lattner5f056bf2005-07-10 01:55:33 +00001216 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001217 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001218 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001219 case TargetLowering::Promote:
1220 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001221 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1222 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001223 // Since loads produce two values, make sure to remember that we legalized
1224 // both of them.
1225 AddLegalizedOperand(SDOperand(Node, 0), Result);
1226 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1227 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001228
Chris Lattner456a93a2006-01-28 07:39:30 +00001229 case TargetLowering::Custom:
1230 isCustom = true;
1231 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001232 case TargetLowering::Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00001233 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +00001234 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1235 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +00001236 else
1237 Result = SDOperand(Node, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001238 Tmp1 = Result.getValue(1);
1239
1240 if (isCustom) {
1241 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1242 if (Tmp3.Val) {
1243 Result = LegalizeOp(Tmp3);
1244 Tmp1 = LegalizeOp(Tmp3.getValue(1));
1245 }
1246 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001247
1248 // Since loads produce two values, make sure to remember that we legalized
1249 // both of them.
1250 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00001251 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1252 return Op.ResNo ? Tmp1 : Result;
Chris Lattner01ff7212005-04-10 22:54:25 +00001253 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001254 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001255 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1256 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001257 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnere7736732005-12-18 23:54:29 +00001258 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001259 Load = LegalizeOp(Load);
1260 AddLegalizedOperand(SDOperand(Node, 0), Result);
1261 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001262 return Op.ResNo ? Load.getValue(1) : Result;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001263 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001264 assert(Node->getOpcode() != ISD::EXTLOAD &&
1265 "EXTLOAD should always be supported!");
1266 // Turn the unsupported load into an EXTLOAD followed by an explicit
1267 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001268 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1269 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001270 SDOperand ValRes;
1271 if (Node->getOpcode() == ISD::SEXTLOAD)
1272 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001273 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001274 else
1275 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnere7736732005-12-18 23:54:29 +00001276 Result = LegalizeOp(Result); // Relegalize new nodes.
1277 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001278 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1279 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001280 return Op.ResNo ? Result.getValue(1) : ValRes;
Chris Lattner01ff7212005-04-10 22:54:25 +00001281 }
1282 assert(0 && "Unreachable");
1283 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001284 case ISD::EXTRACT_ELEMENT: {
1285 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1286 switch (getTypeAction(OpTy)) {
1287 default:
1288 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1289 break;
1290 case Legal:
1291 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1292 // 1 -> Hi
1293 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1294 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1295 TLI.getShiftAmountTy()));
1296 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1297 } else {
1298 // 0 -> Lo
1299 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1300 Node->getOperand(0));
1301 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001302 break;
1303 case Expand:
1304 // Get both the low and high parts.
1305 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1306 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1307 Result = Tmp2; // 1 -> Hi
1308 else
1309 Result = Tmp1; // 0 -> Lo
1310 break;
1311 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001312 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001313 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001314
1315 case ISD::CopyToReg:
1316 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001317
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001318 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001319 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001320 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001321 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001322 if (Node->getNumValues() == 1) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001323 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1324 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1325 Node->getOperand(1), Tmp2);
1326 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001327 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1328 if (Node->getNumOperands() == 4)
1329 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattner7310fb12005-12-18 15:27:43 +00001330 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001331 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001332 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1333 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1334 }
1335
1336 // Since this produces two values, make sure to remember that we legalized
1337 // both of them.
1338 AddLegalizedOperand(SDOperand(Node, 0), Result);
1339 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1340 return Result.getValue(Op.ResNo);
1341 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001342 break;
1343
1344 case ISD::RET:
1345 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1346 switch (Node->getNumOperands()) {
1347 case 2: // ret val
1348 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1349 case Legal:
1350 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001351 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +00001352 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1353 break;
1354 case Expand: {
1355 SDOperand Lo, Hi;
1356 ExpandOp(Node->getOperand(1), Lo, Hi);
1357 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001358 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001359 }
1360 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001361 Tmp2 = PromoteOp(Node->getOperand(1));
1362 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1363 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001364 }
1365 break;
1366 case 1: // ret void
1367 if (Tmp1 != Node->getOperand(0))
1368 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1369 break;
1370 default: { // ret <values>
1371 std::vector<SDOperand> NewValues;
1372 NewValues.push_back(Tmp1);
1373 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1374 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1375 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001376 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001377 break;
1378 case Expand: {
1379 SDOperand Lo, Hi;
1380 ExpandOp(Node->getOperand(i), Lo, Hi);
1381 NewValues.push_back(Lo);
1382 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001383 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001384 }
1385 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001386 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001387 }
1388 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1389 break;
1390 }
1391 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001392
Chris Lattner47f5bea2006-01-06 05:47:48 +00001393 switch (TLI.getOperationAction(Node->getOpcode(),
1394 Node->getValueType(0))) {
Evan Cheng17c428e2006-01-06 00:41:43 +00001395 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001396 case TargetLowering::Legal: break;
1397 case TargetLowering::Custom:
1398 Tmp1 = TLI.LowerOperation(Result, DAG);
1399 if (Tmp1.Val) Result = Tmp1;
Evan Cheng17c428e2006-01-06 00:41:43 +00001400 break;
1401 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001402 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001403 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001404 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1405 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1406
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001407 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001408 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner03c85462005-01-15 05:21:40 +00001409 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001410 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001411 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001412 DAG.getConstant(FloatToBits(CFP->getValue()),
1413 MVT::i32),
Chris Lattner456a93a2006-01-28 07:39:30 +00001414 Tmp2, Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001415 } else {
1416 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001417 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001418 DAG.getConstant(DoubleToBits(CFP->getValue()),
1419 MVT::i64),
Chris Lattner456a93a2006-01-28 07:39:30 +00001420 Tmp2, Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001421 }
Chris Lattner6a542892006-01-24 05:48:21 +00001422 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001423 }
1424
Chris Lattner3e928bb2005-01-07 07:47:09 +00001425 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1426 case Legal: {
1427 SDOperand Val = LegalizeOp(Node->getOperand(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001428 if (Tmp1 != Node->getOperand(0) || Val != Node->getOperand(1) ||
Chris Lattner3e928bb2005-01-07 07:47:09 +00001429 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001430 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1431 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001432
1433 MVT::ValueType VT = Result.Val->getOperand(1).getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001434 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1435 default: assert(0 && "This action is not supported yet!");
1436 case TargetLowering::Legal: break;
1437 case TargetLowering::Custom:
1438 Tmp1 = TLI.LowerOperation(Result, DAG);
1439 if (Tmp1.Val) Result = Tmp1;
1440 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001441 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001442 break;
1443 }
1444 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001445 // Truncate the value and store the result.
1446 Tmp3 = PromoteOp(Node->getOperand(1));
1447 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001448 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001449 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001450 break;
1451
Chris Lattner3e928bb2005-01-07 07:47:09 +00001452 case Expand:
1453 SDOperand Lo, Hi;
1454 ExpandOp(Node->getOperand(1), Lo, Hi);
1455
1456 if (!TLI.isLittleEndian())
1457 std::swap(Lo, Hi);
1458
Chris Lattneredb1add2005-05-11 04:51:16 +00001459 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1460 Node->getOperand(3));
Nate Begemanab48be32005-11-22 18:16:00 +00001461 // If this is a vector type, then we have to calculate the increment as
1462 // the product of the element size in bytes, and the number of elements
1463 // in the high half of the vector.
Chris Lattner456a93a2006-01-28 07:39:30 +00001464 unsigned IncrementSize;
Nate Begemanab48be32005-11-22 18:16:00 +00001465 if (MVT::Vector == Hi.getValueType()) {
1466 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1467 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1468 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1469 } else {
1470 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1471 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001472 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1473 getIntPtrConstant(IncrementSize));
1474 assert(isTypeLegal(Tmp2.getValueType()) &&
1475 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001476 // FIXME: This sets the srcvalue of both halves to be the same, which is
1477 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001478 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1479 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001480 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1481 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001482 }
1483 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001484 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001485 case ISD::PCMARKER:
1486 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001487 if (Tmp1 != Node->getOperand(0))
1488 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001489 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001490 case ISD::STACKSAVE:
1491 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1492 if (Tmp1 != Node->getOperand(0)) {
1493 std::vector<MVT::ValueType> VTs;
1494 VTs.push_back(Node->getValueType(0));
1495 VTs.push_back(MVT::Other);
1496 std::vector<SDOperand> Ops;
1497 Ops.push_back(Tmp1);
1498 Result = DAG.getNode(ISD::STACKSAVE, VTs, Ops);
1499 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001500
1501 Tmp1 = Result.getValue(1);
1502
Chris Lattner140d53c2006-01-13 02:50:02 +00001503 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1504 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001505 case TargetLowering::Legal: break;
1506 case TargetLowering::Custom:
1507 Tmp2 = TLI.LowerOperation(Result, DAG);
1508 if (Tmp2.Val) {
1509 Result = LegalizeOp(Tmp2);
1510 Tmp1 = LegalizeOp(Tmp2.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001511 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001512 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001513 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001514 // Expand to CopyFromReg if the target set
1515 // StackPointerRegisterToSaveRestore.
1516 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001517 Tmp2 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001518 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00001519 Result = Tmp2;
1520 Tmp1 = Tmp2.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001521 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001522 Result = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1523 Tmp1 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001524 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001525 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001526 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001527
1528 // Since stacksave produce two values, make sure to remember that we
1529 // legalized both of them.
1530 AddLegalizedOperand(SDOperand(Node, 0), Result);
1531 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1532 return Op.ResNo ? Tmp1 : Result;
1533
Chris Lattner140d53c2006-01-13 02:50:02 +00001534 case ISD::STACKRESTORE:
1535 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1536 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1537 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1538 Result = DAG.getNode(ISD::STACKRESTORE, MVT::Other, Tmp1, Tmp2);
1539
1540 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1541 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001542 case TargetLowering::Legal: break;
1543 case TargetLowering::Custom:
1544 Tmp1 = TLI.LowerOperation(Result, DAG);
1545 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001546 break;
1547 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001548 // Expand to CopyToReg if the target set
1549 // StackPointerRegisterToSaveRestore.
1550 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1551 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1552 } else {
1553 Result = Tmp1;
1554 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001555 break;
1556 }
1557 break;
1558
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001559 case ISD::READCYCLECOUNTER:
1560 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthcde0f5c2005-12-02 06:08:08 +00001561 if (Tmp1 != Node->getOperand(0)) {
1562 std::vector<MVT::ValueType> rtypes;
1563 std::vector<SDOperand> rvals;
1564 rtypes.push_back(MVT::i64);
1565 rtypes.push_back(MVT::Other);
1566 rvals.push_back(Tmp1);
1567 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1568 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001569
1570 // Since rdcc produce two values, make sure to remember that we legalized
1571 // both of them.
1572 AddLegalizedOperand(SDOperand(Node, 0), Result);
1573 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1574 return Result.getValue(Op.ResNo);
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001575
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001576 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001577 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1578 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1579
Chris Lattner456a93a2006-01-28 07:39:30 +00001580 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1581 "Cannot handle illegal TRUNCSTORE yet!");
1582 Tmp2 = LegalizeOp(Node->getOperand(1));
1583
1584 // The only promote case we handle is TRUNCSTORE:i1 X into
1585 // -> TRUNCSTORE:i8 (and X, 1)
1586 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1587 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1588 TargetLowering::Promote) {
1589 // Promote the bool to a mask then store.
1590 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1591 DAG.getConstant(1, Tmp2.getValueType()));
1592 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1593 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001594
Chris Lattner456a93a2006-01-28 07:39:30 +00001595 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1596 Tmp3 != Node->getOperand(2)) {
1597 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1598 Node->getOperand(3), Node->getOperand(4));
1599 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001600
Chris Lattner456a93a2006-01-28 07:39:30 +00001601 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1602 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1603 default: assert(0 && "This action is not supported yet!");
1604 case TargetLowering::Legal: break;
1605 case TargetLowering::Custom:
1606 Tmp1 = TLI.LowerOperation(Result, DAG);
1607 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001608 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001609 }
1610 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001611 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001612 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001613 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1614 case Expand: assert(0 && "It's impossible to expand bools");
1615 case Legal:
1616 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1617 break;
1618 case Promote:
1619 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1620 break;
1621 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001622 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001623 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001624
Chris Lattner456a93a2006-01-28 07:39:30 +00001625 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1626 Tmp3 != Node->getOperand(2))
1627 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1628 Tmp1, Tmp2, Tmp3);
1629
Nate Begemanb942a3d2005-08-23 04:29:48 +00001630 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001631 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001632 case TargetLowering::Legal: break;
1633 case TargetLowering::Custom: {
1634 Tmp1 = TLI.LowerOperation(Result, DAG);
1635 if (Tmp1.Val) Result = Tmp1;
1636 break;
1637 }
Nate Begeman9373a812005-08-10 20:51:12 +00001638 case TargetLowering::Expand:
1639 if (Tmp1.getOpcode() == ISD::SETCC) {
1640 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1641 Tmp2, Tmp3,
1642 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1643 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001644 // Make sure the condition is either zero or one. It may have been
1645 // promoted from something else.
1646 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001647 Result = DAG.getSelectCC(Tmp1,
1648 DAG.getConstant(0, Tmp1.getValueType()),
1649 Tmp2, Tmp3, ISD::SETNE);
1650 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001651 break;
1652 case TargetLowering::Promote: {
1653 MVT::ValueType NVT =
1654 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1655 unsigned ExtOp, TruncOp;
1656 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001657 ExtOp = ISD::ANY_EXTEND;
1658 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001659 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001660 ExtOp = ISD::FP_EXTEND;
1661 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001662 }
1663 // Promote each of the values to the new type.
1664 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1665 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1666 // Perform the larger operation, then round down.
1667 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1668 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1669 break;
1670 }
1671 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001672 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001673 case ISD::SELECT_CC:
1674 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1675 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1676
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001677 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001678 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1679 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1680 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1681 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1682 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1,Tmp2,
1683 Tmp3, Tmp4, Node->getOperand(4));
1684 }
1685
Chris Lattner23004e52005-08-26 00:23:59 +00001686 // Everything is legal, see if we should expand this op or something.
1687 switch (TLI.getOperationAction(ISD::SELECT_CC,
1688 Node->getOperand(0).getValueType())) {
1689 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001690 case TargetLowering::Legal: break;
1691 case TargetLowering::Custom:
1692 Tmp1 = TLI.LowerOperation(Result, DAG);
1693 if (Tmp1.Val) Result = Tmp1;
Chris Lattner23004e52005-08-26 00:23:59 +00001694 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001695 }
1696 break;
1697 } else {
1698 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1699 Node->getOperand(0), // LHS
1700 Node->getOperand(1), // RHS
1701 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001702 // If we get a SETCC back from legalizing the SETCC node we just
1703 // created, then use its LHS, RHS, and CC directly in creating a new
1704 // node. Otherwise, select between the true and false value based on
1705 // comparing the result of the legalized with zero.
1706 if (Tmp1.getOpcode() == ISD::SETCC) {
1707 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1708 Tmp1.getOperand(0), Tmp1.getOperand(1),
1709 Tmp3, Tmp4, Tmp1.getOperand(2));
1710 } else {
1711 Result = DAG.getSelectCC(Tmp1,
1712 DAG.getConstant(0, Tmp1.getValueType()),
1713 Tmp3, Tmp4, ISD::SETNE);
1714 }
Nate Begeman9373a812005-08-10 20:51:12 +00001715 }
1716 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001717 case ISD::SETCC:
1718 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1719 case Legal:
1720 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1721 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001722 break;
1723 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001724 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1725 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1726
1727 // If this is an FP compare, the operands have already been extended.
1728 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1729 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001730 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001731
1732 // Otherwise, we have to insert explicit sign or zero extends. Note
1733 // that we could insert sign extends for ALL conditions, but zero extend
1734 // is cheaper on many machines (an AND instead of two shifts), so prefer
1735 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001736 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001737 default: assert(0 && "Unknown integer comparison!");
1738 case ISD::SETEQ:
1739 case ISD::SETNE:
1740 case ISD::SETUGE:
1741 case ISD::SETUGT:
1742 case ISD::SETULE:
1743 case ISD::SETULT:
1744 // ALL of these operations will work if we either sign or zero extend
1745 // the operands (including the unsigned comparisons!). Zero extend is
1746 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001747 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1748 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001749 break;
1750 case ISD::SETGE:
1751 case ISD::SETGT:
1752 case ISD::SETLT:
1753 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001754 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1755 DAG.getValueType(VT));
1756 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1757 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001758 break;
1759 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001760 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001761 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001762 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001763 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1764 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1765 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001766 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001767 case ISD::SETEQ:
1768 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001769 if (RHSLo == RHSHi)
1770 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1771 if (RHSCST->isAllOnesValue()) {
1772 // Comparison to -1.
1773 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001774 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001775 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001776 }
1777
Chris Lattner3e928bb2005-01-07 07:47:09 +00001778 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1779 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1780 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001781 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001782 break;
1783 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001784 // If this is a comparison of the sign bit, just look at the top part.
1785 // X > -1, x < 0
1786 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001787 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001788 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001789 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001790 (CST->isAllOnesValue()))) { // X > -1
1791 Tmp1 = LHSHi;
1792 Tmp2 = RHSHi;
1793 break;
1794 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001795
Chris Lattner3e928bb2005-01-07 07:47:09 +00001796 // FIXME: This generated code sucks.
1797 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001798 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001799 default: assert(0 && "Unknown integer setcc!");
1800 case ISD::SETLT:
1801 case ISD::SETULT: LowCC = ISD::SETULT; break;
1802 case ISD::SETGT:
1803 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1804 case ISD::SETLE:
1805 case ISD::SETULE: LowCC = ISD::SETULE; break;
1806 case ISD::SETGE:
1807 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1808 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001809
Chris Lattner3e928bb2005-01-07 07:47:09 +00001810 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1811 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1812 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1813
1814 // NOTE: on targets without efficient SELECT of bools, we can always use
1815 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001816 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1817 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1818 Node->getOperand(2));
1819 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001820 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1821 Result, Tmp1, Tmp2));
Chris Lattner69a889e2005-12-20 00:53:54 +00001822 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001823 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001824 }
1825 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001826
Chris Lattner456a93a2006-01-28 07:39:30 +00001827 switch (TLI.getOperationAction(ISD::SETCC,
1828 Node->getOperand(0).getValueType())) {
1829 default: assert(0 && "Cannot handle this action for SETCC yet!");
1830 case TargetLowering::Custom:
1831 isCustom = true;
1832 // FALLTHROUGH.
1833 case TargetLowering::Legal:
1834 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1835 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1836 Node->getOperand(2));
1837 if (isCustom) {
1838 Tmp3 = TLI.LowerOperation(Result, DAG);
1839 if (Tmp3.Val) Result = Tmp3;
1840 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001841 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001842 case TargetLowering::Promote: {
1843 // First step, figure out the appropriate operation to use.
1844 // Allow SETCC to not be supported for all legal data types
1845 // Mostly this targets FP
1846 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1847 MVT::ValueType OldVT = NewInTy;
1848
1849 // Scan for the appropriate larger type to use.
1850 while (1) {
1851 NewInTy = (MVT::ValueType)(NewInTy+1);
1852
1853 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1854 "Fell off of the edge of the integer world");
1855 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1856 "Fell off of the edge of the floating point world");
1857
1858 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001859 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001860 break;
1861 }
1862 if (MVT::isInteger(NewInTy))
1863 assert(0 && "Cannot promote Legal Integer SETCC yet");
1864 else {
1865 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1866 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1867 }
1868
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001869 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1870 Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001871 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001872 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001873 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001874 case TargetLowering::Expand:
1875 // Expand a setcc node into a select_cc of the same condition, lhs, and
1876 // rhs that selects between const 1 (true) and const 0 (false).
1877 MVT::ValueType VT = Node->getValueType(0);
1878 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1879 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1880 Node->getOperand(2));
1881 Result = LegalizeOp(Result);
1882 break;
1883 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001884 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001885 case ISD::MEMSET:
1886 case ISD::MEMCPY:
1887 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001888 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001889 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1890
1891 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1892 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1893 case Expand: assert(0 && "Cannot expand a byte!");
1894 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001895 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001896 break;
1897 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001898 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001899 break;
1900 }
1901 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001902 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001903 }
Chris Lattner272455b2005-02-02 03:44:41 +00001904
1905 SDOperand Tmp4;
1906 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001907 case Expand: {
1908 // Length is too big, just take the lo-part of the length.
1909 SDOperand HiPart;
1910 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1911 break;
1912 }
Chris Lattnere5605212005-01-28 22:29:18 +00001913 case Legal:
1914 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001915 break;
1916 case Promote:
1917 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001918 break;
1919 }
1920
1921 SDOperand Tmp5;
1922 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1923 case Expand: assert(0 && "Cannot expand this yet!");
1924 case Legal:
1925 Tmp5 = LegalizeOp(Node->getOperand(4));
1926 break;
1927 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001928 Tmp5 = PromoteOp(Node->getOperand(4));
1929 break;
1930 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001931
1932 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1933 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001934 case TargetLowering::Custom:
1935 isCustom = true;
1936 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001937 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001938 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1939 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1940 Tmp5 != Node->getOperand(4)) {
1941 std::vector<SDOperand> Ops;
1942 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1943 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1944 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1945 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001946 if (isCustom) {
1947 Tmp1 = TLI.LowerOperation(Result, DAG);
1948 if (Tmp1.Val) Result = Tmp1;
1949 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001950 break;
1951 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001952 // Otherwise, the target does not support this operation. Lower the
1953 // operation to an explicit libcall as appropriate.
1954 MVT::ValueType IntPtr = TLI.getPointerTy();
1955 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1956 std::vector<std::pair<SDOperand, const Type*> > Args;
1957
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001958 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001959 if (Node->getOpcode() == ISD::MEMSET) {
1960 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1961 // Extend the ubyte argument to be an int value for the call.
1962 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1963 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1964 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1965
1966 FnName = "memset";
1967 } else if (Node->getOpcode() == ISD::MEMCPY ||
1968 Node->getOpcode() == ISD::MEMMOVE) {
1969 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1970 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1971 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1972 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1973 } else {
1974 assert(0 && "Unknown op!");
1975 }
Chris Lattner45982da2005-05-12 16:53:42 +00001976
Chris Lattnere1bd8222005-01-11 05:57:22 +00001977 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001978 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001979 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001980 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001981 break;
1982 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001983 }
1984 break;
1985 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001986
1987 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001988 Tmp1 = LegalizeOp(Node->getOperand(0));
1989 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001990
Chris Lattner3e011362005-05-14 07:45:46 +00001991 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1992 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1993 std::vector<SDOperand> Ops;
1994 Ops.push_back(Tmp1);
1995 Ops.push_back(Tmp2);
1996 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1997 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001998 Result = SDOperand(Node, 0);
1999 // Since these produce two values, make sure to remember that we legalized
2000 // both of them.
2001 AddLegalizedOperand(SDOperand(Node, 0), Result);
2002 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2003 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00002004 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00002005 Tmp1 = LegalizeOp(Node->getOperand(0));
2006 Tmp2 = LegalizeOp(Node->getOperand(1));
2007 Tmp3 = LegalizeOp(Node->getOperand(2));
2008 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
2009 Tmp3 != Node->getOperand(2))
2010 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
2011 break;
2012
Chris Lattner6d5b8e12005-05-09 20:36:57 +00002013 case ISD::READIO:
2014 Tmp1 = LegalizeOp(Node->getOperand(0));
2015 Tmp2 = LegalizeOp(Node->getOperand(1));
2016
2017 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2018 case TargetLowering::Custom:
2019 default: assert(0 && "This action not implemented for this operation!");
2020 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00002021 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
2022 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
2023 std::vector<SDOperand> Ops;
2024 Ops.push_back(Tmp1);
2025 Ops.push_back(Tmp2);
2026 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
2027 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00002028 Result = SDOperand(Node, 0);
2029 break;
2030 case TargetLowering::Expand:
2031 // Replace this with a load from memory.
2032 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
2033 Node->getOperand(1), DAG.getSrcValue(NULL));
2034 Result = LegalizeOp(Result);
2035 break;
2036 }
2037
2038 // Since these produce two values, make sure to remember that we legalized
2039 // both of them.
2040 AddLegalizedOperand(SDOperand(Node, 0), Result);
2041 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2042 return Result.getValue(Op.ResNo);
2043
2044 case ISD::WRITEIO:
2045 Tmp1 = LegalizeOp(Node->getOperand(0));
2046 Tmp2 = LegalizeOp(Node->getOperand(1));
2047 Tmp3 = LegalizeOp(Node->getOperand(2));
2048
2049 switch (TLI.getOperationAction(Node->getOpcode(),
2050 Node->getOperand(1).getValueType())) {
2051 case TargetLowering::Custom:
2052 default: assert(0 && "This action not implemented for this operation!");
2053 case TargetLowering::Legal:
2054 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
2055 Tmp3 != Node->getOperand(2))
2056 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
2057 break;
2058 case TargetLowering::Expand:
2059 // Replace this with a store to memory.
2060 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
2061 Node->getOperand(1), Node->getOperand(2),
2062 DAG.getSrcValue(NULL));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00002063 break;
2064 }
2065 break;
2066
Chris Lattner84f67882005-01-20 18:52:28 +00002067 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00002068 case ISD::SUB_PARTS:
2069 case ISD::SHL_PARTS:
2070 case ISD::SRA_PARTS:
2071 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00002072 std::vector<SDOperand> Ops;
2073 bool Changed = false;
2074 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2075 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2076 Changed |= Ops.back() != Node->getOperand(i);
2077 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002078 if (Changed) {
2079 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
2080 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
2081 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002082
Evan Cheng05a2d562006-01-09 18:31:59 +00002083 switch (TLI.getOperationAction(Node->getOpcode(),
2084 Node->getValueType(0))) {
2085 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002086 case TargetLowering::Legal: break;
2087 case TargetLowering::Custom:
2088 Tmp1 = TLI.LowerOperation(Result, DAG);
2089 if (Tmp1.Val) {
2090 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002091 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002092 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002093 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2094 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002095 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002096 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002097 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002098 return RetVal;
2099 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002100 break;
2101 }
2102
Chris Lattner2c8086f2005-04-02 05:00:07 +00002103 // Since these produce multiple values, make sure to remember that we
2104 // legalized all of them.
2105 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2106 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2107 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002108 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002109
2110 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002111 case ISD::ADD:
2112 case ISD::SUB:
2113 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002114 case ISD::MULHS:
2115 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002116 case ISD::UDIV:
2117 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002118 case ISD::AND:
2119 case ISD::OR:
2120 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002121 case ISD::SHL:
2122 case ISD::SRL:
2123 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002124 case ISD::FADD:
2125 case ISD::FSUB:
2126 case ISD::FMUL:
2127 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002128 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002129 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2130 case Expand: assert(0 && "Not possible");
2131 case Legal:
2132 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2133 break;
2134 case Promote:
2135 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2136 break;
2137 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002138
2139 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2140 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
2141
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002142 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002143 default: assert(0 && "Operation not supported");
2144 case TargetLowering::Legal: break;
2145 case TargetLowering::Custom:
2146 Tmp1 = TLI.LowerOperation(Result, DAG);
2147 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002148 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002149 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002150 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002151
Nate Begeman419f8b62005-10-18 00:27:41 +00002152 case ISD::BUILD_PAIR: {
2153 MVT::ValueType PairTy = Node->getValueType(0);
2154 // TODO: handle the case where the Lo and Hi operands are not of legal type
2155 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2156 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2157 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002158 case TargetLowering::Promote:
2159 case TargetLowering::Custom:
2160 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002161 case TargetLowering::Legal:
2162 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2163 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2164 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002165 case TargetLowering::Expand:
2166 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2167 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2168 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2169 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2170 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002171 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002172 break;
2173 }
2174 break;
2175 }
2176
Nate Begemanc105e192005-04-06 00:23:54 +00002177 case ISD::UREM:
2178 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002179 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002180 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2181 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002182
Nate Begemanc105e192005-04-06 00:23:54 +00002183 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002184 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2185 case TargetLowering::Custom:
2186 isCustom = true;
2187 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002188 case TargetLowering::Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002189 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2190 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
2191 Tmp1, Tmp2);
2192 if (isCustom) {
2193 Tmp1 = TLI.LowerOperation(Result, DAG);
2194 if (Tmp1.Val) Result = Tmp1;
2195 }
Nate Begemanc105e192005-04-06 00:23:54 +00002196 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002197 case TargetLowering::Expand:
2198 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002199 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00002200 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002201 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002202 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2203 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2204 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2205 } else {
2206 // Floating point mod -> fmod libcall.
2207 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2208 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002209 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002210 }
2211 break;
2212 }
2213 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002214 case ISD::VAARG: {
2215 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2216 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2217
Chris Lattner456a93a2006-01-28 07:39:30 +00002218 MVT::ValueType VT;
Nate Begemanacc398c2006-01-25 18:21:52 +00002219 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2220 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002221 case TargetLowering::Custom:
2222 isCustom = true;
2223 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002224 case TargetLowering::Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002225 VT = Node->getValueType(0);
2226 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Nate Begemanacc398c2006-01-25 18:21:52 +00002227 Result = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2228 else
2229 Result = SDOperand(Node, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002230 Tmp1 = Result.getValue(1);
2231
2232 if (isCustom) {
2233 Tmp2 = TLI.LowerOperation(Result, DAG);
2234 if (Tmp2.Val) {
2235 Result = LegalizeOp(Tmp2);
2236 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2237 }
2238 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002239 break;
2240 case TargetLowering::Expand: {
2241 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2242 Node->getOperand(2));
2243 // Increment the pointer, VAList, to the next vaarg
2244 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2245 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2246 TLI.getPointerTy()));
2247 // Store the incremented VAList to the legalized pointer
2248 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2249 Node->getOperand(2));
2250 // Load the actual argument out of the pointer VAList
2251 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002252 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002253 Result = LegalizeOp(Result);
2254 break;
2255 }
2256 }
2257 // Since VAARG produces two values, make sure to remember that we
2258 // legalized both of them.
2259 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002260 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2261 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002262 }
2263
2264 case ISD::VACOPY:
2265 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2266 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2267 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2268
2269 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2270 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002271 case TargetLowering::Custom:
2272 isCustom = true;
2273 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002274 case TargetLowering::Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002275 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Nate Begemanacc398c2006-01-25 18:21:52 +00002276 Tmp3 != Node->getOperand(2))
2277 Result = DAG.getNode(ISD::VACOPY, MVT::Other, Tmp1, Tmp2, Tmp3,
2278 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002279 if (isCustom) {
2280 Tmp1 = TLI.LowerOperation(Result, DAG);
2281 if (Tmp1.Val) Result = Tmp1;
2282 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002283 break;
2284 case TargetLowering::Expand:
2285 // This defaults to loading a pointer from the input and storing it to the
2286 // output, returning the chain.
2287 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2288 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2289 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002290 break;
2291 }
2292 break;
2293
2294 case ISD::VAEND:
2295 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2296 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2297
2298 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2299 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002300 case TargetLowering::Custom:
2301 isCustom = true;
2302 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002303 case TargetLowering::Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002304 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Nate Begemanacc398c2006-01-25 18:21:52 +00002305 Result = DAG.getNode(ISD::VAEND, MVT::Other, Tmp1, Tmp2,
2306 Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002307 if (isCustom) {
2308 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2309 if (Tmp1.Val) Result = Tmp1;
2310 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002311 break;
2312 case TargetLowering::Expand:
2313 Result = Tmp1; // Default to a no-op, return the chain
2314 break;
2315 }
2316 break;
2317
2318 case ISD::VASTART:
2319 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2320 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2321
Chris Lattner456a93a2006-01-28 07:39:30 +00002322 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2323 Result = DAG.getNode(ISD::VASTART, MVT::Other, Tmp1, Tmp2,
2324 Node->getOperand(2));
2325
Nate Begemanacc398c2006-01-25 18:21:52 +00002326 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2327 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002328 case TargetLowering::Legal: break;
2329 case TargetLowering::Custom:
2330 Tmp1 = TLI.LowerOperation(Result, DAG);
2331 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002332 break;
2333 }
2334 break;
2335
Nate Begeman35ef9132006-01-11 21:21:00 +00002336 case ISD::ROTL:
2337 case ISD::ROTR:
2338 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2339 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002340
2341 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2342 "Cannot handle this yet!");
2343 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2344 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002345 break;
2346
Nate Begemand88fc032006-01-14 03:14:10 +00002347 case ISD::BSWAP:
2348 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2349 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002350 case TargetLowering::Custom:
2351 assert(0 && "Cannot custom legalize this yet!");
2352 case TargetLowering::Legal:
2353 if (Tmp1 != Node->getOperand(0))
2354 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2355 break;
2356 case TargetLowering::Promote: {
2357 MVT::ValueType OVT = Tmp1.getValueType();
2358 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2359 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002360
Chris Lattner456a93a2006-01-28 07:39:30 +00002361 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2362 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2363 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2364 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2365 break;
2366 }
2367 case TargetLowering::Expand:
2368 Result = ExpandBSWAP(Tmp1);
2369 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002370 }
2371 break;
2372
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002373 case ISD::CTPOP:
2374 case ISD::CTTZ:
2375 case ISD::CTLZ:
2376 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2377 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002378 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002379 case TargetLowering::Legal:
2380 if (Tmp1 != Node->getOperand(0))
2381 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2382 break;
2383 case TargetLowering::Promote: {
2384 MVT::ValueType OVT = Tmp1.getValueType();
2385 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002386
2387 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002388 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2389 // Perform the larger operation, then subtract if needed.
2390 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002391 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002392 case ISD::CTPOP:
2393 Result = Tmp1;
2394 break;
2395 case ISD::CTTZ:
2396 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002397 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2398 DAG.getConstant(getSizeInBits(NVT), NVT),
2399 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002400 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002401 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2402 break;
2403 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002404 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002405 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2406 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002407 getSizeInBits(OVT), NVT));
2408 break;
2409 }
2410 break;
2411 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002412 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002413 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002414 break;
2415 }
2416 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002417
Chris Lattner2c8086f2005-04-02 05:00:07 +00002418 // Unary operators
2419 case ISD::FABS:
2420 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002421 case ISD::FSQRT:
2422 case ISD::FSIN:
2423 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002424 Tmp1 = LegalizeOp(Node->getOperand(0));
2425 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002426 case TargetLowering::Promote:
2427 case TargetLowering::Custom:
2428 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00002429 case TargetLowering::Legal:
2430 if (Tmp1 != Node->getOperand(0))
2431 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2432 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002433 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002434 switch (Node->getOpcode()) {
2435 default: assert(0 && "Unreachable!");
2436 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002437 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2438 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002439 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002440 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002441 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002442 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2443 MVT::ValueType VT = Node->getValueType(0);
2444 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002445 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002446 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2447 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002448 break;
2449 }
2450 case ISD::FSQRT:
2451 case ISD::FSIN:
2452 case ISD::FCOS: {
2453 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002454 const char *FnName = 0;
2455 switch(Node->getOpcode()) {
2456 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2457 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2458 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2459 default: assert(0 && "Unreachable!");
2460 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002461 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002462 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002463 break;
2464 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002465 }
2466 break;
2467 }
2468 break;
Chris Lattner35481892005-12-23 00:16:34 +00002469
2470 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002471 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002472 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002473 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002474 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2475 Node->getOperand(0).getValueType())) {
2476 default: assert(0 && "Unknown operation action!");
2477 case TargetLowering::Expand:
2478 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2479 break;
2480 case TargetLowering::Legal:
2481 Tmp1 = LegalizeOp(Node->getOperand(0));
2482 if (Tmp1 != Node->getOperand(0))
2483 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Tmp1);
2484 break;
2485 }
2486 }
2487 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002488 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002489 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002490 case ISD::UINT_TO_FP: {
2491 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002492 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2493 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002494 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002495 Node->getOperand(0).getValueType())) {
2496 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002497 case TargetLowering::Custom:
2498 isCustom = true;
2499 // FALLTHROUGH
2500 case TargetLowering::Legal:
2501 Tmp1 = LegalizeOp(Node->getOperand(0));
2502 if (Tmp1 != Node->getOperand(0))
2503 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2504 if (isCustom) {
2505 Tmp1 = TLI.LowerOperation(Result, DAG);
2506 if (Tmp1.Val) Result = Tmp1;
2507 }
2508 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002509 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002510 Result = ExpandLegalINT_TO_FP(isSigned,
2511 LegalizeOp(Node->getOperand(0)),
2512 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002513 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002514 case TargetLowering::Promote:
2515 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2516 Node->getValueType(0),
2517 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002518 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002519 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002520 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002521 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002522 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2523 Node->getValueType(0), Node->getOperand(0));
2524 break;
2525 case Promote:
2526 if (isSigned) {
2527 Result = PromoteOp(Node->getOperand(0));
2528 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2529 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2530 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2531 } else {
2532 Result = PromoteOp(Node->getOperand(0));
2533 Result = DAG.getZeroExtendInReg(Result,
2534 Node->getOperand(0).getValueType());
2535 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00002536 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002537 break;
2538 }
2539 break;
2540 }
2541 case ISD::TRUNCATE:
2542 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2543 case Legal:
2544 Tmp1 = LegalizeOp(Node->getOperand(0));
2545 if (Tmp1 != Node->getOperand(0))
2546 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2547 break;
2548 case Expand:
2549 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2550
2551 // Since the result is legal, we should just be able to truncate the low
2552 // part of the source.
2553 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2554 break;
2555 case Promote:
2556 Result = PromoteOp(Node->getOperand(0));
2557 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2558 break;
2559 }
2560 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002561
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002562 case ISD::FP_TO_SINT:
2563 case ISD::FP_TO_UINT:
2564 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2565 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002566 Tmp1 = LegalizeOp(Node->getOperand(0));
2567
Chris Lattner1618beb2005-07-29 00:11:56 +00002568 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2569 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002570 case TargetLowering::Custom:
2571 isCustom = true;
2572 // FALLTHROUGH
2573 case TargetLowering::Legal:
2574 if (Tmp1 != Node->getOperand(0))
2575 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2576
2577 if (isCustom) {
2578 Tmp1 = TLI.LowerOperation(Result, DAG);
2579 if (Tmp1.Val) Result = Tmp1;
2580 }
2581 break;
2582 case TargetLowering::Promote:
2583 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2584 Node->getOpcode() == ISD::FP_TO_SINT);
2585 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002586 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002587 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2588 SDOperand True, False;
2589 MVT::ValueType VT = Node->getOperand(0).getValueType();
2590 MVT::ValueType NVT = Node->getValueType(0);
2591 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2592 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2593 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2594 Node->getOperand(0), Tmp2, ISD::SETLT);
2595 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2596 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002597 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002598 Tmp2));
2599 False = DAG.getNode(ISD::XOR, NVT, False,
2600 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002601 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2602 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002603 } else {
2604 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2605 }
2606 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002607 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002608 break;
2609 case Expand:
2610 assert(0 && "Shouldn't need to expand other operators here!");
2611 case Promote:
2612 Result = PromoteOp(Node->getOperand(0));
2613 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2614 break;
2615 }
2616 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002617
Chris Lattner13c78e22005-09-02 00:18:10 +00002618 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002619 case ISD::ZERO_EXTEND:
2620 case ISD::SIGN_EXTEND:
2621 case ISD::FP_EXTEND:
2622 case ISD::FP_ROUND:
2623 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002624 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002625 case Legal:
2626 Tmp1 = LegalizeOp(Node->getOperand(0));
2627 if (Tmp1 != Node->getOperand(0))
2628 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2629 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002630 case Promote:
2631 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002632 case ISD::ANY_EXTEND:
2633 Result = PromoteOp(Node->getOperand(0));
2634 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2635 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002636 case ISD::ZERO_EXTEND:
2637 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002638 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002639 Result = DAG.getZeroExtendInReg(Result,
2640 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002641 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002642 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002643 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002644 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002645 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002646 Result,
2647 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002648 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002649 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002650 Result = PromoteOp(Node->getOperand(0));
2651 if (Result.getValueType() != Op.getValueType())
2652 // Dynamically dead while we have only 2 FP types.
2653 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2654 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002655 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002656 Result = PromoteOp(Node->getOperand(0));
2657 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2658 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002659 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002660 }
2661 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002662 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002663 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002664 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002665 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002666
2667 // If this operation is not supported, convert it to a shl/shr or load/store
2668 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002669 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2670 default: assert(0 && "This action not supported for this op yet!");
2671 case TargetLowering::Legal:
2672 if (Tmp1 != Node->getOperand(0))
2673 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00002674 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002675 break;
2676 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002677 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002678 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002679 // NOTE: we could fall back on load/store here too for targets without
2680 // SAR. However, it is doubtful that any exist.
2681 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2682 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002683 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002684 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2685 Node->getOperand(0), ShiftCst);
2686 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2687 Result, ShiftCst);
2688 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2689 // The only way we can lower this is to turn it into a STORETRUNC,
2690 // EXTLOAD pair, targetting a temporary location (a stack slot).
2691
2692 // NOTE: there is a choice here between constantly creating new stack
2693 // slots and always reusing the same one. We currently always create
2694 // new ones, as reuse may inhibit scheduling.
2695 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2696 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2697 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2698 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002699 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002700 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2701 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2702 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002703 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002704 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002705 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2706 Result, StackSlot, DAG.getSrcValue(NULL),
2707 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002708 } else {
2709 assert(0 && "Unknown op");
2710 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002711 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002712 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002713 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002714 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002715 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002716
2717 // Make sure that the generated code is itself legal.
2718 if (Result != Op)
2719 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002720
Chris Lattner45982da2005-05-12 16:53:42 +00002721 // Note that LegalizeOp may be reentered even from single-use nodes, which
2722 // means that we always must cache transformed nodes.
2723 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002724 return Result;
2725}
2726
Chris Lattner8b6fa222005-01-15 22:16:26 +00002727/// PromoteOp - Given an operation that produces a value in an invalid type,
2728/// promote it to compute the value into a larger type. The produced value will
2729/// have the correct bits for the low portion of the register, but no guarantee
2730/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002731SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2732 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002733 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002734 assert(getTypeAction(VT) == Promote &&
2735 "Caller should expand or legalize operands that are not promotable!");
2736 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2737 "Cannot promote to smaller type!");
2738
Chris Lattner03c85462005-01-15 05:21:40 +00002739 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002740 SDOperand Result;
2741 SDNode *Node = Op.Val;
2742
Chris Lattner6fdcb252005-09-02 20:32:45 +00002743 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2744 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002745
Chris Lattner03c85462005-01-15 05:21:40 +00002746 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002747 case ISD::CopyFromReg:
2748 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002749 default:
2750 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2751 assert(0 && "Do not know how to promote this operator!");
2752 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002753 case ISD::UNDEF:
2754 Result = DAG.getNode(ISD::UNDEF, NVT);
2755 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002756 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002757 if (VT != MVT::i1)
2758 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2759 else
2760 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002761 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2762 break;
2763 case ISD::ConstantFP:
2764 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2765 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2766 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002767
Chris Lattner82fbfb62005-01-18 02:59:52 +00002768 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002769 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002770 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2771 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002772 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002773
2774 case ISD::TRUNCATE:
2775 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2776 case Legal:
2777 Result = LegalizeOp(Node->getOperand(0));
2778 assert(Result.getValueType() >= NVT &&
2779 "This truncation doesn't make sense!");
2780 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2781 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2782 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002783 case Promote:
2784 // The truncation is not required, because we don't guarantee anything
2785 // about high bits anyway.
2786 Result = PromoteOp(Node->getOperand(0));
2787 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002788 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002789 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2790 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002791 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002792 }
2793 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002794 case ISD::SIGN_EXTEND:
2795 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002796 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002797 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2798 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2799 case Legal:
2800 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002801 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002802 break;
2803 case Promote:
2804 // Promote the reg if it's smaller.
2805 Result = PromoteOp(Node->getOperand(0));
2806 // The high bits are not guaranteed to be anything. Insert an extend.
2807 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002808 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002809 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002810 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002811 Result = DAG.getZeroExtendInReg(Result,
2812 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002813 break;
2814 }
2815 break;
Chris Lattner35481892005-12-23 00:16:34 +00002816 case ISD::BIT_CONVERT:
2817 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2818 Result = PromoteOp(Result);
2819 break;
2820
Chris Lattner8b6fa222005-01-15 22:16:26 +00002821 case ISD::FP_EXTEND:
2822 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2823 case ISD::FP_ROUND:
2824 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2825 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2826 case Promote: assert(0 && "Unreachable with 2 FP types!");
2827 case Legal:
2828 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002829 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002830 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002831 break;
2832 }
2833 break;
2834
2835 case ISD::SINT_TO_FP:
2836 case ISD::UINT_TO_FP:
2837 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2838 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002839 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002840 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002841 break;
2842
2843 case Promote:
2844 Result = PromoteOp(Node->getOperand(0));
2845 if (Node->getOpcode() == ISD::SINT_TO_FP)
2846 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002847 Result,
2848 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002849 else
Chris Lattner23993562005-04-13 02:38:47 +00002850 Result = DAG.getZeroExtendInReg(Result,
2851 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002852 // No extra round required here.
2853 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002854 break;
2855 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002856 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2857 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002858 // Round if we cannot tolerate excess precision.
2859 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002860 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2861 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002862 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002863 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002864 break;
2865
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002866 case ISD::SIGN_EXTEND_INREG:
2867 Result = PromoteOp(Node->getOperand(0));
2868 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2869 Node->getOperand(1));
2870 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002871 case ISD::FP_TO_SINT:
2872 case ISD::FP_TO_UINT:
2873 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2874 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002875 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002876 break;
2877 case Promote:
2878 // The input result is prerounded, so we don't have to do anything
2879 // special.
2880 Tmp1 = PromoteOp(Node->getOperand(0));
2881 break;
2882 case Expand:
2883 assert(0 && "not implemented");
2884 }
Nate Begemand2558e32005-08-14 01:20:53 +00002885 // If we're promoting a UINT to a larger size, check to see if the new node
2886 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2887 // we can use that instead. This allows us to generate better code for
2888 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2889 // legal, such as PowerPC.
2890 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002891 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002892 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2893 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002894 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2895 } else {
2896 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2897 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002898 break;
2899
Chris Lattner2c8086f2005-04-02 05:00:07 +00002900 case ISD::FABS:
2901 case ISD::FNEG:
2902 Tmp1 = PromoteOp(Node->getOperand(0));
2903 assert(Tmp1.getValueType() == NVT);
2904 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2905 // NOTE: we do not have to do any extra rounding here for
2906 // NoExcessFPPrecision, because we know the input will have the appropriate
2907 // precision, and these operations don't modify precision at all.
2908 break;
2909
Chris Lattnerda6ba872005-04-28 21:44:33 +00002910 case ISD::FSQRT:
2911 case ISD::FSIN:
2912 case ISD::FCOS:
2913 Tmp1 = PromoteOp(Node->getOperand(0));
2914 assert(Tmp1.getValueType() == NVT);
2915 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002916 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002917 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2918 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002919 break;
2920
Chris Lattner03c85462005-01-15 05:21:40 +00002921 case ISD::AND:
2922 case ISD::OR:
2923 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002924 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002925 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002926 case ISD::MUL:
2927 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002928 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002929 // that too is okay if they are integer operations.
2930 Tmp1 = PromoteOp(Node->getOperand(0));
2931 Tmp2 = PromoteOp(Node->getOperand(1));
2932 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2933 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002934 break;
2935 case ISD::FADD:
2936 case ISD::FSUB:
2937 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002938 Tmp1 = PromoteOp(Node->getOperand(0));
2939 Tmp2 = PromoteOp(Node->getOperand(1));
2940 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2941 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2942
2943 // Floating point operations will give excess precision that we may not be
2944 // able to tolerate. If we DO allow excess precision, just leave it,
2945 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002946 // FIXME: Why would we need to round FP ops more than integer ones?
2947 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002948 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002949 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2950 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002951 break;
2952
Chris Lattner8b6fa222005-01-15 22:16:26 +00002953 case ISD::SDIV:
2954 case ISD::SREM:
2955 // These operators require that their input be sign extended.
2956 Tmp1 = PromoteOp(Node->getOperand(0));
2957 Tmp2 = PromoteOp(Node->getOperand(1));
2958 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002959 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2960 DAG.getValueType(VT));
2961 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2962 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002963 }
2964 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2965
2966 // Perform FP_ROUND: this is probably overly pessimistic.
2967 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002968 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2969 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002970 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002971 case ISD::FDIV:
2972 case ISD::FREM:
2973 // These operators require that their input be fp extended.
2974 Tmp1 = PromoteOp(Node->getOperand(0));
2975 Tmp2 = PromoteOp(Node->getOperand(1));
2976 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2977
2978 // Perform FP_ROUND: this is probably overly pessimistic.
2979 if (NoExcessFPPrecision)
2980 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2981 DAG.getValueType(VT));
2982 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002983
2984 case ISD::UDIV:
2985 case ISD::UREM:
2986 // These operators require that their input be zero extended.
2987 Tmp1 = PromoteOp(Node->getOperand(0));
2988 Tmp2 = PromoteOp(Node->getOperand(1));
2989 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002990 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2991 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002992 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2993 break;
2994
2995 case ISD::SHL:
2996 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002997 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002998 break;
2999 case ISD::SRA:
3000 // The input value must be properly sign extended.
3001 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003002 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3003 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003004 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003005 break;
3006 case ISD::SRL:
3007 // The input value must be properly zero extended.
3008 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003009 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003010 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003011 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003012
3013 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003014 Tmp1 = Node->getOperand(0); // Get the chain.
3015 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003016 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3017 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3018 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3019 } else {
3020 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
3021 Node->getOperand(2));
3022 // Increment the pointer, VAList, to the next vaarg
3023 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3024 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3025 TLI.getPointerTy()));
3026 // Store the incremented VAList to the legalized pointer
3027 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
3028 Node->getOperand(2));
3029 // Load the actual argument out of the pointer VAList
3030 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
3031 DAG.getSrcValue(0), VT);
3032 }
3033 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003034 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003035 break;
3036
Chris Lattner03c85462005-01-15 05:21:40 +00003037 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00003038 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
3039 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003040 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003041 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003042 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003043 case ISD::SEXTLOAD:
3044 case ISD::ZEXTLOAD:
3045 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00003046 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
3047 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00003048 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003049 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003050 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003051 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003052 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003053 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3054 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003055 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003056 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003057 case ISD::SELECT_CC:
3058 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3059 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3060 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003061 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003062 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003063 case ISD::BSWAP:
3064 Tmp1 = Node->getOperand(0);
3065 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3066 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3067 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3068 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3069 TLI.getShiftAmountTy()));
3070 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003071 case ISD::CTPOP:
3072 case ISD::CTTZ:
3073 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003074 // Zero extend the argument
3075 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003076 // Perform the larger operation, then subtract if needed.
3077 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003078 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003079 case ISD::CTPOP:
3080 Result = Tmp1;
3081 break;
3082 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003083 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003084 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003085 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003086 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00003087 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003088 break;
3089 case ISD::CTLZ:
3090 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003091 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3092 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003093 getSizeInBits(VT), NVT));
3094 break;
3095 }
3096 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003097 }
3098
3099 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003100
3101 // Make sure the result is itself legal.
3102 Result = LegalizeOp(Result);
3103
3104 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003105 AddPromotedOperand(Op, Result);
3106 return Result;
3107}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003108
Chris Lattner35481892005-12-23 00:16:34 +00003109/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003110/// The resultant code need not be legal. Note that SrcOp is the input operand
3111/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003112SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3113 SDOperand SrcOp) {
3114 // Create the stack frame object.
3115 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3116 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00003117 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00003118 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3119
3120 // Emit a store to the stack slot.
3121 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003122 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003123 // Result is a load from the stack slot.
3124 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3125}
3126
Chris Lattner5b359c62005-04-02 04:00:59 +00003127void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3128 SDOperand Op, SDOperand Amt,
3129 SDOperand &Lo, SDOperand &Hi) {
3130 // Expand the subcomponents.
3131 SDOperand LHSL, LHSH;
3132 ExpandOp(Op, LHSL, LHSH);
3133
3134 std::vector<SDOperand> Ops;
3135 Ops.push_back(LHSL);
3136 Ops.push_back(LHSH);
3137 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003138 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003139 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003140 Hi = Lo.getValue(1);
3141}
3142
3143
Chris Lattnere34b3962005-01-19 04:19:40 +00003144/// ExpandShift - Try to find a clever way to expand this shift operation out to
3145/// smaller elements. If we can't find a way that is more efficient than a
3146/// libcall on this target, return false. Otherwise, return true with the
3147/// low-parts expanded into Lo and Hi.
3148bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3149 SDOperand &Lo, SDOperand &Hi) {
3150 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3151 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003152
Chris Lattnere34b3962005-01-19 04:19:40 +00003153 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003154 SDOperand ShAmt = LegalizeOp(Amt);
3155 MVT::ValueType ShTy = ShAmt.getValueType();
3156 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3157 unsigned NVTBits = MVT::getSizeInBits(NVT);
3158
3159 // Handle the case when Amt is an immediate. Other cases are currently broken
3160 // and are disabled.
3161 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3162 unsigned Cst = CN->getValue();
3163 // Expand the incoming operand to be shifted, so that we have its parts
3164 SDOperand InL, InH;
3165 ExpandOp(Op, InL, InH);
3166 switch(Opc) {
3167 case ISD::SHL:
3168 if (Cst > VTBits) {
3169 Lo = DAG.getConstant(0, NVT);
3170 Hi = DAG.getConstant(0, NVT);
3171 } else if (Cst > NVTBits) {
3172 Lo = DAG.getConstant(0, NVT);
3173 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003174 } else if (Cst == NVTBits) {
3175 Lo = DAG.getConstant(0, NVT);
3176 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003177 } else {
3178 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3179 Hi = DAG.getNode(ISD::OR, NVT,
3180 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3181 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3182 }
3183 return true;
3184 case ISD::SRL:
3185 if (Cst > VTBits) {
3186 Lo = DAG.getConstant(0, NVT);
3187 Hi = DAG.getConstant(0, NVT);
3188 } else if (Cst > NVTBits) {
3189 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3190 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003191 } else if (Cst == NVTBits) {
3192 Lo = InH;
3193 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003194 } else {
3195 Lo = DAG.getNode(ISD::OR, NVT,
3196 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3197 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3198 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3199 }
3200 return true;
3201 case ISD::SRA:
3202 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003203 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003204 DAG.getConstant(NVTBits-1, ShTy));
3205 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003206 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003207 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003208 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003209 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003210 } else if (Cst == NVTBits) {
3211 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003212 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003213 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003214 } else {
3215 Lo = DAG.getNode(ISD::OR, NVT,
3216 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3217 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3218 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3219 }
3220 return true;
3221 }
3222 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003223 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003224}
Chris Lattner77e77a62005-01-21 06:05:23 +00003225
Chris Lattner9530ddc2005-05-13 05:09:11 +00003226/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
3227/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003228/// Found.
Chris Lattnerde387ce2006-01-09 23:21:49 +00003229static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found,
3230 std::set<SDNode*> &Visited) {
Chris Lattnera68d2042006-01-20 18:40:10 +00003231 if (Node->getNodeDepth() <= Found->getNodeDepth() ||
Chris Lattnerde387ce2006-01-09 23:21:49 +00003232 !Visited.insert(Node).second) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00003233
Chris Lattner16cd04d2005-05-12 23:24:06 +00003234 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003235 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00003236 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003237 Found = Node;
3238 return;
3239 }
3240
3241 // Otherwise, scan the operands of Node to see if any of them is a call.
3242 assert(Node->getNumOperands() != 0 &&
3243 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00003244 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattnerde387ce2006-01-09 23:21:49 +00003245 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003246
3247 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003248 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattnerde387ce2006-01-09 23:21:49 +00003249 Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003250}
3251
3252
Chris Lattner9530ddc2005-05-13 05:09:11 +00003253/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
3254/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003255/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00003256static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
3257 std::set<SDNode*> &Visited) {
Chris Lattnera68d2042006-01-20 18:40:10 +00003258 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
Chris Lattner82299e72005-08-05 18:10:27 +00003259 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003260
Chris Lattner16cd04d2005-05-12 23:24:06 +00003261 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003262 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00003263 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003264 Found = Node;
3265 return;
3266 }
3267
3268 // Otherwise, scan the operands of Node to see if any of them is a call.
3269 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
3270 if (UI == E) return;
3271 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00003272 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003273
3274 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00003275 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003276}
3277
Chris Lattner9530ddc2005-05-13 05:09:11 +00003278/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003279/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003280static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00003281 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003282 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00003283 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00003284 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003285
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003286 // The chain is usually at the end.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003287 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003288 if (TheChain.getValueType() != MVT::Other) {
3289 // Sometimes it's at the beginning.
Chris Lattner2789bde2005-05-14 08:34:53 +00003290 TheChain = SDOperand(Node, 0);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003291 if (TheChain.getValueType() != MVT::Other) {
3292 // Otherwise, hunt for it.
3293 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
3294 if (Node->getValueType(i) == MVT::Other) {
3295 TheChain = SDOperand(Node, i);
3296 break;
3297 }
3298
3299 // Otherwise, we walked into a node without a chain.
3300 if (TheChain.getValueType() != MVT::Other)
3301 return 0;
3302 }
3303 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003304
3305 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00003306 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003307
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003308 // Make sure to only follow users of our token chain.
3309 SDNode *User = *UI;
3310 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3311 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00003312 if (SDNode *Result = FindCallSeqEnd(User))
3313 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003314 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00003315 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003316}
3317
Chris Lattner9530ddc2005-05-13 05:09:11 +00003318/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003319/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003320static SDNode *FindCallSeqStart(SDNode *Node) {
3321 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00003322 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003323
3324 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3325 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00003326 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003327}
3328
3329
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003330/// FindInputOutputChains - If we are replacing an operation with a call we need
3331/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003332/// properly serialize the calls in the block. The returned operand is the
3333/// input chain value for the new call (e.g. the entry node or the previous
3334/// call), and OutChain is set to be the chain node to update to point to the
3335/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003336static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3337 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003338 SDNode *LatestCallSeqStart = Entry.Val;
3339 SDNode *LatestCallSeqEnd = 0;
Chris Lattnerde387ce2006-01-09 23:21:49 +00003340 std::set<SDNode*> Visited;
3341 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited);
3342 Visited.clear();
Chris Lattner9530ddc2005-05-13 05:09:11 +00003343 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003344
Chris Lattner16cd04d2005-05-12 23:24:06 +00003345 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00003346 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00003347 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3348 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00003349 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003350 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00003351 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3352 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003353 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00003354 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00003355 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00003356
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003357 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00003358 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003359 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00003360 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattnerde387ce2006-01-09 23:21:49 +00003361 Visited.clear();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003362
Chris Lattner9530ddc2005-05-13 05:09:11 +00003363 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003364 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00003365 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003366
Chris Lattner9530ddc2005-05-13 05:09:11 +00003367 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003368}
3369
Jeff Cohen00b168892005-07-27 06:12:32 +00003370/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003371void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3372 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003373 // Nothing to splice it into?
3374 if (OutChain == 0) return;
3375
3376 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3377 //OutChain->dump();
3378
3379 // Form a token factor node merging the old inval and the new inval.
3380 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3381 OutChain->getOperand(0));
3382 // Change the node to refer to the new token.
3383 OutChain->setAdjCallChain(InToken);
3384}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003385
3386
Chris Lattner77e77a62005-01-21 06:05:23 +00003387// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3388// does not fit into a register, return the lo part and set the hi part to the
3389// by-reg argument. If it does fit into a single register, return the result
3390// and leave the Hi part unset.
3391SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3392 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003393 SDNode *OutChain;
3394 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3395 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00003396 if (InChain.Val == 0)
3397 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003398
Chris Lattner77e77a62005-01-21 06:05:23 +00003399 TargetLowering::ArgListTy Args;
3400 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3401 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3402 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3403 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3404 }
3405 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003406
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003407 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003408 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003409 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003410 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3411 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003412
Chris Lattner99c25b82005-09-02 20:26:58 +00003413 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003414 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003415 default: assert(0 && "Unknown thing");
3416 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003417 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003418 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003419 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003420 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003421 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003422 }
Chris Lattner9c6b4b82006-01-28 04:28:26 +00003423
3424 CallInfo.second = LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003425 SpliceCallInto(CallInfo.second, OutChain);
Chris Lattner99c25b82005-09-02 20:26:58 +00003426 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003427}
3428
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003429
Chris Lattner77e77a62005-01-21 06:05:23 +00003430/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3431/// destination type is legal.
3432SDOperand SelectionDAGLegalize::
3433ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003434 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003435 assert(getTypeAction(Source.getValueType()) == Expand &&
3436 "This is not an expansion!");
3437 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3438
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003439 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003440 assert(Source.getValueType() == MVT::i64 &&
3441 "This only works for 64-bit -> FP");
3442 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3443 // incoming integer is set. To handle this, we dynamically test to see if
3444 // it is set, and, if so, add a fudge factor.
3445 SDOperand Lo, Hi;
3446 ExpandOp(Source, Lo, Hi);
3447
Chris Lattner66de05b2005-05-13 04:45:13 +00003448 // If this is unsigned, and not supported, first perform the conversion to
3449 // signed, then adjust the result if the sign bit is set.
3450 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3451 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3452
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003453 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3454 DAG.getConstant(0, Hi.getValueType()),
3455 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003456 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3457 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3458 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003459 uint64_t FF = 0x5f800000ULL;
3460 if (TLI.isLittleEndian()) FF <<= 32;
3461 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003462
Chris Lattner5839bf22005-08-26 17:15:30 +00003463 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003464 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3465 SDOperand FudgeInReg;
3466 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003467 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3468 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003469 else {
3470 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003471 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3472 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003473 }
Chris Lattner473a9902005-09-29 06:44:39 +00003474 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003475 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003476
Chris Lattnera88a2602005-05-14 05:33:54 +00003477 // Check to see if the target has a custom way to lower this. If so, use it.
3478 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3479 default: assert(0 && "This action not implemented for this operation!");
3480 case TargetLowering::Legal:
3481 case TargetLowering::Expand:
3482 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003483 case TargetLowering::Custom: {
3484 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3485 Source), DAG);
3486 if (NV.Val)
3487 return LegalizeOp(NV);
3488 break; // The target decided this was legal after all
3489 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003490 }
3491
Chris Lattner13689e22005-05-12 07:00:44 +00003492 // Expand the source, then glue it back together for the call. We must expand
3493 // the source in case it is shared (this pass of legalize must traverse it).
3494 SDOperand SrcLo, SrcHi;
3495 ExpandOp(Source, SrcLo, SrcHi);
3496 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3497
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003498 SDNode *OutChain = 0;
3499 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3500 DAG.getEntryNode());
3501 const char *FnName = 0;
3502 if (DestTy == MVT::f32)
3503 FnName = "__floatdisf";
3504 else {
3505 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3506 FnName = "__floatdidf";
3507 }
3508
Chris Lattner77e77a62005-01-21 06:05:23 +00003509 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3510
3511 TargetLowering::ArgListTy Args;
3512 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00003513
Chris Lattner77e77a62005-01-21 06:05:23 +00003514 Args.push_back(std::make_pair(Source, ArgTy));
3515
3516 // We don't care about token chains for libcalls. We just use the entry
3517 // node as our input and ignore the output chain. This allows us to place
3518 // calls wherever we need them to satisfy data dependences.
3519 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003520
3521 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00003522 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3523 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003524
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003525 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003526 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003527}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003528
Chris Lattnere34b3962005-01-19 04:19:40 +00003529
3530
Chris Lattner3e928bb2005-01-07 07:47:09 +00003531/// ExpandOp - Expand the specified SDOperand into its two component pieces
3532/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3533/// LegalizeNodes map is filled in for any results that are not expanded, the
3534/// ExpandedNodes map is filled in for any results that are expanded, and the
3535/// Lo/Hi values are returned.
3536void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3537 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003538 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003539 SDNode *Node = Op.Val;
3540 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003541 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3542 "Cannot expand FP values!");
3543 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003544 "Cannot expand to FP value or to larger int value!");
3545
Chris Lattner6fdcb252005-09-02 20:32:45 +00003546 // See if we already expanded it.
3547 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3548 = ExpandedNodes.find(Op);
3549 if (I != ExpandedNodes.end()) {
3550 Lo = I->second.first;
3551 Hi = I->second.second;
3552 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003553 }
3554
Chris Lattner3e928bb2005-01-07 07:47:09 +00003555 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003556 case ISD::CopyFromReg:
3557 assert(0 && "CopyFromReg must be legal!");
3558 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003559 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3560 assert(0 && "Do not know how to expand this operator!");
3561 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003562 case ISD::UNDEF:
3563 Lo = DAG.getNode(ISD::UNDEF, NVT);
3564 Hi = DAG.getNode(ISD::UNDEF, NVT);
3565 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003566 case ISD::Constant: {
3567 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3568 Lo = DAG.getConstant(Cst, NVT);
3569 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3570 break;
3571 }
Nate Begemancc827e62005-12-07 19:48:11 +00003572 case ISD::ConstantVec: {
3573 unsigned NumElements = Node->getNumOperands();
3574 // If we only have two elements left in the constant vector, just break it
3575 // apart into the two scalar constants it contains. Otherwise, bisect the
3576 // ConstantVec, and return each half as a new ConstantVec.
3577 // FIXME: this is hard coded as big endian, it may have to change to support
3578 // SSE and Alpha MVI
3579 if (NumElements == 2) {
3580 Hi = Node->getOperand(0);
3581 Lo = Node->getOperand(1);
3582 } else {
3583 NumElements /= 2;
3584 std::vector<SDOperand> LoOps, HiOps;
3585 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3586 HiOps.push_back(Node->getOperand(I));
3587 LoOps.push_back(Node->getOperand(I+NumElements));
3588 }
3589 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3590 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3591 }
3592 break;
3593 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003594
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003595 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003596 // Return the operands.
3597 Lo = Node->getOperand(0);
3598 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003599 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003600
3601 case ISD::SIGN_EXTEND_INREG:
3602 ExpandOp(Node->getOperand(0), Lo, Hi);
3603 // Sign extend the lo-part.
3604 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3605 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3606 TLI.getShiftAmountTy()));
3607 // sext_inreg the low part if needed.
3608 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3609 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003610
Nate Begemand88fc032006-01-14 03:14:10 +00003611 case ISD::BSWAP: {
3612 ExpandOp(Node->getOperand(0), Lo, Hi);
3613 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3614 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3615 Lo = TempLo;
3616 break;
3617 }
3618
Chris Lattneredb1add2005-05-11 04:51:16 +00003619 case ISD::CTPOP:
3620 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003621 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3622 DAG.getNode(ISD::CTPOP, NVT, Lo),
3623 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003624 Hi = DAG.getConstant(0, NVT);
3625 break;
3626
Chris Lattner39a8f332005-05-12 19:05:01 +00003627 case ISD::CTLZ: {
3628 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003629 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003630 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3631 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003632 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3633 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003634 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3635 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3636
3637 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3638 Hi = DAG.getConstant(0, NVT);
3639 break;
3640 }
3641
3642 case ISD::CTTZ: {
3643 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003644 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003645 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3646 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003647 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3648 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003649 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3650 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3651
3652 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3653 Hi = DAG.getConstant(0, NVT);
3654 break;
3655 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003656
Nate Begemanacc398c2006-01-25 18:21:52 +00003657 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003658 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3659 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003660 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3661 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3662
3663 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003664 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003665 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3666 if (!TLI.isLittleEndian())
3667 std::swap(Lo, Hi);
3668 break;
3669 }
3670
Chris Lattner3e928bb2005-01-07 07:47:09 +00003671 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003672 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3673 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003674 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003675
3676 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003677 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003678 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3679 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003680 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003681 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003682
3683 // Build a factor node to remember that this load is independent of the
3684 // other one.
3685 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3686 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003687
Chris Lattner3e928bb2005-01-07 07:47:09 +00003688 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003689 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003690 if (!TLI.isLittleEndian())
3691 std::swap(Lo, Hi);
3692 break;
3693 }
Nate Begemanab48be32005-11-22 18:16:00 +00003694 case ISD::VLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003695 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3696 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanab48be32005-11-22 18:16:00 +00003697 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3698 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3699
3700 // If we only have two elements, turn into a pair of scalar loads.
3701 // FIXME: handle case where a vector of two elements is fine, such as
3702 // 2 x double on SSE2.
3703 if (NumElements == 2) {
3704 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3705 // Increment the pointer to the other half.
3706 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3707 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3708 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003709 // FIXME: This creates a bogus srcvalue!
Nate Begemanab48be32005-11-22 18:16:00 +00003710 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3711 } else {
3712 NumElements /= 2; // Split the vector in half
3713 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3714 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3715 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3716 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003717 // FIXME: This creates a bogus srcvalue!
Nate Begemanab48be32005-11-22 18:16:00 +00003718 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3719 }
3720
3721 // Build a factor node to remember that this load is independent of the
3722 // other one.
3723 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3724 Hi.getValue(1));
3725
3726 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003727 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemanab48be32005-11-22 18:16:00 +00003728 if (!TLI.isLittleEndian())
3729 std::swap(Lo, Hi);
3730 break;
3731 }
3732 case ISD::VADD:
3733 case ISD::VSUB:
3734 case ISD::VMUL: {
3735 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3736 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3737 SDOperand LL, LH, RL, RH;
3738
3739 ExpandOp(Node->getOperand(0), LL, LH);
3740 ExpandOp(Node->getOperand(1), RL, RH);
3741
3742 // If we only have two elements, turn into a pair of scalar loads.
3743 // FIXME: handle case where a vector of two elements is fine, such as
3744 // 2 x double on SSE2.
3745 if (NumElements == 2) {
3746 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3747 Lo = DAG.getNode(Opc, EVT, LL, RL);
3748 Hi = DAG.getNode(Opc, EVT, LH, RH);
3749 } else {
3750 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3751 LL.getOperand(3));
3752 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3753 LH.getOperand(3));
3754 }
3755 break;
3756 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003757 case ISD::AND:
3758 case ISD::OR:
3759 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3760 SDOperand LL, LH, RL, RH;
3761 ExpandOp(Node->getOperand(0), LL, LH);
3762 ExpandOp(Node->getOperand(1), RL, RH);
3763 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3764 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3765 break;
3766 }
3767 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003768 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003769 ExpandOp(Node->getOperand(1), LL, LH);
3770 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003771 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3772 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003773 break;
3774 }
Nate Begeman9373a812005-08-10 20:51:12 +00003775 case ISD::SELECT_CC: {
3776 SDOperand TL, TH, FL, FH;
3777 ExpandOp(Node->getOperand(2), TL, TH);
3778 ExpandOp(Node->getOperand(3), FL, FH);
3779 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3780 Node->getOperand(1), TL, FL, Node->getOperand(4));
3781 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3782 Node->getOperand(1), TH, FH, Node->getOperand(4));
3783 break;
3784 }
Nate Begeman144ff662005-10-13 17:15:37 +00003785 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003786 SDOperand Chain = Node->getOperand(0);
3787 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003788 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3789
3790 if (EVT == NVT)
3791 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3792 else
3793 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3794 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003795
3796 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003797 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003798
Nate Begeman144ff662005-10-13 17:15:37 +00003799 // The high part is obtained by SRA'ing all but one of the bits of the lo
3800 // part.
3801 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3802 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3803 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003804 break;
3805 }
3806 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003807 SDOperand Chain = Node->getOperand(0);
3808 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003809 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3810
3811 if (EVT == NVT)
3812 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3813 else
3814 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3815 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003816
3817 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003818 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003819
Nate Begeman144ff662005-10-13 17:15:37 +00003820 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003821 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003822 break;
3823 }
3824 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003825 SDOperand Chain = Node->getOperand(0);
3826 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003827 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3828
3829 if (EVT == NVT)
3830 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3831 else
3832 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3833 EVT);
3834
3835 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003836 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003837
3838 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00003839 Hi = DAG.getNode(ISD::UNDEF, NVT);
3840 break;
3841 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003842 case ISD::ANY_EXTEND:
3843 // The low part is any extension of the input (which degenerates to a copy).
3844 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3845 // The high part is undefined.
3846 Hi = DAG.getNode(ISD::UNDEF, NVT);
3847 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003848 case ISD::SIGN_EXTEND: {
3849 // The low part is just a sign extension of the input (which degenerates to
3850 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003851 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003852
Chris Lattner3e928bb2005-01-07 07:47:09 +00003853 // The high part is obtained by SRA'ing all but one of the bits of the lo
3854 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003855 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00003856 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3857 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003858 break;
3859 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003860 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003861 // The low part is just a zero extension of the input (which degenerates to
3862 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003863 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003864
Chris Lattner3e928bb2005-01-07 07:47:09 +00003865 // The high part is just a zero.
3866 Hi = DAG.getConstant(0, NVT);
3867 break;
Chris Lattner35481892005-12-23 00:16:34 +00003868
3869 case ISD::BIT_CONVERT: {
3870 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3871 Node->getOperand(0));
3872 ExpandOp(Tmp, Lo, Hi);
3873 break;
3874 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003875
Chris Lattner8137c9e2006-01-28 05:07:51 +00003876 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00003877 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3878 TargetLowering::Custom &&
3879 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003880 Lo = TLI.LowerOperation(Op, DAG);
3881 assert(Lo.Val && "Node must be custom expanded!");
3882 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00003883 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003884 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003885 break;
3886
Chris Lattner4e6c7462005-01-08 19:27:05 +00003887 // These operators cannot be expanded directly, emit them as calls to
3888 // library functions.
3889 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003890 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003891 SDOperand Op;
3892 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3893 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003894 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3895 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00003896 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003897
Chris Lattnerf20d1832005-07-30 01:40:57 +00003898 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3899
Chris Lattner80a3e942005-07-29 00:33:32 +00003900 // Now that the custom expander is done, expand the result, which is still
3901 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003902 if (Op.Val) {
3903 ExpandOp(Op, Lo, Hi);
3904 break;
3905 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003906 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003907
Chris Lattner4e6c7462005-01-08 19:27:05 +00003908 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003909 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003910 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003911 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003912 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003913
Chris Lattner4e6c7462005-01-08 19:27:05 +00003914 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003915 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003916 SDOperand Op;
3917 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3918 case Expand: assert(0 && "cannot expand FP!");
3919 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3920 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3921 }
3922
3923 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3924
3925 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00003926 if (Op.Val) {
3927 ExpandOp(Op, Lo, Hi);
3928 break;
3929 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003930 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003931
Chris Lattner4e6c7462005-01-08 19:27:05 +00003932 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003933 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003934 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003935 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003936 break;
3937
Evan Cheng05a2d562006-01-09 18:31:59 +00003938 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003939 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003940 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003941 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003942 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003943 Op = TLI.LowerOperation(Op, DAG);
3944 if (Op.Val) {
3945 // Now that the custom expander is done, expand the result, which is
3946 // still VT.
3947 ExpandOp(Op, Lo, Hi);
3948 break;
3949 }
3950 }
3951
Chris Lattnere34b3962005-01-19 04:19:40 +00003952 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003953 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003954 break;
Chris Lattner47599822005-04-02 03:38:53 +00003955
3956 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003957 TargetLowering::LegalizeAction Action =
3958 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3959 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3960 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003961 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003962 break;
3963 }
3964
Chris Lattnere34b3962005-01-19 04:19:40 +00003965 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003966 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003967 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003968 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003969
Evan Cheng05a2d562006-01-09 18:31:59 +00003970 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003971 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003972 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003973 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003974 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003975 Op = TLI.LowerOperation(Op, DAG);
3976 if (Op.Val) {
3977 // Now that the custom expander is done, expand the result, which is
3978 // still VT.
3979 ExpandOp(Op, Lo, Hi);
3980 break;
3981 }
3982 }
3983
Chris Lattnere34b3962005-01-19 04:19:40 +00003984 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003985 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003986 break;
Chris Lattner47599822005-04-02 03:38:53 +00003987
3988 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003989 TargetLowering::LegalizeAction Action =
3990 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3991 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3992 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003993 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003994 break;
3995 }
3996
Chris Lattnere34b3962005-01-19 04:19:40 +00003997 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003998 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003999 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004000 }
4001
4002 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004003 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004004 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004005 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004006 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004007 Op = TLI.LowerOperation(Op, DAG);
4008 if (Op.Val) {
4009 // Now that the custom expander is done, expand the result, which is
4010 // still VT.
4011 ExpandOp(Op, Lo, Hi);
4012 break;
4013 }
4014 }
4015
Chris Lattnere34b3962005-01-19 04:19:40 +00004016 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004017 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004018 break;
Chris Lattner47599822005-04-02 03:38:53 +00004019
4020 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004021 TargetLowering::LegalizeAction Action =
4022 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4023 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4024 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004025 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004026 break;
4027 }
4028
Chris Lattnere34b3962005-01-19 04:19:40 +00004029 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004030 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004031 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004032 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004033
Misha Brukmanedf128a2005-04-21 22:36:52 +00004034 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004035 case ISD::SUB: {
4036 // If the target wants to custom expand this, let them.
4037 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4038 TargetLowering::Custom) {
4039 Op = TLI.LowerOperation(Op, DAG);
4040 if (Op.Val) {
4041 ExpandOp(Op, Lo, Hi);
4042 break;
4043 }
4044 }
4045
4046 // Expand the subcomponents.
4047 SDOperand LHSL, LHSH, RHSL, RHSH;
4048 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4049 ExpandOp(Node->getOperand(1), RHSL, RHSH);
4050
4051 std::vector<SDOperand> Ops;
4052 Ops.push_back(LHSL);
4053 Ops.push_back(LHSH);
4054 Ops.push_back(RHSL);
4055 Ops.push_back(RHSH);
4056 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
4057 unsigned Opc =
4058 Node->getOpcode() == ISD::ADD ? ISD::ADD_PARTS : ISD::SUB_PARTS;
4059 Lo = DAG.getNode(Opc, VTs, Ops);
4060 Hi = Lo.getValue(1);
Chris Lattner84f67882005-01-20 18:52:28 +00004061 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00004062 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004063 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004064 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004065 SDOperand LL, LH, RL, RH;
4066 ExpandOp(Node->getOperand(0), LL, LH);
4067 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004068 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4069 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4070 // extended the sign bit of the low half through the upper half, and if so
4071 // emit a MULHS instead of the alternate sequence that is valid for any
4072 // i64 x i64 multiply.
4073 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4074 // is RH an extension of the sign bit of RL?
4075 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4076 RH.getOperand(1).getOpcode() == ISD::Constant &&
4077 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4078 // is LH an extension of the sign bit of LL?
4079 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4080 LH.getOperand(1).getOpcode() == ISD::Constant &&
4081 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4082 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4083 } else {
4084 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4085 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4086 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4087 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4088 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4089 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004090 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4091 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004092 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004093 }
4094 break;
4095 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004096 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4097 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4098 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4099 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004100 }
4101
Chris Lattner83397362005-12-21 18:02:52 +00004102 // Make sure the resultant values have been legalized themselves, unless this
4103 // is a type that requires multi-step expansion.
4104 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4105 Lo = LegalizeOp(Lo);
4106 Hi = LegalizeOp(Hi);
4107 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004108
4109 // Remember in a map if the values will be reused later.
4110 bool isNew =
4111 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4112 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004113}
4114
4115
4116// SelectionDAG::Legalize - This is the entry point for the file.
4117//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004118void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004119 /// run - This is the main entry point to this class.
4120 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004121 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004122}
4123