blob: 6505fedaf8e84d32d590811790393afa14ada86f [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattnerdc750592005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman89b049a2005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000056
Chris Lattnerdc750592005-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 Lattner1f2c9d82005-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 Lattnerdc750592005-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 Lattnerea4ca942005-01-07 22:28:47 +000073 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-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 Lattnerea4ca942005-01-07 22:28:47 +000078 }
Chris Lattner1f2c9d82005-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 Lattner2af3ee42005-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 Lattner1f2c9d82005-01-15 05:21:40 +000084 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000085
Chris Lattnerdc750592005-01-07 07:47:09 +000086public:
87
Chris Lattner4add7e32005-01-23 04:42:50 +000088 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000089
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +0000103 void LegalizeDAG();
104
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000105private:
106
Chris Lattnerdc750592005-01-07 07:47:09 +0000107 SDOperand LegalizeOp(SDOperand O);
108 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000109 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000110
Chris Lattneraac464e2005-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 Lattnere3e847b2005-07-16 00:19:57 +0000115
Chris Lattner36e663d2005-12-23 00:16:34 +0000116 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000117 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
118 SDOperand LegalOp,
119 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000120 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
121 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000122 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
123 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000124
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000125 SDOperand ExpandBSWAP(SDOperand Op);
126 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000127 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
128 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000129 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
130 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000131 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
132
Chris Lattnerdc750592005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
Chris Lattner301015a2005-11-19 05:51:46 +0000139static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000140 switch (VecOp) {
141 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-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 Lattnerdc750592005-01-07 07:47:09 +0000147
Chris Lattner4add7e32005-01-23 04:42:50 +0000148SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
149 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
150 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000151 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000152 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000153}
154
Jim Laskeyf2516a92005-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 Lattnere3e847b2005-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 Laskeyf2516a92005-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 Laskey686d6a12005-08-17 17:42:52 +0000202 SDOperand Bias = DAG.getConstantFP(isSigned ?
203 BitsToDouble(0x4330000080000000ULL)
204 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000205 MVT::f64);
206 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000207 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-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 Lattner2af3ee42005-12-20 00:53:54 +0000218 return LegalizeOp(Result);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000219 }
220 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000221 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000222
Chris Lattnerd47675e2005-08-09 20:20:18 +0000223 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
224 DAG.getConstant(0, Op0.getValueType()),
225 ISD::SETLT);
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000229
Jim Laskeyf2516a92005-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 Lattnerb35912e2005-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 Lattnere3e847b2005-07-16 00:19:57 +0000241 if (TLI.isLittleEndian()) FF <<= 32;
242 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000243
Chris Lattnerc30405e2005-08-26 17:15:30 +0000244 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000256
Chris Lattner2af3ee42005-12-20 00:53:54 +0000257 return LegalizeOp(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000258}
259
Chris Lattner19732782005-08-16 18:17:10 +0000260/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000261/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-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 Begeman7e74c832005-07-16 02:02:34 +0000265SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
266 MVT::ValueType DestVT,
267 bool isSigned) {
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000270
Chris Lattnere3e847b2005-07-16 00:19:57 +0000271 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000272
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000277
Chris Lattnere3e847b2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000282 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-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 Begeman7e74c832005-07-16 02:02:34 +0000290 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000291
Chris Lattnere3e847b2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000296 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000304
Chris Lattnere3e847b2005-07-16 00:19:57 +0000305 // Otherwise, try a larger type.
306 }
307
Chris Lattnere3e847b2005-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 Lattner2af3ee42005-12-20 00:53:54 +0000310 SDOperand N = DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000311 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
312 NewInTy, LegalOp));
Chris Lattner2af3ee42005-12-20 00:53:54 +0000313 // Make sure to legalize any nodes we create here.
314 return LegalizeOp(N);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000315}
316
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000327
Chris Lattner44fe26f2005-07-29 00:11:56 +0000328 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000329
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000334
Chris Lattner44fe26f2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000339 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000347
Chris Lattner44fe26f2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000352 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000360
Chris Lattner44fe26f2005-07-29 00:11:56 +0000361 // Otherwise, try a larger type.
362 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000363
Chris Lattner44fe26f2005-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 Lattner2af3ee42005-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 Lattner44fe26f2005-07-29 00:11:56 +0000370}
371
Chris Lattner9dcce6d2006-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 Lattner4bbbb9e2005-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 Lattner4bbbb9e2005-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 Lattner44fe26f2005-07-29 00:11:56 +0000505
Chris Lattnerdc750592005-01-07 07:47:09 +0000506void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-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 Lattner4bbbb9e2005-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 Lattner9cfccfb2005-10-02 17:49:46 +0000515
Chris Lattner4bbbb9e2005-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 Lattnerbf4f23322005-11-09 23:47:37 +0000520 if (I->getNumOperands() == 0) {
521 Visited[I] = 0 - 1U;
522 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000523 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000524 }
525
Chris Lattnerbf4f23322005-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 Lattner4bbbb9e2005-10-06 01:20:27 +0000529 "Error: DAG is cyclic!");
530 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000531
Chris Lattner4bbbb9e2005-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 Lattnerdc750592005-01-07 07:47:09 +0000551 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000552 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
553 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000554
555 ExpandedNodes.clear();
556 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000557 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000558
559 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000560 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000561}
562
563SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000564 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000565 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000566 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000567
Chris Lattnerdc750592005-01-07 07:47:09 +0000568 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000569 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-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 Lattnerdc750592005-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 Lattner1f2c9d82005-01-15 05:21:40 +0000582 PromoteOp(Op.getValue(i));
583 assert(LegalizedNodes.count(Op) &&
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000584 "Promotion didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000585 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000586 }
587 }
588
Chris Lattnerb5a78e02005-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 Lattner85d70c62005-01-11 05:57:22 +0000591 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
592 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000593
Nate Begemane5b86d72005-08-10 20:51:12 +0000594 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000595 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000596 bool isCustom = false;
597
Chris Lattnerdc750592005-01-07 07:47:09 +0000598 switch (Node->getOpcode()) {
599 default:
Chris Lattner3eb86932005-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 Lattnerdc750592005-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 Lattner4bbbb9e2005-10-06 01:20:27 +0000628 case ISD::TargetFrameIndex:
629 case ISD::Register:
630 case ISD::TargetConstant:
Nate Begeman4e56db62005-12-10 02:36:00 +0000631 case ISD::TargetConstantPool:
Chris Lattnerdc750592005-01-07 07:47:09 +0000632 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000633 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000634 case ISD::ExternalSymbol:
Andrew Lenharth72594262005-12-24 23:42:32 +0000635 case ISD::TargetExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000636 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000637 case ISD::BasicBlock:
638 case ISD::CONDCODE:
639 case ISD::VALUETYPE:
640 case ISD::SRCVALUE:
Chris Lattner435b4022005-11-29 06:21:05 +0000641 case ISD::STRING:
Chris Lattner45ca1c02005-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 Lattner9dcce6d2006-01-28 07:39:30 +0000647 Result = Tmp;
Chris Lattner45ca1c02005-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 Lattnerdc750592005-01-07 07:47:09 +0000655 break;
Chris Lattnerd9af1aa2005-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 Lattner44c28c22005-11-20 22:56:56 +0000663 case ISD::MERGE_VALUES:
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000664 Result = Node->getOperand(Op.ResNo);
665 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000666 case ISD::CopyFromReg:
667 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000668 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000669 if (Node->getNumValues() == 2) {
Chris Lattnere3c67e92005-12-18 15:27:43 +0000670 if (Tmp1 != Node->getOperand(0))
671 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattner33182322005-08-16 21:55:35 +0000672 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattnere3c67e92005-12-18 15:27:43 +0000673 Node->getValueType(0));
674 } else {
Chris Lattnerebcfa0c22005-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 Lattnere3c67e92005-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 Lattnereb6614d2005-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 Begemancda9aa72005-04-01 22:34:39 +0000690 case ISD::UNDEF: {
691 MVT::ValueType VT = Op.getValueType();
692 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000693 default: assert(0 && "This action is not supported yet!");
694 case TargetLowering::Expand:
Nate Begemancda9aa72005-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 Begeman69d39432005-04-02 00:41:14 +0000702 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000703 break;
704 }
705 break;
706 }
Chris Lattner435b4022005-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 Laskey7c462762005-12-16 22:45:29 +0000715 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000716 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-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 Laskeyb9966022006-01-17 17:31:53 +0000725 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000726
Jim Laskey9e296be2005-12-21 20:51:37 +0000727 std::vector<SDOperand> Ops;
728 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-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 Laskey9e296be2005-12-21 20:51:37 +0000744 } else {
745 Result = Tmp1; // chain
746 }
Chris Lattner435b4022005-11-29 06:21:05 +0000747 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000748 }
Chris Lattner435b4022005-11-29 06:21:05 +0000749 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000750 if (Tmp1 != Node->getOperand(0) ||
751 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000752 std::vector<SDOperand> Ops;
753 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-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 Lattner435b4022005-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 Laskey7c462762005-12-16 22:45:29 +0000769
770 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000771 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000772 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000773 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-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 Laskey762e9ec2006-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 Laskey7c462762005-12-16 22:45:29 +0000801 }
802 break;
803 }
804 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000805
Chris Lattnerdc750592005-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 Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +0000831 bool Extend = false;
832
Chris Lattner9dcce6d2006-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 Lattnerdc750592005-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 Lattnerbc7497d2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000844 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000845 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
846 VT = MVT::f32;
847 Extend = true;
848 }
Misha Brukman835702a2005-04-21 22:36:52 +0000849
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000850 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000851 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000852 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
853 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000854 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000855 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
856 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000857 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000858 }
859 break;
860 }
Nate Begeman41b1cdc2005-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 Lattner9dcce6d2006-01-28 07:39:30 +0000865 // FIXME: Allow custom lowering to TargetConstantVec's.
Nate Begeman41b1cdc2005-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 Lattner9dcce6d2006-01-28 07:39:30 +0000881 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000882 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
883 break;
884 }
Chris Lattneraf3aefa2005-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 Lattner05b4e372005-01-13 17:59:25 +0000903 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000904 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000905
Chris Lattner2dce7032005-05-12 23:24:06 +0000906 case ISD::CALLSEQ_START:
907 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000908 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000909 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000910 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000911 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000912 Node->setAdjCallChain(Tmp1);
Chris Lattnerf9a1e3a2006-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 Begeman54fb5002005-10-04 00:37:37 +0000920
Chris Lattner2dce7032005-05-12 23:24:06 +0000921 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-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 Lattnerdc750592005-01-07 07:47:09 +0000925 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000926 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-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 Lattner96c262e2005-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 Lattner02f5ce22005-01-09 19:07:54 +0000937 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000938
Chris Lattner2d591422006-01-15 08:43:08 +0000939 Tmp1 = Result;
940 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-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 Lattner59b82f92006-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 Lattner2d591422006-01-15 08:43:08 +0000956 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
957 if (Tmp3.Val) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000958 Tmp1 = Tmp3;
959 Tmp2 = Tmp3.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +0000960 }
Chris Lattner59b82f92006-01-15 08:54:32 +0000961 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000962 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +0000963 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000964 }
Chris Lattner59b82f92006-01-15 08:54:32 +0000965 // Since this op produce two values, make sure to remember that we
966 // legalized both of them.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000967 AddLegalizedOperand(SDOperand(Node, 0), LegalizeOp(Tmp1));
968 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Tmp2));
Chris Lattner59b82f92006-01-15 08:54:32 +0000969 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000970 }
Chris Lattner476e67b2006-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 Lattner68a12142005-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 Lattnerec3fe7c2005-01-07 08:19:42 +0000999 case ISD::BRCOND:
1000 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +00001001
Chris Lattnerd65c3f32005-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 Lattner9dcce6d2006-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 Begeman371e4952005-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 Lattner9dcce6d2006-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 Begeman371e4952005-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 Lattner539c3fa2005-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 Begeman371e4952005-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 Begeman371e4952005-08-16 19:49:35 +00001042 }
1043 break;
1044 case ISD::BR_CC:
1045 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001046 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-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 Lattnerbf0bd992005-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 Lattner9dcce6d2006-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 Lattnerbf0bd992005-12-17 23:46:46 +00001077 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1078 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-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 Lattnerbf0bd992005-12-17 23:46:46 +00001083 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001084 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001085 break;
Chris Lattnerfd986782005-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 Lattner9dcce6d2006-01-28 07:39:30 +00001097
1098
Chris Lattnerfd986782005-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 Begeman371e4952005-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 Lattnerf12eb4d2005-08-24 16:35:28 +00001118 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-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 Lattner9dcce6d2006-01-28 07:39:30 +00001130 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001131 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
1132 }
Chris Lattnerfd986782005-04-09 03:30:19 +00001133 break;
1134 }
1135 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001136 case ISD::BRTWOWAY_CC:
1137 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001138 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-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 Cheng31d15fa2005-12-23 07:29:34 +00001179 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001180 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1181 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001182
Evan Cheng31d15fa2005-12-23 07:29:34 +00001183 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-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 Cheng31d15fa2005-12-23 07:29:34 +00001189 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1190 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001191 case TargetLowering::Custom:
1192 Tmp1 = TLI.LowerOperation(Result, DAG);
1193 if (Tmp1.Val) {
Nate Begemane74795c2006-01-25 18:21:52 +00001194 // Since loads produce two values, make sure to remember that we
1195 // legalized both of them.
Chris Lattner9dcce6d2006-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 Cheng31d15fa2005-12-23 07:29:34 +00001199 }
1200 // FALLTHROUGH if the target thinks it is legal.
Evan Cheng31d15fa2005-12-23 07:29:34 +00001201 case TargetLowering::Legal:
Evan Cheng31d15fa2005-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 Lattnerc6c9a5b2005-01-15 06:18:18 +00001210 case ISD::EXTLOAD:
1211 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001212 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001213 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1214 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001215
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001216 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001217 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001218 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001219 case TargetLowering::Promote:
1220 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001221 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1222 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-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 Brukman835702a2005-04-21 22:36:52 +00001228
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001229 case TargetLowering::Custom:
1230 isCustom = true;
1231 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001232 case TargetLowering::Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001233 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001234 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1235 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001236 else
1237 Result = SDOperand(Node, 0);
Chris Lattner9dcce6d2006-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 Lattnera3b7ef02005-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 Lattner9dcce6d2006-01-28 07:39:30 +00001251 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1252 return Op.ResNo ? Tmp1 : Result;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001253 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001254 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-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 Lenharth0a370f42005-06-30 19:32:57 +00001257 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc06da622005-12-18 23:54:29 +00001258 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001259 Load = LegalizeOp(Load);
1260 AddLegalizedOperand(SDOperand(Node, 0), Result);
1261 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001262 return Op.ResNo ? Load.getValue(1) : Result;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001263 }
Chris Lattnera3b7ef02005-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 Lattnerde0a4b12005-07-10 01:55:33 +00001268 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1269 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-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 Lattner0b6ba902005-07-10 00:07:11 +00001273 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001274 else
1275 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc06da622005-12-18 23:54:29 +00001276 Result = LegalizeOp(Result); // Relegalize new nodes.
1277 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001278 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1279 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001280 return Op.ResNo ? Result.getValue(1) : ValRes;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001281 }
1282 assert(0 && "Unreachable");
1283 }
Nate Begeman5172ce62005-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 Begeman5172ce62005-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 Lattnerdc750592005-01-07 07:47:09 +00001312 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001313 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001314
1315 case ISD::CopyToReg:
1316 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001317
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001318 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001319 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001320 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001321 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001322 if (Node->getNumValues() == 1) {
Chris Lattnere3c67e92005-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 Lattnerebcfa0c22005-12-18 15:36:21 +00001327 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1328 if (Node->getNumOperands() == 4)
1329 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001330 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001331 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattnere3c67e92005-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 Lattnerdc750592005-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 Lattnerea4ca942005-01-07 22:28:47 +00001351 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00001358 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001359 }
1360 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001361 Tmp2 = PromoteOp(Node->getOperand(1));
1362 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1363 break;
Chris Lattnerdc750592005-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 Lattner7e6eeba2005-01-08 19:27:05 +00001376 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00001383 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001384 }
1385 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001386 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001387 }
1388 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1389 break;
1390 }
1391 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001392
Chris Lattnerfae8afb2006-01-06 05:47:48 +00001393 switch (TLI.getOperationAction(Node->getOpcode(),
1394 Node->getValueType(0))) {
Evan Chengf35b1c82006-01-06 00:41:43 +00001395 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-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 Chengf35b1c82006-01-06 00:41:43 +00001400 break;
1401 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001402 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001403 case ISD::STORE: {
Chris Lattnerdc750592005-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 Lattnere69daaf2005-01-08 06:25:56 +00001407 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001408 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001409 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001410 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001411 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001412 DAG.getConstant(FloatToBits(CFP->getValue()),
1413 MVT::i32),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001414 Tmp2, Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001415 } else {
1416 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001417 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001418 DAG.getConstant(DoubleToBits(CFP->getValue()),
1419 MVT::i64),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001420 Tmp2, Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001421 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001422 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001423 }
1424
Chris Lattnerdc750592005-01-07 07:47:09 +00001425 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1426 case Legal: {
1427 SDOperand Val = LegalizeOp(Node->getOperand(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001428 if (Tmp1 != Node->getOperand(0) || Val != Node->getOperand(1) ||
Chris Lattnerdc750592005-01-07 07:47:09 +00001429 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001430 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1431 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001432
1433 MVT::ValueType VT = Result.Val->getOperand(1).getValueType();
Chris Lattner9dcce6d2006-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 Cheng31d15fa2005-12-23 07:29:34 +00001441 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001442 break;
1443 }
1444 case Promote:
Chris Lattner1f2c9d82005-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 Lenharth4a73c2c2005-04-27 20:10:01 +00001448 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001449 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001450 break;
1451
Chris Lattnerdc750592005-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 Lattner55e9cde2005-05-11 04:51:16 +00001459 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1460 Node->getOperand(3));
Nate Begemand37c1312005-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 Lattner9dcce6d2006-01-28 07:39:30 +00001464 unsigned IncrementSize;
Nate Begemand37c1312005-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 Lattnerdc750592005-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 Lattner9dcce6d2006-01-28 07:39:30 +00001476 // FIXME: This sets the srcvalue of both halves to be the same, which is
1477 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001478 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1479 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001480 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1481 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001482 }
1483 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001484 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001485 case ISD::PCMARKER:
1486 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001487 if (Tmp1 != Node->getOperand(0))
1488 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001489 break;
Chris Lattnerb3266452006-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 Lattner9dcce6d2006-01-28 07:39:30 +00001500
1501 Tmp1 = Result.getValue(1);
1502
Chris Lattnerb3266452006-01-13 02:50:02 +00001503 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1504 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-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 Lattnerb3266452006-01-13 02:50:02 +00001511 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001512 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001513 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001514 // Expand to CopyFromReg if the target set
1515 // StackPointerRegisterToSaveRestore.
1516 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001517 Tmp2 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001518 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001519 Result = Tmp2;
1520 Tmp1 = Tmp2.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001521 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001522 Result = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1523 Tmp1 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001524 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001525 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001526 }
Chris Lattner9dcce6d2006-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 Lattnerb3266452006-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 Lattner9dcce6d2006-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 Lattnerb3266452006-01-13 02:50:02 +00001546 break;
1547 case TargetLowering::Expand:
Chris Lattnered9b3e12006-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 Lattnerb3266452006-01-13 02:50:02 +00001555 break;
1556 }
1557 break;
1558
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001559 case ISD::READCYCLECOUNTER:
1560 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthf9b27d72005-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 Lenharth73420b32005-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 Lenharth627cbd42005-11-20 21:32:07 +00001575
Evan Cheng31d15fa2005-12-23 07:29:34 +00001576 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-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 Lattner9dcce6d2006-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 Lattner2d454bf2005-09-10 00:20:18 +00001594
Chris Lattner9dcce6d2006-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 Cheng31d15fa2005-12-23 07:29:34 +00001600
Chris Lattner9dcce6d2006-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 Lattnerc6c9a5b2005-01-15 06:18:18 +00001608 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001609 }
1610 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001611 }
Chris Lattner39c67442005-01-14 22:08:15 +00001612 case ISD::SELECT:
Chris Lattnerd65c3f32005-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 Lattnerdc750592005-01-07 07:47:09 +00001622 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001623 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001624
Chris Lattner9dcce6d2006-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 Begeman987121a2005-08-23 04:29:48 +00001630 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001631 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-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 Begemane5b86d72005-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 Lattner539c3fa2005-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 Begemane5b86d72005-08-10 20:51:12 +00001647 Result = DAG.getSelectCC(Tmp1,
1648 DAG.getConstant(0, Tmp1.getValueType()),
1649 Tmp2, Tmp3, ISD::SETNE);
1650 }
Chris Lattner3c0dd462005-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 Lattner9dcce6d2006-01-28 07:39:30 +00001657 ExtOp = ISD::ANY_EXTEND;
1658 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001659 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001660 ExtOp = ISD::FP_EXTEND;
1661 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-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 Lattnerdc750592005-01-07 07:47:09 +00001672 break;
Nate Begemane5b86d72005-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 Lattnerf12eb4d2005-08-24 16:35:28 +00001677 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-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 Lattner5f573412005-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 Lattner9dcce6d2006-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 Lattner5f573412005-08-26 00:23:59 +00001694 break;
Nate Begemane5b86d72005-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 Begeman371e4952005-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 Begemane5b86d72005-08-10 20:51:12 +00001715 }
1716 break;
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +00001722 break;
1723 case Promote:
Chris Lattner4d978642005-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 Lattner87a769c2005-01-16 01:11:45 +00001730 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-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 Lattnerd47675e2005-08-09 20:20:18 +00001736 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-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 Lattner0e852af2005-04-13 02:38:47 +00001747 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1748 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001749 break;
1750 case ISD::SETGE:
1751 case ISD::SETGT:
1752 case ISD::SETLT:
1753 case ISD::SETLE:
Chris Lattner0b6ba902005-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 Lattner4d978642005-01-15 22:16:26 +00001758 break;
1759 }
Chris Lattner4d978642005-01-15 22:16:26 +00001760 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001761 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001762 case Expand:
Chris Lattnerdc750592005-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 Lattnerd47675e2005-08-09 20:20:18 +00001766 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001767 case ISD::SETEQ:
1768 case ISD::SETNE:
Chris Lattner71ff44e2005-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 Begeman987121a2005-08-23 04:29:48 +00001774 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001775 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001776 }
1777
Chris Lattnerdc750592005-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 Begeman987121a2005-08-23 04:29:48 +00001781 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001782 break;
1783 default:
Chris Lattneraedcabe2005-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 Lattnerd47675e2005-08-09 20:20:18 +00001787 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001788 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001789 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001790 (CST->isAllOnesValue()))) { // X > -1
1791 Tmp1 = LHSHi;
1792 Tmp2 = RHSHi;
1793 break;
1794 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001795
Chris Lattnerdc750592005-01-07 07:47:09 +00001796 // FIXME: This generated code sucks.
1797 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001798 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00001809
Chris Lattnerdc750592005-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 Lattnerd47675e2005-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 Begeman987121a2005-08-23 04:29:48 +00001820 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1821 Result, Tmp1, Tmp2));
Chris Lattner2af3ee42005-12-20 00:53:54 +00001822 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman987121a2005-08-23 04:29:48 +00001823 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001824 }
1825 }
Nate Begeman987121a2005-08-23 04:29:48 +00001826
Chris Lattner9dcce6d2006-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 Begeman987121a2005-08-23 04:29:48 +00001841 break;
Andrew Lenharth6ee85662005-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 Lattner1408c052005-12-22 05:23:45 +00001859 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-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 Lenharth835cbb32005-08-29 20:46:51 +00001869 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1870 Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001871 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001872 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001873 }
Nate Begeman987121a2005-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 Lattnerdc750592005-01-07 07:47:09 +00001884 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001885 case ISD::MEMSET:
1886 case ISD::MEMCPY:
1887 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001888 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-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 Lattner4487b2e2005-02-01 18:38:28 +00001895 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001896 break;
1897 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001898 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001899 break;
1900 }
1901 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001902 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001903 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001904
1905 SDOperand Tmp4;
1906 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-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 Lattnera4cfafe2005-01-28 22:29:18 +00001913 case Legal:
1914 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001915 break;
1916 case Promote:
1917 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-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 Lattnera4cfafe2005-01-28 22:29:18 +00001928 Tmp5 = PromoteOp(Node->getOperand(4));
1929 break;
1930 }
Chris Lattner3c0dd462005-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 Lattner9dcce6d2006-01-28 07:39:30 +00001934 case TargetLowering::Custom:
1935 isCustom = true;
1936 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001937 case TargetLowering::Legal:
Chris Lattner85d70c62005-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 Lattner9dcce6d2006-01-28 07:39:30 +00001946 if (isCustom) {
1947 Tmp1 = TLI.LowerOperation(Result, DAG);
1948 if (Tmp1.Val) Result = Tmp1;
1949 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001950 break;
1951 case TargetLowering::Expand: {
Chris Lattner85d70c62005-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 Spencer6dced922005-01-12 14:53:45 +00001958 const char *FnName = 0;
Chris Lattner85d70c62005-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 Lattnerb5a78e02005-05-12 16:53:42 +00001976
Chris Lattner85d70c62005-01-11 05:57:22 +00001977 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001978 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001979 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001980 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001981 break;
1982 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001983 }
1984 break;
1985 }
Chris Lattner5385db52005-05-09 20:23:03 +00001986
1987 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001988 Tmp1 = LegalizeOp(Node->getOperand(0));
1989 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001990
Chris Lattner86535992005-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 Lattner5385db52005-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 Lattner5385db52005-05-09 20:23:03 +00002004 case ISD::WRITEPORT:
Chris Lattner5385db52005-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 Lattnerba45e6c2005-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 Lattner86535992005-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 Lattnerba45e6c2005-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 Lattnerba45e6c2005-05-09 20:36:57 +00002063 break;
2064 }
2065 break;
2066
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002067 case ISD::ADD_PARTS:
Chris Lattner4157c412005-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 Lattnerb3f83b282005-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 Lattner669e8c22005-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 Lattner13fe99c2005-04-02 05:00:07 +00002082
Evan Cheng870e4f82006-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 Lattner9dcce6d2006-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 Cheng870e4f82006-01-09 18:31:59 +00002091 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002092 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002093 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2094 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002095 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002096 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002097 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002098 return RetVal;
2099 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002100 break;
2101 }
2102
Chris Lattner13fe99c2005-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 Lattnerb3f83b282005-01-20 18:52:28 +00002108 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002109
2110 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002111 case ISD::ADD:
2112 case ISD::SUB:
2113 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002114 case ISD::MULHS:
2115 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002116 case ISD::UDIV:
2117 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002118 case ISD::AND:
2119 case ISD::OR:
2120 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002121 case ISD::SHL:
2122 case ISD::SRL:
2123 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002124 case ISD::FADD:
2125 case ISD::FSUB:
2126 case ISD::FMUL:
2127 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002128 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-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 Lattner9dcce6d2006-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 Lenharth72594262005-12-24 23:42:32 +00002142 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-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 Lenharth30db2ec2005-12-25 01:07:37 +00002148 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00002149 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002150 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002151
Nate Begemanbd5f41a2005-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 Lattner9dcce6d2006-01-28 07:39:30 +00002158 case TargetLowering::Promote:
2159 case TargetLowering::Custom:
2160 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-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 Begemanbd5f41a2005-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 Lattner9dcce6d2006-01-28 07:39:30 +00002171 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002172 break;
2173 }
2174 break;
2175 }
2176
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002177 case ISD::UREM:
2178 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002179 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002180 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2181 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002182
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002183 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-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 Begeman20b7d2a2005-04-06 00:23:54 +00002188 case TargetLowering::Legal:
Chris Lattner9dcce6d2006-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 Begeman20b7d2a2005-04-06 00:23:54 +00002196 break;
Chris Lattner81914422005-08-03 20:31:37 +00002197 case TargetLowering::Expand:
2198 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002199 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00002200 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002201 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-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 Lattner10f67752006-01-28 04:28:26 +00002209 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002210 }
2211 break;
2212 }
2213 break;
Nate Begemane74795c2006-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 Lattner364b89a2006-01-28 07:42:08 +00002218 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002219 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2220 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002221 case TargetLowering::Custom:
2222 isCustom = true;
2223 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002224 case TargetLowering::Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002225 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Nate Begemane74795c2006-01-25 18:21:52 +00002226 Result = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2227 else
2228 Result = SDOperand(Node, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002229 Tmp1 = Result.getValue(1);
2230
2231 if (isCustom) {
2232 Tmp2 = TLI.LowerOperation(Result, DAG);
2233 if (Tmp2.Val) {
2234 Result = LegalizeOp(Tmp2);
2235 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2236 }
2237 }
Nate Begemane74795c2006-01-25 18:21:52 +00002238 break;
2239 case TargetLowering::Expand: {
2240 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2241 Node->getOperand(2));
2242 // Increment the pointer, VAList, to the next vaarg
2243 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2244 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2245 TLI.getPointerTy()));
2246 // Store the incremented VAList to the legalized pointer
2247 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2248 Node->getOperand(2));
2249 // Load the actual argument out of the pointer VAList
2250 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002251 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002252 Result = LegalizeOp(Result);
2253 break;
2254 }
2255 }
2256 // Since VAARG produces two values, make sure to remember that we
2257 // legalized both of them.
2258 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002259 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2260 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002261 }
2262
2263 case ISD::VACOPY:
2264 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2265 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2266 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2267
2268 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2269 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002270 case TargetLowering::Custom:
2271 isCustom = true;
2272 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002273 case TargetLowering::Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002274 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Nate Begemane74795c2006-01-25 18:21:52 +00002275 Tmp3 != Node->getOperand(2))
2276 Result = DAG.getNode(ISD::VACOPY, MVT::Other, Tmp1, Tmp2, Tmp3,
2277 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002278 if (isCustom) {
2279 Tmp1 = TLI.LowerOperation(Result, DAG);
2280 if (Tmp1.Val) Result = Tmp1;
2281 }
Nate Begemane74795c2006-01-25 18:21:52 +00002282 break;
2283 case TargetLowering::Expand:
2284 // This defaults to loading a pointer from the input and storing it to the
2285 // output, returning the chain.
2286 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2287 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2288 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00002289 break;
2290 }
2291 break;
2292
2293 case ISD::VAEND:
2294 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2295 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2296
2297 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2298 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002299 case TargetLowering::Custom:
2300 isCustom = true;
2301 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002302 case TargetLowering::Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002303 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Nate Begemane74795c2006-01-25 18:21:52 +00002304 Result = DAG.getNode(ISD::VAEND, MVT::Other, Tmp1, Tmp2,
2305 Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002306 if (isCustom) {
2307 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2308 if (Tmp1.Val) Result = Tmp1;
2309 }
Nate Begemane74795c2006-01-25 18:21:52 +00002310 break;
2311 case TargetLowering::Expand:
2312 Result = Tmp1; // Default to a no-op, return the chain
2313 break;
2314 }
2315 break;
2316
2317 case ISD::VASTART:
2318 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2319 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2320
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002321 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2322 Result = DAG.getNode(ISD::VASTART, MVT::Other, Tmp1, Tmp2,
2323 Node->getOperand(2));
2324
Nate Begemane74795c2006-01-25 18:21:52 +00002325 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2326 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002327 case TargetLowering::Legal: break;
2328 case TargetLowering::Custom:
2329 Tmp1 = TLI.LowerOperation(Result, DAG);
2330 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002331 break;
2332 }
2333 break;
2334
Nate Begeman1b8121b2006-01-11 21:21:00 +00002335 case ISD::ROTL:
2336 case ISD::ROTR:
2337 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2338 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002339
2340 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2341 "Cannot handle this yet!");
2342 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2343 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002344 break;
2345
Nate Begeman2fba8a32006-01-14 03:14:10 +00002346 case ISD::BSWAP:
2347 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2348 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002349 case TargetLowering::Custom:
2350 assert(0 && "Cannot custom legalize this yet!");
2351 case TargetLowering::Legal:
2352 if (Tmp1 != Node->getOperand(0))
2353 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2354 break;
2355 case TargetLowering::Promote: {
2356 MVT::ValueType OVT = Tmp1.getValueType();
2357 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2358 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002359
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002360 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2361 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2362 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2363 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2364 break;
2365 }
2366 case TargetLowering::Expand:
2367 Result = ExpandBSWAP(Tmp1);
2368 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002369 }
2370 break;
2371
Andrew Lenharth5e177822005-05-03 17:19:30 +00002372 case ISD::CTPOP:
2373 case ISD::CTTZ:
2374 case ISD::CTLZ:
2375 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2376 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002377 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002378 case TargetLowering::Legal:
2379 if (Tmp1 != Node->getOperand(0))
2380 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2381 break;
2382 case TargetLowering::Promote: {
2383 MVT::ValueType OVT = Tmp1.getValueType();
2384 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002385
2386 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002387 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2388 // Perform the larger operation, then subtract if needed.
2389 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002390 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002391 case ISD::CTPOP:
2392 Result = Tmp1;
2393 break;
2394 case ISD::CTTZ:
2395 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002396 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2397 DAG.getConstant(getSizeInBits(NVT), NVT),
2398 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002399 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002400 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2401 break;
2402 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002403 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002404 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2405 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002406 getSizeInBits(OVT), NVT));
2407 break;
2408 }
2409 break;
2410 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002411 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002412 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002413 break;
2414 }
2415 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002416
Chris Lattner13fe99c2005-04-02 05:00:07 +00002417 // Unary operators
2418 case ISD::FABS:
2419 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002420 case ISD::FSQRT:
2421 case ISD::FSIN:
2422 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002423 Tmp1 = LegalizeOp(Node->getOperand(0));
2424 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002425 case TargetLowering::Promote:
2426 case TargetLowering::Custom:
2427 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002428 case TargetLowering::Legal:
2429 if (Tmp1 != Node->getOperand(0))
2430 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2431 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002432 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002433 switch (Node->getOpcode()) {
2434 default: assert(0 && "Unreachable!");
2435 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002436 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2437 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002438 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002439 break;
Chris Lattner80026402005-04-30 04:43:14 +00002440 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002441 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2442 MVT::ValueType VT = Node->getValueType(0);
2443 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002444 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002445 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2446 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002447 break;
2448 }
2449 case ISD::FSQRT:
2450 case ISD::FSIN:
2451 case ISD::FCOS: {
2452 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002453 const char *FnName = 0;
2454 switch(Node->getOpcode()) {
2455 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2456 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2457 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2458 default: assert(0 && "Unreachable!");
2459 }
Nate Begeman77558da2005-08-04 21:43:28 +00002460 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002461 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002462 break;
2463 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002464 }
2465 break;
2466 }
2467 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002468
2469 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002470 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002471 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002472 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002473 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2474 Node->getOperand(0).getValueType())) {
2475 default: assert(0 && "Unknown operation action!");
2476 case TargetLowering::Expand:
2477 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2478 break;
2479 case TargetLowering::Legal:
2480 Tmp1 = LegalizeOp(Node->getOperand(0));
2481 if (Tmp1 != Node->getOperand(0))
2482 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Tmp1);
2483 break;
2484 }
2485 }
2486 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002487 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002488 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002489 case ISD::UINT_TO_FP: {
2490 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002491 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2492 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002493 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002494 Node->getOperand(0).getValueType())) {
2495 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002496 case TargetLowering::Custom:
2497 isCustom = true;
2498 // FALLTHROUGH
2499 case TargetLowering::Legal:
2500 Tmp1 = LegalizeOp(Node->getOperand(0));
2501 if (Tmp1 != Node->getOperand(0))
2502 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2503 if (isCustom) {
2504 Tmp1 = TLI.LowerOperation(Result, DAG);
2505 if (Tmp1.Val) Result = Tmp1;
2506 }
2507 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002508 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002509 Result = ExpandLegalINT_TO_FP(isSigned,
2510 LegalizeOp(Node->getOperand(0)),
2511 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002512 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002513 case TargetLowering::Promote:
2514 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2515 Node->getValueType(0),
2516 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002517 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002518 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002519 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002520 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002521 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2522 Node->getValueType(0), Node->getOperand(0));
2523 break;
2524 case Promote:
2525 if (isSigned) {
2526 Result = PromoteOp(Node->getOperand(0));
2527 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2528 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2529 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2530 } else {
2531 Result = PromoteOp(Node->getOperand(0));
2532 Result = DAG.getZeroExtendInReg(Result,
2533 Node->getOperand(0).getValueType());
2534 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002535 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002536 break;
2537 }
2538 break;
2539 }
2540 case ISD::TRUNCATE:
2541 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2542 case Legal:
2543 Tmp1 = LegalizeOp(Node->getOperand(0));
2544 if (Tmp1 != Node->getOperand(0))
2545 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2546 break;
2547 case Expand:
2548 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2549
2550 // Since the result is legal, we should just be able to truncate the low
2551 // part of the source.
2552 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2553 break;
2554 case Promote:
2555 Result = PromoteOp(Node->getOperand(0));
2556 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2557 break;
2558 }
2559 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002560
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002561 case ISD::FP_TO_SINT:
2562 case ISD::FP_TO_UINT:
2563 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2564 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002565 Tmp1 = LegalizeOp(Node->getOperand(0));
2566
Chris Lattner44fe26f2005-07-29 00:11:56 +00002567 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2568 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002569 case TargetLowering::Custom:
2570 isCustom = true;
2571 // FALLTHROUGH
2572 case TargetLowering::Legal:
2573 if (Tmp1 != Node->getOperand(0))
2574 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2575
2576 if (isCustom) {
2577 Tmp1 = TLI.LowerOperation(Result, DAG);
2578 if (Tmp1.Val) Result = Tmp1;
2579 }
2580 break;
2581 case TargetLowering::Promote:
2582 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2583 Node->getOpcode() == ISD::FP_TO_SINT);
2584 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002585 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002586 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2587 SDOperand True, False;
2588 MVT::ValueType VT = Node->getOperand(0).getValueType();
2589 MVT::ValueType NVT = Node->getValueType(0);
2590 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2591 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2592 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2593 Node->getOperand(0), Tmp2, ISD::SETLT);
2594 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2595 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002596 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002597 Tmp2));
2598 False = DAG.getNode(ISD::XOR, NVT, False,
2599 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002600 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2601 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002602 } else {
2603 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2604 }
2605 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002606 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002607 break;
2608 case Expand:
2609 assert(0 && "Shouldn't need to expand other operators here!");
2610 case Promote:
2611 Result = PromoteOp(Node->getOperand(0));
2612 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2613 break;
2614 }
2615 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002616
Chris Lattner7753f172005-09-02 00:18:10 +00002617 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002618 case ISD::ZERO_EXTEND:
2619 case ISD::SIGN_EXTEND:
2620 case ISD::FP_EXTEND:
2621 case ISD::FP_ROUND:
2622 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002623 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002624 case Legal:
2625 Tmp1 = LegalizeOp(Node->getOperand(0));
2626 if (Tmp1 != Node->getOperand(0))
2627 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2628 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002629 case Promote:
2630 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002631 case ISD::ANY_EXTEND:
2632 Result = PromoteOp(Node->getOperand(0));
2633 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2634 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002635 case ISD::ZERO_EXTEND:
2636 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002637 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002638 Result = DAG.getZeroExtendInReg(Result,
2639 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002640 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002641 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002642 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002643 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002644 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002645 Result,
2646 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002647 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002648 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002649 Result = PromoteOp(Node->getOperand(0));
2650 if (Result.getValueType() != Op.getValueType())
2651 // Dynamically dead while we have only 2 FP types.
2652 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2653 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002654 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002655 Result = PromoteOp(Node->getOperand(0));
2656 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2657 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002658 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002659 }
2660 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002661 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002662 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002663 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002664 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002665
2666 // If this operation is not supported, convert it to a shl/shr or load/store
2667 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002668 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2669 default: assert(0 && "This action not supported for this op yet!");
2670 case TargetLowering::Legal:
2671 if (Tmp1 != Node->getOperand(0))
2672 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002673 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002674 break;
2675 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002676 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002677 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002678 // NOTE: we could fall back on load/store here too for targets without
2679 // SAR. However, it is doubtful that any exist.
2680 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2681 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002682 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002683 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2684 Node->getOperand(0), ShiftCst);
2685 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2686 Result, ShiftCst);
2687 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2688 // The only way we can lower this is to turn it into a STORETRUNC,
2689 // EXTLOAD pair, targetting a temporary location (a stack slot).
2690
2691 // NOTE: there is a choice here between constantly creating new stack
2692 // slots and always reusing the same one. We currently always create
2693 // new ones, as reuse may inhibit scheduling.
2694 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2695 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2696 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2697 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002698 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002699 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2700 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2701 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002702 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002703 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002704 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2705 Result, StackSlot, DAG.getSrcValue(NULL),
2706 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002707 } else {
2708 assert(0 && "Unknown op");
2709 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002710 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002711 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002712 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002713 }
Chris Lattner99222f72005-01-15 07:15:18 +00002714 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002715
2716 // Make sure that the generated code is itself legal.
2717 if (Result != Op)
2718 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002719
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002720 // Note that LegalizeOp may be reentered even from single-use nodes, which
2721 // means that we always must cache transformed nodes.
2722 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002723 return Result;
2724}
2725
Chris Lattner4d978642005-01-15 22:16:26 +00002726/// PromoteOp - Given an operation that produces a value in an invalid type,
2727/// promote it to compute the value into a larger type. The produced value will
2728/// have the correct bits for the low portion of the register, but no guarantee
2729/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002730SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2731 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002732 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002733 assert(getTypeAction(VT) == Promote &&
2734 "Caller should expand or legalize operands that are not promotable!");
2735 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2736 "Cannot promote to smaller type!");
2737
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002738 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002739 SDOperand Result;
2740 SDNode *Node = Op.Val;
2741
Chris Lattner1a570f12005-09-02 20:32:45 +00002742 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2743 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002744
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002745 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002746 case ISD::CopyFromReg:
2747 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002748 default:
2749 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2750 assert(0 && "Do not know how to promote this operator!");
2751 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002752 case ISD::UNDEF:
2753 Result = DAG.getNode(ISD::UNDEF, NVT);
2754 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002755 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002756 if (VT != MVT::i1)
2757 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2758 else
2759 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002760 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2761 break;
2762 case ISD::ConstantFP:
2763 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2764 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2765 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002766
Chris Lattner2cb338d2005-01-18 02:59:52 +00002767 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002768 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002769 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2770 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002771 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002772
2773 case ISD::TRUNCATE:
2774 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2775 case Legal:
2776 Result = LegalizeOp(Node->getOperand(0));
2777 assert(Result.getValueType() >= NVT &&
2778 "This truncation doesn't make sense!");
2779 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2780 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2781 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002782 case Promote:
2783 // The truncation is not required, because we don't guarantee anything
2784 // about high bits anyway.
2785 Result = PromoteOp(Node->getOperand(0));
2786 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002787 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002788 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2789 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002790 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002791 }
2792 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002793 case ISD::SIGN_EXTEND:
2794 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002795 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002796 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2797 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2798 case Legal:
2799 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002800 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002801 break;
2802 case Promote:
2803 // Promote the reg if it's smaller.
2804 Result = PromoteOp(Node->getOperand(0));
2805 // The high bits are not guaranteed to be anything. Insert an extend.
2806 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002807 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002808 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002809 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002810 Result = DAG.getZeroExtendInReg(Result,
2811 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002812 break;
2813 }
2814 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002815 case ISD::BIT_CONVERT:
2816 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2817 Result = PromoteOp(Result);
2818 break;
2819
Chris Lattner4d978642005-01-15 22:16:26 +00002820 case ISD::FP_EXTEND:
2821 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2822 case ISD::FP_ROUND:
2823 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2824 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2825 case Promote: assert(0 && "Unreachable with 2 FP types!");
2826 case Legal:
2827 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002828 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002829 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002830 break;
2831 }
2832 break;
2833
2834 case ISD::SINT_TO_FP:
2835 case ISD::UINT_TO_FP:
2836 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2837 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002838 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002839 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002840 break;
2841
2842 case Promote:
2843 Result = PromoteOp(Node->getOperand(0));
2844 if (Node->getOpcode() == ISD::SINT_TO_FP)
2845 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002846 Result,
2847 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002848 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002849 Result = DAG.getZeroExtendInReg(Result,
2850 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002851 // No extra round required here.
2852 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002853 break;
2854 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002855 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2856 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002857 // Round if we cannot tolerate excess precision.
2858 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002859 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2860 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002861 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002862 }
Chris Lattner4d978642005-01-15 22:16:26 +00002863 break;
2864
Chris Lattner268d4572005-12-09 17:32:47 +00002865 case ISD::SIGN_EXTEND_INREG:
2866 Result = PromoteOp(Node->getOperand(0));
2867 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2868 Node->getOperand(1));
2869 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002870 case ISD::FP_TO_SINT:
2871 case ISD::FP_TO_UINT:
2872 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2873 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002874 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002875 break;
2876 case Promote:
2877 // The input result is prerounded, so we don't have to do anything
2878 // special.
2879 Tmp1 = PromoteOp(Node->getOperand(0));
2880 break;
2881 case Expand:
2882 assert(0 && "not implemented");
2883 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002884 // If we're promoting a UINT to a larger size, check to see if the new node
2885 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2886 // we can use that instead. This allows us to generate better code for
2887 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2888 // legal, such as PowerPC.
2889 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002890 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002891 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2892 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002893 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2894 } else {
2895 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2896 }
Chris Lattner4d978642005-01-15 22:16:26 +00002897 break;
2898
Chris Lattner13fe99c2005-04-02 05:00:07 +00002899 case ISD::FABS:
2900 case ISD::FNEG:
2901 Tmp1 = PromoteOp(Node->getOperand(0));
2902 assert(Tmp1.getValueType() == NVT);
2903 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2904 // NOTE: we do not have to do any extra rounding here for
2905 // NoExcessFPPrecision, because we know the input will have the appropriate
2906 // precision, and these operations don't modify precision at all.
2907 break;
2908
Chris Lattner9d6fa982005-04-28 21:44:33 +00002909 case ISD::FSQRT:
2910 case ISD::FSIN:
2911 case ISD::FCOS:
2912 Tmp1 = PromoteOp(Node->getOperand(0));
2913 assert(Tmp1.getValueType() == NVT);
2914 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002915 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002916 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2917 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002918 break;
2919
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002920 case ISD::AND:
2921 case ISD::OR:
2922 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002923 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002924 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002925 case ISD::MUL:
2926 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002927 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002928 // that too is okay if they are integer operations.
2929 Tmp1 = PromoteOp(Node->getOperand(0));
2930 Tmp2 = PromoteOp(Node->getOperand(1));
2931 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2932 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002933 break;
2934 case ISD::FADD:
2935 case ISD::FSUB:
2936 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002937 Tmp1 = PromoteOp(Node->getOperand(0));
2938 Tmp2 = PromoteOp(Node->getOperand(1));
2939 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2940 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2941
2942 // Floating point operations will give excess precision that we may not be
2943 // able to tolerate. If we DO allow excess precision, just leave it,
2944 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002945 // FIXME: Why would we need to round FP ops more than integer ones?
2946 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002947 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002948 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2949 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002950 break;
2951
Chris Lattner4d978642005-01-15 22:16:26 +00002952 case ISD::SDIV:
2953 case ISD::SREM:
2954 // These operators require that their input be sign extended.
2955 Tmp1 = PromoteOp(Node->getOperand(0));
2956 Tmp2 = PromoteOp(Node->getOperand(1));
2957 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002958 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2959 DAG.getValueType(VT));
2960 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2961 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002962 }
2963 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2964
2965 // Perform FP_ROUND: this is probably overly pessimistic.
2966 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002967 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2968 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002969 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002970 case ISD::FDIV:
2971 case ISD::FREM:
2972 // These operators require that their input be fp extended.
2973 Tmp1 = PromoteOp(Node->getOperand(0));
2974 Tmp2 = PromoteOp(Node->getOperand(1));
2975 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2976
2977 // Perform FP_ROUND: this is probably overly pessimistic.
2978 if (NoExcessFPPrecision)
2979 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2980 DAG.getValueType(VT));
2981 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002982
2983 case ISD::UDIV:
2984 case ISD::UREM:
2985 // These operators require that their input be zero extended.
2986 Tmp1 = PromoteOp(Node->getOperand(0));
2987 Tmp2 = PromoteOp(Node->getOperand(1));
2988 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002989 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2990 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002991 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2992 break;
2993
2994 case ISD::SHL:
2995 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002996 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002997 break;
2998 case ISD::SRA:
2999 // The input value must be properly sign extended.
3000 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003001 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3002 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003003 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003004 break;
3005 case ISD::SRL:
3006 // The input value must be properly zero extended.
3007 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003008 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003009 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003010 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003011
3012 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003013 Tmp1 = Node->getOperand(0); // Get the chain.
3014 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003015 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3016 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3017 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3018 } else {
3019 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
3020 Node->getOperand(2));
3021 // Increment the pointer, VAList, to the next vaarg
3022 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3023 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3024 TLI.getPointerTy()));
3025 // Store the incremented VAList to the legalized pointer
3026 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
3027 Node->getOperand(2));
3028 // Load the actual argument out of the pointer VAList
3029 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
3030 DAG.getSrcValue(0), VT);
3031 }
3032 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003033 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003034 break;
3035
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003036 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003037 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
3038 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003039 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003040 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003041 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00003042 case ISD::SEXTLOAD:
3043 case ISD::ZEXTLOAD:
3044 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003045 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
3046 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00003047 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00003048 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003049 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00003050 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003051 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003052 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3053 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003054 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003055 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003056 case ISD::SELECT_CC:
3057 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3058 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3059 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003060 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003061 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003062 case ISD::BSWAP:
3063 Tmp1 = Node->getOperand(0);
3064 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3065 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3066 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3067 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3068 TLI.getShiftAmountTy()));
3069 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003070 case ISD::CTPOP:
3071 case ISD::CTTZ:
3072 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003073 // Zero extend the argument
3074 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003075 // Perform the larger operation, then subtract if needed.
3076 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003077 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003078 case ISD::CTPOP:
3079 Result = Tmp1;
3080 break;
3081 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003082 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003083 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00003084 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003085 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003086 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003087 break;
3088 case ISD::CTLZ:
3089 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003090 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3091 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003092 getSizeInBits(VT), NVT));
3093 break;
3094 }
3095 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003096 }
3097
3098 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003099
3100 // Make sure the result is itself legal.
3101 Result = LegalizeOp(Result);
3102
3103 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003104 AddPromotedOperand(Op, Result);
3105 return Result;
3106}
Chris Lattnerdc750592005-01-07 07:47:09 +00003107
Chris Lattner36e663d2005-12-23 00:16:34 +00003108/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003109/// The resultant code need not be legal. Note that SrcOp is the input operand
3110/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003111SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3112 SDOperand SrcOp) {
3113 // Create the stack frame object.
3114 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3115 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003116 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner36e663d2005-12-23 00:16:34 +00003117 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3118
3119 // Emit a store to the stack slot.
3120 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00003121 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00003122 // Result is a load from the stack slot.
3123 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3124}
3125
Chris Lattner4157c412005-04-02 04:00:59 +00003126void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3127 SDOperand Op, SDOperand Amt,
3128 SDOperand &Lo, SDOperand &Hi) {
3129 // Expand the subcomponents.
3130 SDOperand LHSL, LHSH;
3131 ExpandOp(Op, LHSL, LHSH);
3132
3133 std::vector<SDOperand> Ops;
3134 Ops.push_back(LHSL);
3135 Ops.push_back(LHSH);
3136 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00003137 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00003138 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00003139 Hi = Lo.getValue(1);
3140}
3141
3142
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003143/// ExpandShift - Try to find a clever way to expand this shift operation out to
3144/// smaller elements. If we can't find a way that is more efficient than a
3145/// libcall on this target, return false. Otherwise, return true with the
3146/// low-parts expanded into Lo and Hi.
3147bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3148 SDOperand &Lo, SDOperand &Hi) {
3149 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3150 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003151
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003152 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003153 SDOperand ShAmt = LegalizeOp(Amt);
3154 MVT::ValueType ShTy = ShAmt.getValueType();
3155 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3156 unsigned NVTBits = MVT::getSizeInBits(NVT);
3157
3158 // Handle the case when Amt is an immediate. Other cases are currently broken
3159 // and are disabled.
3160 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3161 unsigned Cst = CN->getValue();
3162 // Expand the incoming operand to be shifted, so that we have its parts
3163 SDOperand InL, InH;
3164 ExpandOp(Op, InL, InH);
3165 switch(Opc) {
3166 case ISD::SHL:
3167 if (Cst > VTBits) {
3168 Lo = DAG.getConstant(0, NVT);
3169 Hi = DAG.getConstant(0, NVT);
3170 } else if (Cst > NVTBits) {
3171 Lo = DAG.getConstant(0, NVT);
3172 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003173 } else if (Cst == NVTBits) {
3174 Lo = DAG.getConstant(0, NVT);
3175 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00003176 } else {
3177 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3178 Hi = DAG.getNode(ISD::OR, NVT,
3179 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3180 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3181 }
3182 return true;
3183 case ISD::SRL:
3184 if (Cst > VTBits) {
3185 Lo = DAG.getConstant(0, NVT);
3186 Hi = DAG.getConstant(0, NVT);
3187 } else if (Cst > NVTBits) {
3188 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3189 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003190 } else if (Cst == NVTBits) {
3191 Lo = InH;
3192 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003193 } else {
3194 Lo = DAG.getNode(ISD::OR, NVT,
3195 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3196 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3197 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3198 }
3199 return true;
3200 case ISD::SRA:
3201 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003202 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003203 DAG.getConstant(NVTBits-1, ShTy));
3204 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003205 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003206 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003207 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003208 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003209 } else if (Cst == NVTBits) {
3210 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003211 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003212 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003213 } else {
3214 Lo = DAG.getNode(ISD::OR, NVT,
3215 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3216 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3217 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3218 }
3219 return true;
3220 }
3221 }
Nate Begemanb0674922005-04-06 21:13:14 +00003222 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003223}
Chris Lattneraac464e2005-01-21 06:05:23 +00003224
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003225/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
3226/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00003227/// Found.
Chris Lattner90ba5442006-01-09 23:21:49 +00003228static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found,
3229 std::set<SDNode*> &Visited) {
Chris Lattner15afe462006-01-20 18:40:10 +00003230 if (Node->getNodeDepth() <= Found->getNodeDepth() ||
Chris Lattner90ba5442006-01-09 23:21:49 +00003231 !Visited.insert(Node).second) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00003232
Chris Lattner2dce7032005-05-12 23:24:06 +00003233 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00003234 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00003235 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003236 Found = Node;
3237 return;
3238 }
3239
3240 // Otherwise, scan the operands of Node to see if any of them is a call.
3241 assert(Node->getNumOperands() != 0 &&
3242 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00003243 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner90ba5442006-01-09 23:21:49 +00003244 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003245
3246 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003247 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner90ba5442006-01-09 23:21:49 +00003248 Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003249}
3250
3251
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003252/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
3253/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00003254/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00003255static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
3256 std::set<SDNode*> &Visited) {
Chris Lattner15afe462006-01-20 18:40:10 +00003257 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
Chris Lattner96ad3132005-08-05 18:10:27 +00003258 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00003259
Chris Lattner2dce7032005-05-12 23:24:06 +00003260 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00003261 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00003262 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003263 Found = Node;
3264 return;
3265 }
3266
3267 // Otherwise, scan the operands of Node to see if any of them is a call.
3268 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
3269 if (UI == E) return;
3270 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00003271 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003272
3273 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00003274 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003275}
3276
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003277/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003278/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003279static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00003280 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00003281 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00003282 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003283 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00003284
Chris Lattner02011c92006-01-14 22:41:46 +00003285 // The chain is usually at the end.
Chris Lattner4add7e32005-01-23 04:42:50 +00003286 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner02011c92006-01-14 22:41:46 +00003287 if (TheChain.getValueType() != MVT::Other) {
3288 // Sometimes it's at the beginning.
Chris Lattner3268f242005-05-14 08:34:53 +00003289 TheChain = SDOperand(Node, 0);
Chris Lattner02011c92006-01-14 22:41:46 +00003290 if (TheChain.getValueType() != MVT::Other) {
3291 // Otherwise, hunt for it.
3292 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
3293 if (Node->getValueType(i) == MVT::Other) {
3294 TheChain = SDOperand(Node, i);
3295 break;
3296 }
3297
3298 // Otherwise, we walked into a node without a chain.
3299 if (TheChain.getValueType() != MVT::Other)
3300 return 0;
3301 }
3302 }
Misha Brukman835702a2005-04-21 22:36:52 +00003303
3304 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00003305 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00003306
Chris Lattner4add7e32005-01-23 04:42:50 +00003307 // Make sure to only follow users of our token chain.
3308 SDNode *User = *UI;
3309 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3310 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00003311 if (SDNode *Result = FindCallSeqEnd(User))
3312 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00003313 }
Chris Lattnercabdc342005-08-05 16:23:57 +00003314 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00003315}
3316
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003317/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003318/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003319static SDNode *FindCallSeqStart(SDNode *Node) {
3320 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00003321 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003322
3323 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3324 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003325 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003326}
3327
3328
Chris Lattner4add7e32005-01-23 04:42:50 +00003329/// FindInputOutputChains - If we are replacing an operation with a call we need
3330/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00003331/// properly serialize the calls in the block. The returned operand is the
3332/// input chain value for the new call (e.g. the entry node or the previous
3333/// call), and OutChain is set to be the chain node to update to point to the
3334/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00003335static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3336 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003337 SDNode *LatestCallSeqStart = Entry.Val;
3338 SDNode *LatestCallSeqEnd = 0;
Chris Lattner90ba5442006-01-09 23:21:49 +00003339 std::set<SDNode*> Visited;
3340 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited);
3341 Visited.clear();
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003342 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00003343
Chris Lattner2dce7032005-05-12 23:24:06 +00003344 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00003345 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00003346 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3347 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00003348 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003349 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00003350 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3351 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003352 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00003353 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003354 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00003355
Chris Lattner06bbeb62005-05-11 19:02:11 +00003356 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003357 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003358 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00003359 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner90ba5442006-01-09 23:21:49 +00003360 Visited.clear();
Chris Lattner4add7e32005-01-23 04:42:50 +00003361
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003362 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003363 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003364 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00003365
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003366 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00003367}
3368
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003369/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00003370void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3371 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00003372 // Nothing to splice it into?
3373 if (OutChain == 0) return;
3374
3375 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3376 //OutChain->dump();
3377
3378 // Form a token factor node merging the old inval and the new inval.
3379 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3380 OutChain->getOperand(0));
3381 // Change the node to refer to the new token.
3382 OutChain->setAdjCallChain(InToken);
3383}
Chris Lattner4add7e32005-01-23 04:42:50 +00003384
3385
Chris Lattneraac464e2005-01-21 06:05:23 +00003386// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3387// does not fit into a register, return the lo part and set the hi part to the
3388// by-reg argument. If it does fit into a single register, return the result
3389// and leave the Hi part unset.
3390SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3391 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003392 SDNode *OutChain;
3393 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3394 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00003395 if (InChain.Val == 0)
3396 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00003397
Chris Lattneraac464e2005-01-21 06:05:23 +00003398 TargetLowering::ArgListTy Args;
3399 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3400 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3401 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3402 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3403 }
3404 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003405
Chris Lattner06bbeb62005-05-11 19:02:11 +00003406 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003407 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003408 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003409 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3410 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003411
Chris Lattner63022662005-09-02 20:26:58 +00003412 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003413 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003414 default: assert(0 && "Unknown thing");
3415 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003416 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003417 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003418 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003419 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003420 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003421 }
Chris Lattner10f67752006-01-28 04:28:26 +00003422
3423 CallInfo.second = LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003424 SpliceCallInto(CallInfo.second, OutChain);
Chris Lattner63022662005-09-02 20:26:58 +00003425 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003426}
3427
Chris Lattner4add7e32005-01-23 04:42:50 +00003428
Chris Lattneraac464e2005-01-21 06:05:23 +00003429/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3430/// destination type is legal.
3431SDOperand SelectionDAGLegalize::
3432ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003433 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003434 assert(getTypeAction(Source.getValueType()) == Expand &&
3435 "This is not an expansion!");
3436 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3437
Chris Lattner06bbeb62005-05-11 19:02:11 +00003438 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003439 assert(Source.getValueType() == MVT::i64 &&
3440 "This only works for 64-bit -> FP");
3441 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3442 // incoming integer is set. To handle this, we dynamically test to see if
3443 // it is set, and, if so, add a fudge factor.
3444 SDOperand Lo, Hi;
3445 ExpandOp(Source, Lo, Hi);
3446
Chris Lattner2a4f7312005-05-13 04:45:13 +00003447 // If this is unsigned, and not supported, first perform the conversion to
3448 // signed, then adjust the result if the sign bit is set.
3449 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3450 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3451
Chris Lattnerd47675e2005-08-09 20:20:18 +00003452 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3453 DAG.getConstant(0, Hi.getValueType()),
3454 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003455 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3456 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3457 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003458 uint64_t FF = 0x5f800000ULL;
3459 if (TLI.isLittleEndian()) FF <<= 32;
3460 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003461
Chris Lattnerc30405e2005-08-26 17:15:30 +00003462 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003463 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3464 SDOperand FudgeInReg;
3465 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003466 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3467 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003468 else {
3469 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003470 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3471 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003472 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003473 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003474 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003475
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003476 // Check to see if the target has a custom way to lower this. If so, use it.
3477 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3478 default: assert(0 && "This action not implemented for this operation!");
3479 case TargetLowering::Legal:
3480 case TargetLowering::Expand:
3481 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003482 case TargetLowering::Custom: {
3483 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3484 Source), DAG);
3485 if (NV.Val)
3486 return LegalizeOp(NV);
3487 break; // The target decided this was legal after all
3488 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003489 }
3490
Chris Lattner153587e2005-05-12 07:00:44 +00003491 // Expand the source, then glue it back together for the call. We must expand
3492 // the source in case it is shared (this pass of legalize must traverse it).
3493 SDOperand SrcLo, SrcHi;
3494 ExpandOp(Source, SrcLo, SrcHi);
3495 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3496
Chris Lattner06bbeb62005-05-11 19:02:11 +00003497 SDNode *OutChain = 0;
3498 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3499 DAG.getEntryNode());
3500 const char *FnName = 0;
3501 if (DestTy == MVT::f32)
3502 FnName = "__floatdisf";
3503 else {
3504 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3505 FnName = "__floatdidf";
3506 }
3507
Chris Lattneraac464e2005-01-21 06:05:23 +00003508 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3509
3510 TargetLowering::ArgListTy Args;
3511 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003512
Chris Lattneraac464e2005-01-21 06:05:23 +00003513 Args.push_back(std::make_pair(Source, ArgTy));
3514
3515 // We don't care about token chains for libcalls. We just use the entry
3516 // node as our input and ignore the output chain. This allows us to place
3517 // calls wherever we need them to satisfy data dependences.
3518 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003519
3520 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003521 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3522 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003523
Chris Lattnera5bf1032005-05-12 04:49:08 +00003524 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003525 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003526}
Misha Brukman835702a2005-04-21 22:36:52 +00003527
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003528
3529
Chris Lattnerdc750592005-01-07 07:47:09 +00003530/// ExpandOp - Expand the specified SDOperand into its two component pieces
3531/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3532/// LegalizeNodes map is filled in for any results that are not expanded, the
3533/// ExpandedNodes map is filled in for any results that are expanded, and the
3534/// Lo/Hi values are returned.
3535void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3536 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003537 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003538 SDNode *Node = Op.Val;
3539 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003540 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3541 "Cannot expand FP values!");
3542 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003543 "Cannot expand to FP value or to larger int value!");
3544
Chris Lattner1a570f12005-09-02 20:32:45 +00003545 // See if we already expanded it.
3546 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3547 = ExpandedNodes.find(Op);
3548 if (I != ExpandedNodes.end()) {
3549 Lo = I->second.first;
3550 Hi = I->second.second;
3551 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003552 }
3553
Chris Lattnerdc750592005-01-07 07:47:09 +00003554 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003555 case ISD::CopyFromReg:
3556 assert(0 && "CopyFromReg must be legal!");
3557 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003558 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3559 assert(0 && "Do not know how to expand this operator!");
3560 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003561 case ISD::UNDEF:
3562 Lo = DAG.getNode(ISD::UNDEF, NVT);
3563 Hi = DAG.getNode(ISD::UNDEF, NVT);
3564 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003565 case ISD::Constant: {
3566 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3567 Lo = DAG.getConstant(Cst, NVT);
3568 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3569 break;
3570 }
Nate Begemanae89d862005-12-07 19:48:11 +00003571 case ISD::ConstantVec: {
3572 unsigned NumElements = Node->getNumOperands();
3573 // If we only have two elements left in the constant vector, just break it
3574 // apart into the two scalar constants it contains. Otherwise, bisect the
3575 // ConstantVec, and return each half as a new ConstantVec.
3576 // FIXME: this is hard coded as big endian, it may have to change to support
3577 // SSE and Alpha MVI
3578 if (NumElements == 2) {
3579 Hi = Node->getOperand(0);
3580 Lo = Node->getOperand(1);
3581 } else {
3582 NumElements /= 2;
3583 std::vector<SDOperand> LoOps, HiOps;
3584 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3585 HiOps.push_back(Node->getOperand(I));
3586 LoOps.push_back(Node->getOperand(I+NumElements));
3587 }
3588 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3589 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3590 }
3591 break;
3592 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003593
Chris Lattner32e08b72005-03-28 22:03:13 +00003594 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003595 // Return the operands.
3596 Lo = Node->getOperand(0);
3597 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003598 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003599
3600 case ISD::SIGN_EXTEND_INREG:
3601 ExpandOp(Node->getOperand(0), Lo, Hi);
3602 // Sign extend the lo-part.
3603 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3604 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3605 TLI.getShiftAmountTy()));
3606 // sext_inreg the low part if needed.
3607 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3608 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003609
Nate Begeman2fba8a32006-01-14 03:14:10 +00003610 case ISD::BSWAP: {
3611 ExpandOp(Node->getOperand(0), Lo, Hi);
3612 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3613 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3614 Lo = TempLo;
3615 break;
3616 }
3617
Chris Lattner55e9cde2005-05-11 04:51:16 +00003618 case ISD::CTPOP:
3619 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003620 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3621 DAG.getNode(ISD::CTPOP, NVT, Lo),
3622 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003623 Hi = DAG.getConstant(0, NVT);
3624 break;
3625
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003626 case ISD::CTLZ: {
3627 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003628 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003629 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3630 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003631 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3632 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003633 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3634 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3635
3636 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3637 Hi = DAG.getConstant(0, NVT);
3638 break;
3639 }
3640
3641 case ISD::CTTZ: {
3642 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003643 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003644 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3645 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003646 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3647 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003648 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3649 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3650
3651 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3652 Hi = DAG.getConstant(0, NVT);
3653 break;
3654 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003655
Nate Begemane74795c2006-01-25 18:21:52 +00003656 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003657 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3658 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003659 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3660 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3661
3662 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003663 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003664 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3665 if (!TLI.isLittleEndian())
3666 std::swap(Lo, Hi);
3667 break;
3668 }
3669
Chris Lattnerdc750592005-01-07 07:47:09 +00003670 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003671 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3672 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003673 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003674
3675 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003676 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003677 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3678 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003679 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003680 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003681
3682 // Build a factor node to remember that this load is independent of the
3683 // other one.
3684 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3685 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003686
Chris Lattnerdc750592005-01-07 07:47:09 +00003687 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003688 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00003689 if (!TLI.isLittleEndian())
3690 std::swap(Lo, Hi);
3691 break;
3692 }
Nate Begemand37c1312005-11-22 18:16:00 +00003693 case ISD::VLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003694 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3695 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemand37c1312005-11-22 18:16:00 +00003696 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3697 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3698
3699 // If we only have two elements, turn into a pair of scalar loads.
3700 // FIXME: handle case where a vector of two elements is fine, such as
3701 // 2 x double on SSE2.
3702 if (NumElements == 2) {
3703 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3704 // Increment the pointer to the other half.
3705 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3706 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3707 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003708 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003709 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3710 } else {
3711 NumElements /= 2; // Split the vector in half
3712 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3713 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3714 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3715 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003716 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003717 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3718 }
3719
3720 // Build a factor node to remember that this load is independent of the
3721 // other one.
3722 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3723 Hi.getValue(1));
3724
3725 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003726 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemand37c1312005-11-22 18:16:00 +00003727 if (!TLI.isLittleEndian())
3728 std::swap(Lo, Hi);
3729 break;
3730 }
3731 case ISD::VADD:
3732 case ISD::VSUB:
3733 case ISD::VMUL: {
3734 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3735 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3736 SDOperand LL, LH, RL, RH;
3737
3738 ExpandOp(Node->getOperand(0), LL, LH);
3739 ExpandOp(Node->getOperand(1), RL, RH);
3740
3741 // If we only have two elements, turn into a pair of scalar loads.
3742 // FIXME: handle case where a vector of two elements is fine, such as
3743 // 2 x double on SSE2.
3744 if (NumElements == 2) {
3745 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3746 Lo = DAG.getNode(Opc, EVT, LL, RL);
3747 Hi = DAG.getNode(Opc, EVT, LH, RH);
3748 } else {
3749 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3750 LL.getOperand(3));
3751 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3752 LH.getOperand(3));
3753 }
3754 break;
3755 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003756 case ISD::AND:
3757 case ISD::OR:
3758 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3759 SDOperand LL, LH, RL, RH;
3760 ExpandOp(Node->getOperand(0), LL, LH);
3761 ExpandOp(Node->getOperand(1), RL, RH);
3762 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3763 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3764 break;
3765 }
3766 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003767 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00003768 ExpandOp(Node->getOperand(1), LL, LH);
3769 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003770 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3771 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00003772 break;
3773 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003774 case ISD::SELECT_CC: {
3775 SDOperand TL, TH, FL, FH;
3776 ExpandOp(Node->getOperand(2), TL, TH);
3777 ExpandOp(Node->getOperand(3), FL, FH);
3778 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3779 Node->getOperand(1), TL, FL, Node->getOperand(4));
3780 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3781 Node->getOperand(1), TH, FH, Node->getOperand(4));
3782 break;
3783 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003784 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003785 SDOperand Chain = Node->getOperand(0);
3786 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003787 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3788
3789 if (EVT == NVT)
3790 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3791 else
3792 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3793 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003794
3795 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003796 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003797
Nate Begemanc3a89c52005-10-13 17:15:37 +00003798 // The high part is obtained by SRA'ing all but one of the bits of the lo
3799 // part.
3800 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3801 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3802 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003803 break;
3804 }
3805 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003806 SDOperand Chain = Node->getOperand(0);
3807 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003808 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3809
3810 if (EVT == NVT)
3811 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3812 else
3813 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3814 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003815
3816 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003817 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003818
Nate Begemanc3a89c52005-10-13 17:15:37 +00003819 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003820 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003821 break;
3822 }
3823 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003824 SDOperand Chain = Node->getOperand(0);
3825 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00003826 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3827
3828 if (EVT == NVT)
3829 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3830 else
3831 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3832 EVT);
3833
3834 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003835 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003836
3837 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00003838 Hi = DAG.getNode(ISD::UNDEF, NVT);
3839 break;
3840 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003841 case ISD::ANY_EXTEND:
3842 // The low part is any extension of the input (which degenerates to a copy).
3843 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3844 // The high part is undefined.
3845 Hi = DAG.getNode(ISD::UNDEF, NVT);
3846 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003847 case ISD::SIGN_EXTEND: {
3848 // The low part is just a sign extension of the input (which degenerates to
3849 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003850 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003851
Chris Lattnerdc750592005-01-07 07:47:09 +00003852 // The high part is obtained by SRA'ing all but one of the bits of the lo
3853 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003854 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003855 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3856 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003857 break;
3858 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003859 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00003860 // The low part is just a zero extension of the input (which degenerates to
3861 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003862 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003863
Chris Lattnerdc750592005-01-07 07:47:09 +00003864 // The high part is just a zero.
3865 Hi = DAG.getConstant(0, NVT);
3866 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003867
3868 case ISD::BIT_CONVERT: {
3869 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3870 Node->getOperand(0));
3871 ExpandOp(Tmp, Lo, Hi);
3872 break;
3873 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003874
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003875 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00003876 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3877 TargetLowering::Custom &&
3878 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003879 Lo = TLI.LowerOperation(Op, DAG);
3880 assert(Lo.Val && "Node must be custom expanded!");
3881 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00003882 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003883 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003884 break;
3885
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003886 // These operators cannot be expanded directly, emit them as calls to
3887 // library functions.
3888 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003889 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003890 SDOperand Op;
3891 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3892 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003893 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3894 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00003895 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003896
Chris Lattner941d84a2005-07-30 01:40:57 +00003897 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3898
Chris Lattnerfe68d752005-07-29 00:33:32 +00003899 // Now that the custom expander is done, expand the result, which is still
3900 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003901 if (Op.Val) {
3902 ExpandOp(Op, Lo, Hi);
3903 break;
3904 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003905 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003906
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003907 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003908 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003909 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003910 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003911 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003912
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003913 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003914 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003915 SDOperand Op;
3916 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3917 case Expand: assert(0 && "cannot expand FP!");
3918 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3919 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3920 }
3921
3922 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3923
3924 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003925 if (Op.Val) {
3926 ExpandOp(Op, Lo, Hi);
3927 break;
3928 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003929 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003930
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003931 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003932 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003933 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003934 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003935 break;
3936
Evan Cheng870e4f82006-01-09 18:31:59 +00003937 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003938 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003939 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003940 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003941 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003942 Op = TLI.LowerOperation(Op, DAG);
3943 if (Op.Val) {
3944 // Now that the custom expander is done, expand the result, which is
3945 // still VT.
3946 ExpandOp(Op, Lo, Hi);
3947 break;
3948 }
3949 }
3950
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003951 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003952 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003953 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003954
3955 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003956 TargetLowering::LegalizeAction Action =
3957 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3958 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3959 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003960 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003961 break;
3962 }
3963
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003964 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003965 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003966 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003967 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003968
Evan Cheng870e4f82006-01-09 18:31:59 +00003969 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003970 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003971 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003972 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003973 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003974 Op = TLI.LowerOperation(Op, DAG);
3975 if (Op.Val) {
3976 // Now that the custom expander is done, expand the result, which is
3977 // still VT.
3978 ExpandOp(Op, Lo, Hi);
3979 break;
3980 }
3981 }
3982
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003983 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003984 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003985 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003986
3987 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003988 TargetLowering::LegalizeAction Action =
3989 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3990 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3991 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003992 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003993 break;
3994 }
3995
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003996 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003997 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003998 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003999 }
4000
4001 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004002 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004003 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004004 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004005 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004006 Op = TLI.LowerOperation(Op, DAG);
4007 if (Op.Val) {
4008 // Now that the custom expander is done, expand the result, which is
4009 // still VT.
4010 ExpandOp(Op, Lo, Hi);
4011 break;
4012 }
4013 }
4014
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004015 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004016 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004017 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004018
4019 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004020 TargetLowering::LegalizeAction Action =
4021 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4022 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4023 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004024 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004025 break;
4026 }
4027
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004028 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004029 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004030 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004031 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004032
Misha Brukman835702a2005-04-21 22:36:52 +00004033 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004034 case ISD::SUB: {
4035 // If the target wants to custom expand this, let them.
4036 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4037 TargetLowering::Custom) {
4038 Op = TLI.LowerOperation(Op, DAG);
4039 if (Op.Val) {
4040 ExpandOp(Op, Lo, Hi);
4041 break;
4042 }
4043 }
4044
4045 // Expand the subcomponents.
4046 SDOperand LHSL, LHSH, RHSL, RHSH;
4047 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4048 ExpandOp(Node->getOperand(1), RHSL, RHSH);
4049
4050 std::vector<SDOperand> Ops;
4051 Ops.push_back(LHSL);
4052 Ops.push_back(LHSH);
4053 Ops.push_back(RHSL);
4054 Ops.push_back(RHSH);
4055 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
4056 unsigned Opc =
4057 Node->getOpcode() == ISD::ADD ? ISD::ADD_PARTS : ISD::SUB_PARTS;
4058 Lo = DAG.getNode(Opc, VTs, Ops);
4059 Hi = Lo.getValue(1);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004060 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004061 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004062 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004063 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004064 SDOperand LL, LH, RL, RH;
4065 ExpandOp(Node->getOperand(0), LL, LH);
4066 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004067 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4068 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4069 // extended the sign bit of the low half through the upper half, and if so
4070 // emit a MULHS instead of the alternate sequence that is valid for any
4071 // i64 x i64 multiply.
4072 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4073 // is RH an extension of the sign bit of RL?
4074 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4075 RH.getOperand(1).getOpcode() == ISD::Constant &&
4076 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4077 // is LH an extension of the sign bit of LL?
4078 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4079 LH.getOperand(1).getOpcode() == ISD::Constant &&
4080 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4081 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4082 } else {
4083 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4084 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4085 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4086 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4087 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4088 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004089 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4090 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004091 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004092 }
4093 break;
4094 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004095 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4096 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4097 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4098 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004099 }
4100
Chris Lattnerac12f682005-12-21 18:02:52 +00004101 // Make sure the resultant values have been legalized themselves, unless this
4102 // is a type that requires multi-step expansion.
4103 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4104 Lo = LegalizeOp(Lo);
4105 Hi = LegalizeOp(Hi);
4106 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004107
4108 // Remember in a map if the values will be reused later.
4109 bool isNew =
4110 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4111 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004112}
4113
4114
4115// SelectionDAG::Legalize - This is the entry point for the file.
4116//
Chris Lattner4add7e32005-01-23 04:42:50 +00004117void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004118 /// run - This is the main entry point to this class.
4119 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004120 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004121}
4122