blob: dec782a070c51d9c3ebbe8f77128753bd9f6ed3b [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattnerdc750592005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman89b049a2005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000056
57 /// NeedsAnotherIteration - This is set when we expand a large integer
58 /// operation into smaller integer operations, but the smaller operations are
59 /// not set. This occurs only rarely in practice, for targets that don't have
60 /// 32-bit or larger integer registers.
61 bool NeedsAnotherIteration;
62
63 /// LegalizedNodes - For nodes that are of legal width, and that have more
64 /// than one use, this map indicates what regularized operand to use. This
65 /// allows us to avoid legalizing the same thing more than once.
66 std::map<SDOperand, SDOperand> LegalizedNodes;
67
Chris Lattner1f2c9d82005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattnerdc750592005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattnerea4ca942005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +000080 LegalizedNodes.insert(std::make_pair(From, To));
81 // If someone requests legalization of the new node, return itself.
82 if (From != To)
83 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +000084 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000085 void AddPromotedOperand(SDOperand From, SDOperand To) {
86 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
87 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +000088 // If someone requests legalization of the new node, return itself.
89 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +000090 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000091
Chris Lattnerdc750592005-01-07 07:47:09 +000092public:
93
Chris Lattner4add7e32005-01-23 04:42:50 +000094 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000095
96 /// Run - While there is still lowering to do, perform a pass over the DAG.
97 /// Most regularization can be done in a single pass, but targets that require
98 /// large values to be split into registers multiple times (e.g. i64 -> 4x
99 /// i16) require iteration for these values (the first iteration will demote
100 /// to i32, the second will demote to i16).
101 void Run() {
102 do {
103 NeedsAnotherIteration = false;
104 LegalizeDAG();
105 } while (NeedsAnotherIteration);
106 }
107
108 /// getTypeAction - Return how we should legalize values of this type, either
109 /// it is already legal or we need to expand it into multiple registers of
110 /// smaller integer type, or we need to promote it to a larger type.
111 LegalizeAction getTypeAction(MVT::ValueType VT) const {
112 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
113 }
114
115 /// isTypeLegal - Return true if this type is legal on this target.
116 ///
117 bool isTypeLegal(MVT::ValueType VT) const {
118 return getTypeAction(VT) == Legal;
119 }
120
121private:
122 void LegalizeDAG();
123
124 SDOperand LegalizeOp(SDOperand O);
125 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000126 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000127
Chris Lattneraac464e2005-01-21 06:05:23 +0000128 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
129 SDOperand &Hi);
130 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
131 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000132
Chris Lattner36e663d2005-12-23 00:16:34 +0000133 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000134 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
135 SDOperand LegalOp,
136 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000137 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
138 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000139 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
140 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000141
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000142 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
143 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000144 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
145 SDOperand &Lo, SDOperand &Hi);
146 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000147 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000148
Chris Lattnera5bf1032005-05-12 04:49:08 +0000149 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
150
Chris Lattnerdc750592005-01-07 07:47:09 +0000151 SDOperand getIntPtrConstant(uint64_t Val) {
152 return DAG.getConstant(Val, TLI.getPointerTy());
153 }
154};
155}
156
Chris Lattner301015a2005-11-19 05:51:46 +0000157static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000158 switch (VecOp) {
159 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-11-19 00:36:38 +0000160 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
161 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
162 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
163 }
164}
Chris Lattnerdc750592005-01-07 07:47:09 +0000165
Chris Lattner4add7e32005-01-23 04:42:50 +0000166SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
167 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
168 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000169 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000170 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000171}
172
Jim Laskeyf2516a92005-08-17 00:39:29 +0000173/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
174/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000175/// we expand it. At this point, we know that the result and operand types are
176/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000177SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
178 SDOperand Op0,
179 MVT::ValueType DestVT) {
180 if (Op0.getValueType() == MVT::i32) {
181 // simple 32-bit [signed|unsigned] integer to float/double expansion
182
183 // get the stack frame index of a 8 byte buffer
184 MachineFunction &MF = DAG.getMachineFunction();
185 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
186 // get address of 8 byte buffer
187 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
188 // word offset constant for Hi/Lo address computation
189 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
190 // set up Hi and Lo (into buffer) address based on endian
191 SDOperand Hi, Lo;
192 if (TLI.isLittleEndian()) {
193 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
194 Lo = StackSlot;
195 } else {
196 Hi = StackSlot;
197 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
198 }
199 // if signed map to unsigned space
200 SDOperand Op0Mapped;
201 if (isSigned) {
202 // constant used to invert sign bit (signed to unsigned mapping)
203 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
204 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
205 } else {
206 Op0Mapped = Op0;
207 }
208 // store the lo of the constructed double - based on integer input
209 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
210 Op0Mapped, Lo, DAG.getSrcValue(NULL));
211 // initial hi portion of constructed double
212 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
213 // store the hi of the constructed double - biased exponent
214 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
215 InitialHi, Hi, DAG.getSrcValue(NULL));
216 // load the constructed double
217 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
218 DAG.getSrcValue(NULL));
219 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000220 SDOperand Bias = DAG.getConstantFP(isSigned ?
221 BitsToDouble(0x4330000080000000ULL)
222 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000223 MVT::f64);
224 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000225 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000226 // final result
227 SDOperand Result;
228 // handle final rounding
229 if (DestVT == MVT::f64) {
230 // do nothing
231 Result = Sub;
232 } else {
233 // if f32 then cast to f32
234 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
235 }
Chris Lattner2af3ee42005-12-20 00:53:54 +0000236 return LegalizeOp(Result);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000237 }
238 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000239 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000240
Chris Lattnerd47675e2005-08-09 20:20:18 +0000241 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
242 DAG.getConstant(0, Op0.getValueType()),
243 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000244 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
245 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
246 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000247
Jim Laskeyf2516a92005-08-17 00:39:29 +0000248 // If the sign bit of the integer is set, the large number will be treated
249 // as a negative number. To counteract this, the dynamic code adds an
250 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000251 uint64_t FF;
252 switch (Op0.getValueType()) {
253 default: assert(0 && "Unsupported integer type!");
254 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
255 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
256 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
257 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
258 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000259 if (TLI.isLittleEndian()) FF <<= 32;
260 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000261
Chris Lattnerc30405e2005-08-26 17:15:30 +0000262 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-07-16 00:19:57 +0000263 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
264 SDOperand FudgeInReg;
265 if (DestVT == MVT::f32)
266 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
267 DAG.getSrcValue(NULL));
268 else {
269 assert(DestVT == MVT::f64 && "Unexpected conversion");
270 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
271 DAG.getEntryNode(), CPIdx,
272 DAG.getSrcValue(NULL), MVT::f32));
273 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000274
Chris Lattner2af3ee42005-12-20 00:53:54 +0000275 return LegalizeOp(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000276}
277
Chris Lattner19732782005-08-16 18:17:10 +0000278/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000279/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000280/// we promote it. At this point, we know that the result and operand types are
281/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
282/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000283SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
284 MVT::ValueType DestVT,
285 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000286 // First step, figure out the appropriate *INT_TO_FP operation to use.
287 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000288
Chris Lattnere3e847b2005-07-16 00:19:57 +0000289 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000290
Chris Lattnere3e847b2005-07-16 00:19:57 +0000291 // Scan for the appropriate larger type to use.
292 while (1) {
293 NewInTy = (MVT::ValueType)(NewInTy+1);
294 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000295
Chris Lattnere3e847b2005-07-16 00:19:57 +0000296 // If the target supports SINT_TO_FP of this type, use it.
297 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
298 default: break;
299 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000300 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000301 break; // Can't use this datatype.
302 // FALL THROUGH.
303 case TargetLowering::Custom:
304 OpToUse = ISD::SINT_TO_FP;
305 break;
306 }
307 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000308 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000309
Chris Lattnere3e847b2005-07-16 00:19:57 +0000310 // If the target supports UINT_TO_FP of this type, use it.
311 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
312 default: break;
313 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000314 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000315 break; // Can't use this datatype.
316 // FALL THROUGH.
317 case TargetLowering::Custom:
318 OpToUse = ISD::UINT_TO_FP;
319 break;
320 }
321 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000322
Chris Lattnere3e847b2005-07-16 00:19:57 +0000323 // Otherwise, try a larger type.
324 }
325
Chris Lattnere3e847b2005-07-16 00:19:57 +0000326 // Okay, we found the operation and type to use. Zero extend our input to the
327 // desired type then run the operation on it.
Chris Lattner2af3ee42005-12-20 00:53:54 +0000328 SDOperand N = DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattner2af3ee42005-12-20 00:53:54 +0000331 // Make sure to legalize any nodes we create here.
332 return LegalizeOp(N);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000333}
334
Chris Lattner44fe26f2005-07-29 00:11:56 +0000335/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
336/// FP_TO_*INT operation of the specified operand when the target requests that
337/// we promote it. At this point, we know that the result and operand types are
338/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
339/// operation that returns a larger result.
340SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
341 MVT::ValueType DestVT,
342 bool isSigned) {
343 // First step, figure out the appropriate FP_TO*INT operation to use.
344 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-07-29 00:11:56 +0000346 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000347
Chris Lattner44fe26f2005-07-29 00:11:56 +0000348 // Scan for the appropriate larger type to use.
349 while (1) {
350 NewOutTy = (MVT::ValueType)(NewOutTy+1);
351 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000352
Chris Lattner44fe26f2005-07-29 00:11:56 +0000353 // If the target supports FP_TO_SINT returning this type, use it.
354 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
355 default: break;
356 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000357 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000358 break; // Can't use this datatype.
359 // FALL THROUGH.
360 case TargetLowering::Custom:
361 OpToUse = ISD::FP_TO_SINT;
362 break;
363 }
364 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000365
Chris Lattner44fe26f2005-07-29 00:11:56 +0000366 // If the target supports FP_TO_UINT of this type, use it.
367 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
368 default: break;
369 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000370 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000371 break; // Can't use this datatype.
372 // FALL THROUGH.
373 case TargetLowering::Custom:
374 OpToUse = ISD::FP_TO_UINT;
375 break;
376 }
377 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000378
Chris Lattner44fe26f2005-07-29 00:11:56 +0000379 // Otherwise, try a larger type.
380 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000381
Chris Lattner44fe26f2005-07-29 00:11:56 +0000382 // Okay, we found the operation and type to use. Truncate the result of the
383 // extended FP_TO_*INT operation to the desired size.
Chris Lattner2af3ee42005-12-20 00:53:54 +0000384 SDOperand N = DAG.getNode(ISD::TRUNCATE, DestVT,
385 DAG.getNode(OpToUse, NewOutTy, LegalOp));
386 // Make sure to legalize any nodes we create here in the next pass.
387 return LegalizeOp(N);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000388}
389
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000390/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
391/// not been visited yet and if all of its operands have already been visited.
392static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
393 std::map<SDNode*, unsigned> &Visited) {
394 if (++Visited[N] != N->getNumOperands())
395 return; // Haven't visited all operands yet
396
397 Order.push_back(N);
398
399 if (N->hasOneUse()) { // Tail recurse in common case.
400 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
401 return;
402 }
403
404 // Now that we have N in, add anything that uses it if all of their operands
405 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000406 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
407 ComputeTopDownOrdering(*UI, Order, Visited);
408}
409
Chris Lattner44fe26f2005-07-29 00:11:56 +0000410
Chris Lattnerdc750592005-01-07 07:47:09 +0000411void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000412 // The legalize process is inherently a bottom-up recursive process (users
413 // legalize their uses before themselves). Given infinite stack space, we
414 // could just start legalizing on the root and traverse the whole graph. In
415 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000416 // blocks. To avoid this problem, compute an ordering of the nodes where each
417 // node is only legalized after all of its operands are legalized.
418 std::map<SDNode*, unsigned> Visited;
419 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000420
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000421 // Compute ordering from all of the leaves in the graphs, those (like the
422 // entry node) that have no operands.
423 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
424 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000425 if (I->getNumOperands() == 0) {
426 Visited[I] = 0 - 1U;
427 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000428 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000429 }
430
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000431 assert(Order.size() == Visited.size() &&
432 Order.size() ==
433 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000434 "Error: DAG is cyclic!");
435 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000436
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000437 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
438 SDNode *N = Order[i];
439 switch (getTypeAction(N->getValueType(0))) {
440 default: assert(0 && "Bad type action!");
441 case Legal:
442 LegalizeOp(SDOperand(N, 0));
443 break;
444 case Promote:
445 PromoteOp(SDOperand(N, 0));
446 break;
447 case Expand: {
448 SDOperand X, Y;
449 ExpandOp(SDOperand(N, 0), X, Y);
450 break;
451 }
452 }
453 }
454
455 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000456 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000457 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
458 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000459
460 ExpandedNodes.clear();
461 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000462 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000463
464 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000465 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000466}
467
468SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000469 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000470 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000471 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000472
Chris Lattnerdc750592005-01-07 07:47:09 +0000473 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000474 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000475 if (Node->getNumValues() > 1) {
476 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
477 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000478 case Legal: break; // Nothing to do.
479 case Expand: {
480 SDOperand T1, T2;
481 ExpandOp(Op.getValue(i), T1, T2);
482 assert(LegalizedNodes.count(Op) &&
483 "Expansion didn't add legal operands!");
484 return LegalizedNodes[Op];
485 }
486 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000487 PromoteOp(Op.getValue(i));
488 assert(LegalizedNodes.count(Op) &&
489 "Expansion didn't add legal operands!");
490 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000491 }
492 }
493
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000494 // Note that LegalizeOp may be reentered even from single-use nodes, which
495 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000496 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
497 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000498
Nate Begemane5b86d72005-08-10 20:51:12 +0000499 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000500
501 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000502
503 switch (Node->getOpcode()) {
504 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000505 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
506 // If this is a target node, legalize it by legalizing the operands then
507 // passing it through.
508 std::vector<SDOperand> Ops;
509 bool Changed = false;
510 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
511 Ops.push_back(LegalizeOp(Node->getOperand(i)));
512 Changed = Changed || Node->getOperand(i) != Ops.back();
513 }
514 if (Changed)
515 if (Node->getNumValues() == 1)
516 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
517 else {
518 std::vector<MVT::ValueType> VTs(Node->value_begin(),
519 Node->value_end());
520 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
521 }
522
523 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
524 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
525 return Result.getValue(Op.ResNo);
526 }
527 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000528 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
529 assert(0 && "Do not know how to legalize this operator!");
530 abort();
531 case ISD::EntryToken:
532 case ISD::FrameIndex:
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000533 case ISD::TargetFrameIndex:
534 case ISD::Register:
535 case ISD::TargetConstant:
Nate Begeman4e56db62005-12-10 02:36:00 +0000536 case ISD::TargetConstantPool:
Chris Lattnerdc750592005-01-07 07:47:09 +0000537 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000538 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000539 case ISD::ExternalSymbol:
Andrew Lenharth72594262005-12-24 23:42:32 +0000540 case ISD::TargetExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000541 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000542 case ISD::BasicBlock:
543 case ISD::CONDCODE:
544 case ISD::VALUETYPE:
545 case ISD::SRCVALUE:
Chris Lattner435b4022005-11-29 06:21:05 +0000546 case ISD::STRING:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000547 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
548 default: assert(0 && "This action is not supported yet!");
549 case TargetLowering::Custom: {
550 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
551 if (Tmp.Val) {
552 Result = LegalizeOp(Tmp);
553 break;
554 }
555 } // FALLTHROUGH if the target doesn't want to lower this op after all.
556 case TargetLowering::Legal:
557 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
558 break;
559 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000560 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000561 case ISD::AssertSext:
562 case ISD::AssertZext:
563 Tmp1 = LegalizeOp(Node->getOperand(0));
564 if (Tmp1 != Node->getOperand(0))
565 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
566 Node->getOperand(1));
567 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000568 case ISD::MERGE_VALUES:
569 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000570 case ISD::CopyFromReg:
571 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000572 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000573 if (Node->getNumValues() == 2) {
Chris Lattnere3c67e92005-12-18 15:27:43 +0000574 if (Tmp1 != Node->getOperand(0))
575 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattner33182322005-08-16 21:55:35 +0000576 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattnere3c67e92005-12-18 15:27:43 +0000577 Node->getValueType(0));
578 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000579 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
580 if (Node->getNumOperands() == 3)
581 Tmp2 = LegalizeOp(Node->getOperand(2));
582 if (Tmp1 != Node->getOperand(0) ||
583 (Node->getNumOperands() == 3 && Tmp2 != Node->getOperand(2)))
Chris Lattnere3c67e92005-12-18 15:27:43 +0000584 Result = DAG.getCopyFromReg(Tmp1,
585 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
586 Node->getValueType(0), Tmp2);
587 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
588 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000589 // Since CopyFromReg produces two values, make sure to remember that we
590 // legalized both of them.
591 AddLegalizedOperand(Op.getValue(0), Result);
592 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
593 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000594 case ISD::UNDEF: {
595 MVT::ValueType VT = Op.getValueType();
596 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000597 default: assert(0 && "This action is not supported yet!");
598 case TargetLowering::Expand:
599 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000600 if (MVT::isInteger(VT))
601 Result = DAG.getConstant(0, VT);
602 else if (MVT::isFloatingPoint(VT))
603 Result = DAG.getConstantFP(0, VT);
604 else
605 assert(0 && "Unknown value type!");
606 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000607 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000608 break;
609 }
610 break;
611 }
Chris Lattner435b4022005-11-29 06:21:05 +0000612
613 case ISD::LOCATION:
614 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
615 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
616
617 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
618 case TargetLowering::Promote:
619 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000620 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000621 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
622 if (TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other) && DebugInfo) {
Jim Laskey9e296be2005-12-21 20:51:37 +0000623 std::vector<SDOperand> Ops;
624 Ops.push_back(Tmp1); // chain
625 Ops.push_back(Node->getOperand(1)); // line #
626 Ops.push_back(Node->getOperand(2)); // col #
627 const std::string &fname =
628 cast<StringSDNode>(Node->getOperand(3))->getValue();
629 const std::string &dirname =
630 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey219d5592006-01-04 22:28:25 +0000631 unsigned srcfile = DebugInfo->getUniqueSourceID(fname, dirname);
Jim Laskey0da76a62006-01-04 15:04:11 +0000632 Ops.push_back(DAG.getConstant(srcfile, MVT::i32)); // source file id
Jim Laskey219d5592006-01-04 22:28:25 +0000633 unsigned id = DebugInfo->getNextUniqueID();
Jim Laskey0da76a62006-01-04 15:04:11 +0000634 Ops.push_back(DAG.getConstant(id, MVT::i32)); // label id
Jim Laskey9e296be2005-12-21 20:51:37 +0000635 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
636 } else {
637 Result = Tmp1; // chain
638 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000639 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner435b4022005-11-29 06:21:05 +0000640 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000641 }
Chris Lattner435b4022005-11-29 06:21:05 +0000642 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000643 if (Tmp1 != Node->getOperand(0) ||
644 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000645 std::vector<SDOperand> Ops;
646 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000647 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
648 Ops.push_back(Node->getOperand(1)); // line # must be legal.
649 Ops.push_back(Node->getOperand(2)); // col # must be legal.
650 } else {
651 // Otherwise promote them.
652 Ops.push_back(PromoteOp(Node->getOperand(1)));
653 Ops.push_back(PromoteOp(Node->getOperand(2)));
654 }
Chris Lattner435b4022005-11-29 06:21:05 +0000655 Ops.push_back(Node->getOperand(3)); // filename must be legal.
656 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
657 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
658 }
659 break;
660 }
661 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000662
663 case ISD::DEBUG_LOC:
Jim Laskey0da76a62006-01-04 15:04:11 +0000664 assert(Node->getNumOperands() == 5 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000665 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
666 case TargetLowering::Promote:
667 case TargetLowering::Expand:
668 default: assert(0 && "This action is not supported yet!");
Jim Laskey0da76a62006-01-04 15:04:11 +0000669 case TargetLowering::Legal: {
670 SDOperand Tmp5;
671 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
672 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
673 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
674 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
675 Tmp5 = LegalizeOp(Node->getOperand(4)); // Legalize the label id.
676
677 if (Tmp1 != Node->getOperand(0) ||
678 Tmp2 != Node->getOperand(1) ||
679 Tmp3 != Node->getOperand(2) ||
680 Tmp4 != Node->getOperand(3) ||
681 Tmp5 != Node->getOperand(4)) {
682 Result =
683 DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
684 }
Jim Laskey7c462762005-12-16 22:45:29 +0000685 }
686 break;
687 }
688 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000689
Chris Lattnerdc750592005-01-07 07:47:09 +0000690 case ISD::Constant:
691 // We know we don't need to expand constants here, constants only have one
692 // value and we check that it is fine above.
693
694 // FIXME: Maybe we should handle things like targets that don't support full
695 // 32-bit immediates?
696 break;
697 case ISD::ConstantFP: {
698 // Spill FP immediates to the constant pool if the target cannot directly
699 // codegen them. Targets often have some immediate values that can be
700 // efficiently generated into an FP register without a load. We explicitly
701 // leave these constants as ConstantFP nodes for the target to deal with.
702
703 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
704
705 // Check to see if this FP immediate is already legal.
706 bool isLegal = false;
707 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
708 E = TLI.legal_fpimm_end(); I != E; ++I)
709 if (CFP->isExactlyValue(*I)) {
710 isLegal = true;
711 break;
712 }
713
714 if (!isLegal) {
715 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000716 bool Extend = false;
717
718 // If a FP immediate is precise when represented as a float, we put it
719 // into the constant pool as a float, even if it's is statically typed
720 // as a double.
721 MVT::ValueType VT = CFP->getValueType(0);
722 bool isDouble = VT == MVT::f64;
723 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
724 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000725 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
726 // Only do this if the target has a native EXTLOAD instruction from
727 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000728 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000729 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
730 VT = MVT::f32;
731 Extend = true;
732 }
Misha Brukman835702a2005-04-21 22:36:52 +0000733
Nate Begeman4e56db62005-12-10 02:36:00 +0000734 SDOperand CPIdx =
735 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000736 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000737 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
738 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000739 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000740 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
741 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000742 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000743 }
744 break;
745 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000746 case ISD::ConstantVec: {
747 // We assume that vector constants are not legal, and will be immediately
748 // spilled to the constant pool.
749 //
750 // FIXME: revisit this when we have some kind of mechanism by which targets
751 // can decided legality of vector constants, of which there may be very
752 // many.
753 //
754 // Create a ConstantPacked, and put it in the constant pool.
755 std::vector<Constant*> CV;
756 MVT::ValueType VT = Node->getValueType(0);
757 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
758 SDOperand OpN = Node->getOperand(I);
759 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
760 if (MVT::isFloatingPoint(VT))
761 CV.push_back(ConstantFP::get(OpNTy,
762 cast<ConstantFPSDNode>(OpN)->getValue()));
763 else
764 CV.push_back(ConstantUInt::get(OpNTy,
765 cast<ConstantSDNode>(OpN)->getValue()));
766 }
767 Constant *CP = ConstantPacked::get(CV);
Nate Begeman956aef42005-12-13 03:03:23 +0000768 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000769 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
770 break;
771 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000772 case ISD::TokenFactor:
773 if (Node->getNumOperands() == 2) {
774 bool Changed = false;
775 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
776 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
777 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
778 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
779 } else {
780 std::vector<SDOperand> Ops;
781 bool Changed = false;
782 // Legalize the operands.
783 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
784 SDOperand Op = Node->getOperand(i);
785 Ops.push_back(LegalizeOp(Op));
786 Changed |= Ops[i] != Op;
787 }
788 if (Changed)
789 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000790 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000791 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000792
Chris Lattner2dce7032005-05-12 23:24:06 +0000793 case ISD::CALLSEQ_START:
794 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000795 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000796 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000797 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000798 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000799 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000800
Chris Lattner2dce7032005-05-12 23:24:06 +0000801 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000802 // nodes are treated specially and are mutated in place. This makes the dag
803 // legalization process more efficient and also makes libcall insertion
804 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000805 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000806 case ISD::DYNAMIC_STACKALLOC:
807 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
808 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
809 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
810 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000811 Tmp3 != Node->getOperand(2)) {
812 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
813 std::vector<SDOperand> Ops;
814 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
815 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
816 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000817 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000818
819 // Since this op produces two values, make sure to remember that we
820 // legalized both of them.
821 AddLegalizedOperand(SDOperand(Node, 0), Result);
822 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
823 return Result.getValue(Op.ResNo);
824
Chris Lattnerd0feb642005-05-13 18:43:43 +0000825 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000826 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000827 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
828 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000829
830 bool Changed = false;
831 std::vector<SDOperand> Ops;
832 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
833 Ops.push_back(LegalizeOp(Node->getOperand(i)));
834 Changed |= Ops.back() != Node->getOperand(i);
835 }
836
837 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000838 std::vector<MVT::ValueType> RetTyVTs;
839 RetTyVTs.reserve(Node->getNumValues());
840 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000841 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000842 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
843 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000844 } else {
845 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000846 }
Chris Lattner9242c502005-01-09 19:43:23 +0000847 // Since calls produce multiple values, make sure to remember that we
848 // legalized all of them.
849 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
850 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
851 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000852 }
Chris Lattner68a12142005-01-07 22:12:08 +0000853 case ISD::BR:
854 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
855 if (Tmp1 != Node->getOperand(0))
856 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
857 break;
858
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000859 case ISD::BRCOND:
860 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000861
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000862 switch (getTypeAction(Node->getOperand(1).getValueType())) {
863 case Expand: assert(0 && "It's impossible to expand bools");
864 case Legal:
865 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
866 break;
867 case Promote:
868 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
869 break;
870 }
Nate Begeman371e4952005-08-16 19:49:35 +0000871
872 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
873 default: assert(0 && "This action is not supported yet!");
874 case TargetLowering::Expand:
875 // Expand brcond's setcc into its constituent parts and create a BR_CC
876 // Node.
877 if (Tmp2.getOpcode() == ISD::SETCC) {
878 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
879 Tmp2.getOperand(0), Tmp2.getOperand(1),
880 Node->getOperand(2));
881 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000882 // Make sure the condition is either zero or one. It may have been
883 // promoted from something else.
884 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
885
Nate Begeman371e4952005-08-16 19:49:35 +0000886 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
887 DAG.getCondCode(ISD::SETNE), Tmp2,
888 DAG.getConstant(0, Tmp2.getValueType()),
889 Node->getOperand(2));
890 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000891 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +0000892 break;
Evan Cheng6fc31042005-12-19 23:12:38 +0000893 case TargetLowering::Custom: {
894 SDOperand Tmp =
895 TLI.LowerOperation(DAG.getNode(ISD::BRCOND, Node->getValueType(0),
896 Tmp1, Tmp2, Node->getOperand(2)), DAG);
897 if (Tmp.Val) {
898 Result = LegalizeOp(Tmp);
899 break;
900 }
901 // FALLTHROUGH if the target thinks it is legal.
902 }
Nate Begeman371e4952005-08-16 19:49:35 +0000903 case TargetLowering::Legal:
904 // Basic block destination (Op#2) is always legal.
905 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
906 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
907 Node->getOperand(2));
908 break;
909 }
910 break;
911 case ISD::BR_CC:
912 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000913 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000914 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
915 Node->getOperand(2), // LHS
916 Node->getOperand(3), // RHS
917 Node->getOperand(1)));
918 // If we get a SETCC back from legalizing the SETCC node we just
919 // created, then use its LHS, RHS, and CC directly in creating a new
920 // node. Otherwise, select between the true and false value based on
921 // comparing the result of the legalized with zero.
922 if (Tmp2.getOpcode() == ISD::SETCC) {
923 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
924 Tmp2.getOperand(0), Tmp2.getOperand(1),
925 Node->getOperand(4));
926 } else {
927 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
928 DAG.getCondCode(ISD::SETNE),
929 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
930 Node->getOperand(4));
931 }
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000932 break;
933 }
934
935 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
936 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
937
938 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
939 default: assert(0 && "Unexpected action for BR_CC!");
940 case TargetLowering::Custom: {
941 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
942 Tmp2, Tmp3, Node->getOperand(4));
943 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
944 if (Tmp4.Val) {
945 Result = LegalizeOp(Tmp4);
946 break;
947 }
948 } // FALLTHROUGH if the target doesn't want to lower this op after all.
949 case TargetLowering::Legal:
950 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
951 Tmp3 != Node->getOperand(3)) {
952 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
953 Tmp2, Tmp3, Node->getOperand(4));
954 }
955 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000956 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000957 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000958 case ISD::BRCONDTWOWAY:
959 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
960 switch (getTypeAction(Node->getOperand(1).getValueType())) {
961 case Expand: assert(0 && "It's impossible to expand bools");
962 case Legal:
963 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
964 break;
965 case Promote:
966 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
967 break;
968 }
969 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
970 // pair.
971 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
972 case TargetLowering::Promote:
973 default: assert(0 && "This action is not supported yet!");
974 case TargetLowering::Legal:
975 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
976 std::vector<SDOperand> Ops;
977 Ops.push_back(Tmp1);
978 Ops.push_back(Tmp2);
979 Ops.push_back(Node->getOperand(2));
980 Ops.push_back(Node->getOperand(3));
981 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
982 }
983 break;
984 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000985 // If BRTWOWAY_CC is legal for this target, then simply expand this node
986 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
987 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000988 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000989 if (Tmp2.getOpcode() == ISD::SETCC) {
990 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
991 Tmp2.getOperand(0), Tmp2.getOperand(1),
992 Node->getOperand(2), Node->getOperand(3));
993 } else {
994 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
995 DAG.getConstant(0, Tmp2.getValueType()),
996 Node->getOperand(2), Node->getOperand(3));
997 }
998 } else {
999 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +00001000 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001001 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
1002 }
Chris Lattnerc06da622005-12-18 23:54:29 +00001003 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattnerfd986782005-04-09 03:30:19 +00001004 break;
1005 }
1006 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001007 case ISD::BRTWOWAY_CC:
1008 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001009 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +00001010 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1011 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1012 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1013 Tmp3 != Node->getOperand(3)) {
1014 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1015 Node->getOperand(4), Node->getOperand(5));
1016 }
1017 break;
1018 } else {
1019 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1020 Node->getOperand(2), // LHS
1021 Node->getOperand(3), // RHS
1022 Node->getOperand(1)));
1023 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1024 // pair.
1025 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1026 default: assert(0 && "This action is not supported yet!");
1027 case TargetLowering::Legal:
1028 // If we get a SETCC back from legalizing the SETCC node we just
1029 // created, then use its LHS, RHS, and CC directly in creating a new
1030 // node. Otherwise, select between the true and false value based on
1031 // comparing the result of the legalized with zero.
1032 if (Tmp2.getOpcode() == ISD::SETCC) {
1033 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1034 Tmp2.getOperand(0), Tmp2.getOperand(1),
1035 Node->getOperand(4), Node->getOperand(5));
1036 } else {
1037 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1038 DAG.getConstant(0, Tmp2.getValueType()),
1039 Node->getOperand(4), Node->getOperand(5));
1040 }
1041 break;
1042 case TargetLowering::Expand:
1043 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1044 Node->getOperand(4));
1045 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1046 break;
1047 }
Chris Lattner0fab4592005-12-21 19:40:42 +00001048 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +00001049 }
1050 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001051 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001052 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1053 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001054
Evan Cheng31d15fa2005-12-23 07:29:34 +00001055 MVT::ValueType VT = Node->getValueType(0);
1056 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1057 default: assert(0 && "This action is not supported yet!");
1058 case TargetLowering::Custom: {
1059 SDOperand Op = DAG.getLoad(Node->getValueType(0),
1060 Tmp1, Tmp2, Node->getOperand(2));
1061 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1062 if (Tmp.Val) {
1063 Result = LegalizeOp(Tmp);
1064 // Since loads produce two values, make sure to remember that we legalized
1065 // both of them.
1066 AddLegalizedOperand(SDOperand(Node, 0), Result);
1067 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1068 return Result.getValue(Op.ResNo);
1069 }
1070 // FALLTHROUGH if the target thinks it is legal.
1071 }
1072 case TargetLowering::Legal:
1073 if (Tmp1 != Node->getOperand(0) ||
1074 Tmp2 != Node->getOperand(1))
1075 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1076 Node->getOperand(2));
1077 else
1078 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +00001079
Evan Cheng31d15fa2005-12-23 07:29:34 +00001080 // Since loads produce two values, make sure to remember that we legalized
1081 // both of them.
1082 AddLegalizedOperand(SDOperand(Node, 0), Result);
1083 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1084 return Result.getValue(Op.ResNo);
1085 }
1086 assert(0 && "Unreachable");
1087 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001088 case ISD::EXTLOAD:
1089 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001090 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001091 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1092 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001093
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001094 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001095 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001096 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001097 case TargetLowering::Promote:
1098 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001099 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1100 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001101 // Since loads produce two values, make sure to remember that we legalized
1102 // both of them.
1103 AddLegalizedOperand(SDOperand(Node, 0), Result);
1104 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1105 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +00001106
Evan Cheng31d15fa2005-12-23 07:29:34 +00001107 case TargetLowering::Custom: {
1108 SDOperand Op = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1109 Tmp1, Tmp2, Node->getOperand(2),
1110 SrcVT);
1111 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1112 if (Tmp.Val) {
1113 Result = LegalizeOp(Tmp);
1114 // Since loads produce two values, make sure to remember that we legalized
1115 // both of them.
1116 AddLegalizedOperand(SDOperand(Node, 0), Result);
1117 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1118 return Result.getValue(Op.ResNo);
1119 }
1120 // FALLTHROUGH if the target thinks it is legal.
1121 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001122 case TargetLowering::Legal:
1123 if (Tmp1 != Node->getOperand(0) ||
1124 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001125 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1126 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001127 else
1128 Result = SDOperand(Node, 0);
1129
1130 // Since loads produce two values, make sure to remember that we legalized
1131 // both of them.
1132 AddLegalizedOperand(SDOperand(Node, 0), Result);
1133 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1134 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001135 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001136 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001137 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1138 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001139 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc06da622005-12-18 23:54:29 +00001140 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001141 Load = LegalizeOp(Load);
1142 AddLegalizedOperand(SDOperand(Node, 0), Result);
1143 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001144 if (Op.ResNo)
1145 return Load.getValue(1);
1146 return Result;
1147 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001148 assert(Node->getOpcode() != ISD::EXTLOAD &&
1149 "EXTLOAD should always be supported!");
1150 // Turn the unsupported load into an EXTLOAD followed by an explicit
1151 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001152 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1153 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001154 SDOperand ValRes;
1155 if (Node->getOpcode() == ISD::SEXTLOAD)
1156 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001157 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001158 else
1159 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc06da622005-12-18 23:54:29 +00001160 Result = LegalizeOp(Result); // Relegalize new nodes.
1161 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001162 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1163 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001164 if (Op.ResNo)
1165 return Result.getValue(1);
1166 return ValRes;
1167 }
1168 assert(0 && "Unreachable");
1169 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001170 case ISD::EXTRACT_ELEMENT: {
1171 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1172 switch (getTypeAction(OpTy)) {
1173 default:
1174 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1175 break;
1176 case Legal:
1177 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1178 // 1 -> Hi
1179 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1180 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1181 TLI.getShiftAmountTy()));
1182 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1183 } else {
1184 // 0 -> Lo
1185 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1186 Node->getOperand(0));
1187 }
1188 Result = LegalizeOp(Result);
1189 break;
1190 case Expand:
1191 // Get both the low and high parts.
1192 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1193 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1194 Result = Tmp2; // 1 -> Hi
1195 else
1196 Result = Tmp1; // 0 -> Lo
1197 break;
1198 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001199 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001200 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001201
1202 case ISD::CopyToReg:
1203 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001204
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001205 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001206 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001207 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001208 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001209 if (Node->getNumValues() == 1) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001210 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1211 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1212 Node->getOperand(1), Tmp2);
1213 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001214 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1215 if (Node->getNumOperands() == 4)
1216 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001217 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001218 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001219 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1220 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1221 }
1222
1223 // Since this produces 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);
1228 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001229 break;
1230
1231 case ISD::RET:
1232 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1233 switch (Node->getNumOperands()) {
1234 case 2: // ret val
1235 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1236 case Legal:
1237 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001238 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001239 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1240 break;
1241 case Expand: {
1242 SDOperand Lo, Hi;
1243 ExpandOp(Node->getOperand(1), Lo, Hi);
1244 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001245 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001246 }
1247 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001248 Tmp2 = PromoteOp(Node->getOperand(1));
1249 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1250 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001251 }
1252 break;
1253 case 1: // ret void
1254 if (Tmp1 != Node->getOperand(0))
1255 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1256 break;
1257 default: { // ret <values>
1258 std::vector<SDOperand> NewValues;
1259 NewValues.push_back(Tmp1);
1260 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1261 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1262 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001263 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001264 break;
1265 case Expand: {
1266 SDOperand Lo, Hi;
1267 ExpandOp(Node->getOperand(i), Lo, Hi);
1268 NewValues.push_back(Lo);
1269 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001270 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001271 }
1272 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001273 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001274 }
1275 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1276 break;
1277 }
1278 }
1279 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001280 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001281 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1282 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1283
Chris Lattnere69daaf2005-01-08 06:25:56 +00001284 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001285 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001286 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001287 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001288 DAG.getConstant(FloatToBits(CFP->getValue()),
1289 MVT::i32),
1290 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001291 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001292 } else {
1293 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001294 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001295 DAG.getConstant(DoubleToBits(CFP->getValue()),
1296 MVT::i64),
1297 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001298 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001299 }
Chris Lattnera4743132005-02-22 07:23:39 +00001300 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001301 }
1302
Chris Lattnerdc750592005-01-07 07:47:09 +00001303 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1304 case Legal: {
1305 SDOperand Val = LegalizeOp(Node->getOperand(1));
1306 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1307 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001308 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1309 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001310
1311 MVT::ValueType VT = Result.Val->getOperand(1).getValueType();
1312 switch (TLI.getOperationAction(Result.Val->getOpcode(), VT)) {
1313 default: assert(0 && "This action is not supported yet!");
1314 case TargetLowering::Custom: {
1315 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1316 if (Tmp.Val) {
1317 Result = LegalizeOp(Tmp);
1318 break;
1319 }
1320 // FALLTHROUGH if the target thinks it is legal.
1321 }
1322 case TargetLowering::Legal:
1323 // Nothing to do.
1324 break;
1325 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001326 break;
1327 }
1328 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001329 // Truncate the value and store the result.
1330 Tmp3 = PromoteOp(Node->getOperand(1));
1331 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001332 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001333 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001334 break;
1335
Chris Lattnerdc750592005-01-07 07:47:09 +00001336 case Expand:
1337 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001338 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001339 ExpandOp(Node->getOperand(1), Lo, Hi);
1340
1341 if (!TLI.isLittleEndian())
1342 std::swap(Lo, Hi);
1343
Chris Lattner55e9cde2005-05-11 04:51:16 +00001344 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1345 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001346 // If this is a vector type, then we have to calculate the increment as
1347 // the product of the element size in bytes, and the number of elements
1348 // in the high half of the vector.
1349 if (MVT::Vector == Hi.getValueType()) {
1350 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1351 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1352 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1353 } else {
1354 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1355 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001356 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1357 getIntPtrConstant(IncrementSize));
1358 assert(isTypeLegal(Tmp2.getValueType()) &&
1359 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001360 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001361 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1362 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001363 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1364 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001365 }
1366 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001367 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001368 case ISD::PCMARKER:
1369 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001370 if (Tmp1 != Node->getOperand(0))
1371 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001372 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001373 case ISD::READCYCLECOUNTER:
1374 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthf9b27d72005-12-02 06:08:08 +00001375 if (Tmp1 != Node->getOperand(0)) {
1376 std::vector<MVT::ValueType> rtypes;
1377 std::vector<SDOperand> rvals;
1378 rtypes.push_back(MVT::i64);
1379 rtypes.push_back(MVT::Other);
1380 rvals.push_back(Tmp1);
1381 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1382 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001383
1384 // Since rdcc produce two values, make sure to remember that we legalized
1385 // both of them.
1386 AddLegalizedOperand(SDOperand(Node, 0), Result);
1387 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1388 return Result.getValue(Op.ResNo);
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001389
Evan Cheng31d15fa2005-12-23 07:29:34 +00001390 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001391 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1392 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1393
1394 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattnerc7037ab2005-12-23 16:12:20 +00001395 case Promote:
1396 case Expand:
1397 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001398 case Legal:
1399 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001400
1401 // The only promote case we handle is TRUNCSTORE:i1 X into
1402 // -> TRUNCSTORE:i8 (and X, 1)
1403 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1404 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1405 TargetLowering::Promote) {
1406 // Promote the bool to a mask then store.
1407 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1408 DAG.getConstant(1, Tmp2.getValueType()));
1409 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1410 Node->getOperand(3), DAG.getValueType(MVT::i8));
1411
1412 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1413 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001414 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001415 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001416 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001417
1418 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1419 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1420 default: assert(0 && "This action is not supported yet!");
1421 case TargetLowering::Custom: {
1422 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1423 if (Tmp.Val) {
1424 Result = LegalizeOp(Tmp);
1425 break;
1426 }
1427 // FALLTHROUGH if the target thinks it is legal.
1428 }
1429 case TargetLowering::Legal:
1430 // Nothing to do.
1431 break;
1432 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001433 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001434 }
1435 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001436 }
Chris Lattner39c67442005-01-14 22:08:15 +00001437 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001438 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1439 case Expand: assert(0 && "It's impossible to expand bools");
1440 case Legal:
1441 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1442 break;
1443 case Promote:
1444 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1445 break;
1446 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001447 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001448 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001449
Nate Begeman987121a2005-08-23 04:29:48 +00001450 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001451 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001452 case TargetLowering::Expand:
1453 if (Tmp1.getOpcode() == ISD::SETCC) {
1454 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1455 Tmp2, Tmp3,
1456 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1457 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001458 // Make sure the condition is either zero or one. It may have been
1459 // promoted from something else.
1460 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001461 Result = DAG.getSelectCC(Tmp1,
1462 DAG.getConstant(0, Tmp1.getValueType()),
1463 Tmp2, Tmp3, ISD::SETNE);
1464 }
Chris Lattnerc06da622005-12-18 23:54:29 +00001465 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begemane5b86d72005-08-10 20:51:12 +00001466 break;
Evan Cheng225a4d02005-12-17 01:21:05 +00001467 case TargetLowering::Custom: {
1468 SDOperand Tmp =
1469 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1470 Tmp1, Tmp2, Tmp3), DAG);
1471 if (Tmp.Val) {
1472 Result = LegalizeOp(Tmp);
1473 break;
1474 }
1475 // FALLTHROUGH if the target thinks it is legal.
1476 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001477 case TargetLowering::Legal:
1478 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1479 Tmp3 != Node->getOperand(2))
1480 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1481 Tmp1, Tmp2, Tmp3);
1482 break;
1483 case TargetLowering::Promote: {
1484 MVT::ValueType NVT =
1485 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1486 unsigned ExtOp, TruncOp;
1487 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001488 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001489 TruncOp = ISD::TRUNCATE;
1490 } else {
1491 ExtOp = ISD::FP_EXTEND;
1492 TruncOp = ISD::FP_ROUND;
1493 }
1494 // Promote each of the values to the new type.
1495 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1496 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1497 // Perform the larger operation, then round down.
1498 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1499 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1500 break;
1501 }
1502 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001503 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001504 case ISD::SELECT_CC:
1505 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1506 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1507
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001508 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001509 // Everything is legal, see if we should expand this op or something.
1510 switch (TLI.getOperationAction(ISD::SELECT_CC,
1511 Node->getOperand(0).getValueType())) {
1512 default: assert(0 && "This action is not supported yet!");
1513 case TargetLowering::Custom: {
1514 SDOperand Tmp =
1515 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1516 Node->getOperand(0),
1517 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001518 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001519 if (Tmp.Val) {
1520 Result = LegalizeOp(Tmp);
1521 break;
1522 }
1523 } // FALLTHROUGH if the target can't lower this operation after all.
1524 case TargetLowering::Legal:
1525 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1526 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1527 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1528 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
Chris Lattner1408c052005-12-22 05:23:45 +00001529 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1,Tmp2,
Chris Lattner5f573412005-08-26 00:23:59 +00001530 Tmp3, Tmp4, Node->getOperand(4));
1531 }
1532 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001533 }
1534 break;
1535 } else {
1536 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1537 Node->getOperand(0), // LHS
1538 Node->getOperand(1), // RHS
1539 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001540 // If we get a SETCC back from legalizing the SETCC node we just
1541 // created, then use its LHS, RHS, and CC directly in creating a new
1542 // node. Otherwise, select between the true and false value based on
1543 // comparing the result of the legalized with zero.
1544 if (Tmp1.getOpcode() == ISD::SETCC) {
1545 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1546 Tmp1.getOperand(0), Tmp1.getOperand(1),
1547 Tmp3, Tmp4, Tmp1.getOperand(2));
1548 } else {
1549 Result = DAG.getSelectCC(Tmp1,
1550 DAG.getConstant(0, Tmp1.getValueType()),
1551 Tmp3, Tmp4, ISD::SETNE);
1552 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001553 }
1554 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001555 case ISD::SETCC:
1556 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1557 case Legal:
1558 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1559 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001560 break;
1561 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001562 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1563 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1564
1565 // If this is an FP compare, the operands have already been extended.
1566 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1567 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001568 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001569
1570 // Otherwise, we have to insert explicit sign or zero extends. Note
1571 // that we could insert sign extends for ALL conditions, but zero extend
1572 // is cheaper on many machines (an AND instead of two shifts), so prefer
1573 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001574 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001575 default: assert(0 && "Unknown integer comparison!");
1576 case ISD::SETEQ:
1577 case ISD::SETNE:
1578 case ISD::SETUGE:
1579 case ISD::SETUGT:
1580 case ISD::SETULE:
1581 case ISD::SETULT:
1582 // ALL of these operations will work if we either sign or zero extend
1583 // the operands (including the unsigned comparisons!). Zero extend is
1584 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001585 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1586 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001587 break;
1588 case ISD::SETGE:
1589 case ISD::SETGT:
1590 case ISD::SETLT:
1591 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001592 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1593 DAG.getValueType(VT));
1594 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1595 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001596 break;
1597 }
Chris Lattner4d978642005-01-15 22:16:26 +00001598 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001599 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001600 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001601 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1602 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1603 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001604 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001605 case ISD::SETEQ:
1606 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001607 if (RHSLo == RHSHi)
1608 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1609 if (RHSCST->isAllOnesValue()) {
1610 // Comparison to -1.
1611 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001612 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001613 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001614 }
1615
Chris Lattnerdc750592005-01-07 07:47:09 +00001616 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1617 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1618 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001619 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001620 break;
1621 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001622 // If this is a comparison of the sign bit, just look at the top part.
1623 // X > -1, x < 0
1624 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001625 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001626 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001627 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001628 (CST->isAllOnesValue()))) { // X > -1
1629 Tmp1 = LHSHi;
1630 Tmp2 = RHSHi;
1631 break;
1632 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001633
Chris Lattnerdc750592005-01-07 07:47:09 +00001634 // FIXME: This generated code sucks.
1635 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001636 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001637 default: assert(0 && "Unknown integer setcc!");
1638 case ISD::SETLT:
1639 case ISD::SETULT: LowCC = ISD::SETULT; break;
1640 case ISD::SETGT:
1641 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1642 case ISD::SETLE:
1643 case ISD::SETULE: LowCC = ISD::SETULE; break;
1644 case ISD::SETGE:
1645 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1646 }
Misha Brukman835702a2005-04-21 22:36:52 +00001647
Chris Lattnerdc750592005-01-07 07:47:09 +00001648 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1649 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1650 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1651
1652 // NOTE: on targets without efficient SELECT of bools, we can always use
1653 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001654 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1655 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1656 Node->getOperand(2));
1657 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001658 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1659 Result, Tmp1, Tmp2));
Chris Lattner2af3ee42005-12-20 00:53:54 +00001660 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman987121a2005-08-23 04:29:48 +00001661 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001662 }
1663 }
Nate Begeman987121a2005-08-23 04:29:48 +00001664
Chris Lattner1408c052005-12-22 05:23:45 +00001665 switch(TLI.getOperationAction(ISD::SETCC,
1666 Node->getOperand(0).getValueType())) {
Nate Begeman987121a2005-08-23 04:29:48 +00001667 default:
1668 assert(0 && "Cannot handle this action for SETCC yet!");
1669 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001670 case TargetLowering::Promote: {
1671 // First step, figure out the appropriate operation to use.
1672 // Allow SETCC to not be supported for all legal data types
1673 // Mostly this targets FP
1674 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1675 MVT::ValueType OldVT = NewInTy;
1676
1677 // Scan for the appropriate larger type to use.
1678 while (1) {
1679 NewInTy = (MVT::ValueType)(NewInTy+1);
1680
1681 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1682 "Fell off of the edge of the integer world");
1683 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1684 "Fell off of the edge of the floating point world");
1685
1686 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001687 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001688 break;
1689 }
1690 if (MVT::isInteger(NewInTy))
1691 assert(0 && "Cannot promote Legal Integer SETCC yet");
1692 else {
1693 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1694 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1695 }
1696
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001697 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1698 Node->getOperand(2));
1699 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001700 }
Evan Chengc1583db2005-12-21 20:21:51 +00001701 case TargetLowering::Custom: {
1702 SDOperand Tmp =
1703 TLI.LowerOperation(DAG.getNode(ISD::SETCC, Node->getValueType(0),
1704 Tmp1, Tmp2, Node->getOperand(2)), DAG);
1705 if (Tmp.Val) {
1706 Result = LegalizeOp(Tmp);
1707 break;
1708 }
1709 // FALLTHROUGH if the target thinks it is legal.
1710 }
Nate Begeman987121a2005-08-23 04:29:48 +00001711 case TargetLowering::Legal:
1712 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1713 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1714 Node->getOperand(2));
1715 break;
1716 case TargetLowering::Expand:
1717 // Expand a setcc node into a select_cc of the same condition, lhs, and
1718 // rhs that selects between const 1 (true) and const 0 (false).
1719 MVT::ValueType VT = Node->getValueType(0);
1720 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1721 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1722 Node->getOperand(2));
1723 Result = LegalizeOp(Result);
1724 break;
1725 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001726 break;
1727
Chris Lattner85d70c62005-01-11 05:57:22 +00001728 case ISD::MEMSET:
1729 case ISD::MEMCPY:
1730 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001731 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001732 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1733
1734 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1735 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1736 case Expand: assert(0 && "Cannot expand a byte!");
1737 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001738 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001739 break;
1740 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001741 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001742 break;
1743 }
1744 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001745 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001746 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001747
1748 SDOperand Tmp4;
1749 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001750 case Expand: {
1751 // Length is too big, just take the lo-part of the length.
1752 SDOperand HiPart;
1753 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1754 break;
1755 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001756 case Legal:
1757 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001758 break;
1759 case Promote:
1760 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001761 break;
1762 }
1763
1764 SDOperand Tmp5;
1765 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1766 case Expand: assert(0 && "Cannot expand this yet!");
1767 case Legal:
1768 Tmp5 = LegalizeOp(Node->getOperand(4));
1769 break;
1770 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001771 Tmp5 = PromoteOp(Node->getOperand(4));
1772 break;
1773 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001774
1775 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1776 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001777 case TargetLowering::Custom: {
1778 SDOperand Tmp =
1779 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1780 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1781 if (Tmp.Val) {
1782 Result = LegalizeOp(Tmp);
1783 break;
1784 }
1785 // FALLTHROUGH if the target thinks it is legal.
1786 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001787 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001788 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1789 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1790 Tmp5 != Node->getOperand(4)) {
1791 std::vector<SDOperand> Ops;
1792 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1793 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1794 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1795 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001796 break;
1797 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001798 // Otherwise, the target does not support this operation. Lower the
1799 // operation to an explicit libcall as appropriate.
1800 MVT::ValueType IntPtr = TLI.getPointerTy();
1801 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1802 std::vector<std::pair<SDOperand, const Type*> > Args;
1803
Reid Spencer6dced922005-01-12 14:53:45 +00001804 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001805 if (Node->getOpcode() == ISD::MEMSET) {
1806 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1807 // Extend the ubyte argument to be an int value for the call.
1808 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1809 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1810 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1811
1812 FnName = "memset";
1813 } else if (Node->getOpcode() == ISD::MEMCPY ||
1814 Node->getOpcode() == ISD::MEMMOVE) {
1815 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1816 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1817 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1818 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1819 } else {
1820 assert(0 && "Unknown op!");
1821 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001822
Chris Lattner85d70c62005-01-11 05:57:22 +00001823 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001824 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001825 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner2af3ee42005-12-20 00:53:54 +00001826 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001827 break;
1828 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001829 }
1830 break;
1831 }
Chris Lattner5385db52005-05-09 20:23:03 +00001832
1833 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001834 Tmp1 = LegalizeOp(Node->getOperand(0));
1835 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001836
Chris Lattner86535992005-05-14 07:45:46 +00001837 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1838 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1839 std::vector<SDOperand> Ops;
1840 Ops.push_back(Tmp1);
1841 Ops.push_back(Tmp2);
1842 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1843 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001844 Result = SDOperand(Node, 0);
1845 // Since these produce two values, make sure to remember that we legalized
1846 // both of them.
1847 AddLegalizedOperand(SDOperand(Node, 0), Result);
1848 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1849 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001850 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001851 Tmp1 = LegalizeOp(Node->getOperand(0));
1852 Tmp2 = LegalizeOp(Node->getOperand(1));
1853 Tmp3 = LegalizeOp(Node->getOperand(2));
1854 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1855 Tmp3 != Node->getOperand(2))
1856 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1857 break;
1858
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001859 case ISD::READIO:
1860 Tmp1 = LegalizeOp(Node->getOperand(0));
1861 Tmp2 = LegalizeOp(Node->getOperand(1));
1862
1863 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1864 case TargetLowering::Custom:
1865 default: assert(0 && "This action not implemented for this operation!");
1866 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001867 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1868 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1869 std::vector<SDOperand> Ops;
1870 Ops.push_back(Tmp1);
1871 Ops.push_back(Tmp2);
1872 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1873 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001874 Result = SDOperand(Node, 0);
1875 break;
1876 case TargetLowering::Expand:
1877 // Replace this with a load from memory.
1878 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1879 Node->getOperand(1), DAG.getSrcValue(NULL));
1880 Result = LegalizeOp(Result);
1881 break;
1882 }
1883
1884 // Since these produce two values, make sure to remember that we legalized
1885 // both of them.
1886 AddLegalizedOperand(SDOperand(Node, 0), Result);
1887 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1888 return Result.getValue(Op.ResNo);
1889
1890 case ISD::WRITEIO:
1891 Tmp1 = LegalizeOp(Node->getOperand(0));
1892 Tmp2 = LegalizeOp(Node->getOperand(1));
1893 Tmp3 = LegalizeOp(Node->getOperand(2));
1894
1895 switch (TLI.getOperationAction(Node->getOpcode(),
1896 Node->getOperand(1).getValueType())) {
1897 case TargetLowering::Custom:
1898 default: assert(0 && "This action not implemented for this operation!");
1899 case TargetLowering::Legal:
1900 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1901 Tmp3 != Node->getOperand(2))
1902 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1903 break;
1904 case TargetLowering::Expand:
1905 // Replace this with a store to memory.
1906 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1907 Node->getOperand(1), Node->getOperand(2),
1908 DAG.getSrcValue(NULL));
1909 Result = LegalizeOp(Result);
1910 break;
1911 }
1912 break;
1913
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001914 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001915 case ISD::SUB_PARTS:
1916 case ISD::SHL_PARTS:
1917 case ISD::SRA_PARTS:
1918 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001919 std::vector<SDOperand> Ops;
1920 bool Changed = false;
1921 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1922 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1923 Changed |= Ops.back() != Node->getOperand(i);
1924 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001925 if (Changed) {
1926 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1927 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1928 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001929
1930 // Since these produce multiple values, make sure to remember that we
1931 // legalized all of them.
1932 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1933 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1934 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001935 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001936
1937 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001938 case ISD::ADD:
1939 case ISD::SUB:
1940 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001941 case ISD::MULHS:
1942 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001943 case ISD::UDIV:
1944 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001945 case ISD::AND:
1946 case ISD::OR:
1947 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001948 case ISD::SHL:
1949 case ISD::SRL:
1950 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001951 case ISD::FADD:
1952 case ISD::FSUB:
1953 case ISD::FMUL:
1954 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001955 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001956 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1957 case Expand: assert(0 && "Not possible");
1958 case Legal:
1959 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1960 break;
1961 case Promote:
1962 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1963 break;
1964 }
Andrew Lenharth72594262005-12-24 23:42:32 +00001965 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth72594262005-12-24 23:42:32 +00001966 case TargetLowering::Custom: {
1967 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2);
1968 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1969 if (Tmp.Val) {
1970 Tmp = LegalizeOp(Tmp); // Relegalize input.
1971 AddLegalizedOperand(Op, Tmp);
1972 return Tmp;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001973 } //else it was considered legal and we fall through
Andrew Lenharth72594262005-12-24 23:42:32 +00001974 }
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001975 case TargetLowering::Legal:
1976 if (Tmp1 != Node->getOperand(0) ||
1977 Tmp2 != Node->getOperand(1))
1978 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1979 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001980 default:
1981 assert(0 && "Operation not supported");
1982 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001983 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001984
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001985 case ISD::BUILD_PAIR: {
1986 MVT::ValueType PairTy = Node->getValueType(0);
1987 // TODO: handle the case where the Lo and Hi operands are not of legal type
1988 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1989 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1990 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1991 case TargetLowering::Legal:
1992 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1993 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1994 break;
1995 case TargetLowering::Promote:
1996 case TargetLowering::Custom:
1997 assert(0 && "Cannot promote/custom this yet!");
1998 case TargetLowering::Expand:
1999 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2000 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2001 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2002 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2003 TLI.getShiftAmountTy()));
2004 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
2005 break;
2006 }
2007 break;
2008 }
2009
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002010 case ISD::UREM:
2011 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002012 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002013 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2014 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2015 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002016 case TargetLowering::Custom: {
2017 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2);
2018 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2019 if (Tmp.Val) {
2020 Tmp = LegalizeOp(Tmp); // Relegalize input.
2021 AddLegalizedOperand(Op, Tmp);
2022 return Tmp;
2023 } //else it was considered legal and we fall through
2024 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002025 case TargetLowering::Legal:
2026 if (Tmp1 != Node->getOperand(0) ||
2027 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00002028 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002029 Tmp2);
2030 break;
2031 case TargetLowering::Promote:
Andrew Lenharth72594262005-12-24 23:42:32 +00002032 assert(0 && "Cannot promote handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00002033 case TargetLowering::Expand:
2034 if (MVT::isInteger(Node->getValueType(0))) {
2035 MVT::ValueType VT = Node->getValueType(0);
2036 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
2037 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2038 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2039 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2040 } else {
2041 // Floating point mod -> fmod libcall.
2042 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2043 SDOperand Dummy;
2044 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002045 }
2046 break;
2047 }
2048 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002049
Andrew Lenharth5e177822005-05-03 17:19:30 +00002050 case ISD::CTPOP:
2051 case ISD::CTTZ:
2052 case ISD::CTLZ:
2053 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2054 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2055 case TargetLowering::Legal:
2056 if (Tmp1 != Node->getOperand(0))
2057 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2058 break;
2059 case TargetLowering::Promote: {
2060 MVT::ValueType OVT = Tmp1.getValueType();
2061 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002062
2063 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002064 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2065 // Perform the larger operation, then subtract if needed.
2066 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2067 switch(Node->getOpcode())
2068 {
2069 case ISD::CTPOP:
2070 Result = Tmp1;
2071 break;
2072 case ISD::CTTZ:
2073 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002074 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2075 DAG.getConstant(getSizeInBits(NVT), NVT),
2076 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002077 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002078 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2079 break;
2080 case ISD::CTLZ:
2081 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002082 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2083 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002084 getSizeInBits(OVT), NVT));
2085 break;
2086 }
2087 break;
2088 }
2089 case TargetLowering::Custom:
2090 assert(0 && "Cannot custom handle this yet!");
2091 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002092 switch(Node->getOpcode())
2093 {
2094 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00002095 static const uint64_t mask[6] = {
2096 0x5555555555555555ULL, 0x3333333333333333ULL,
2097 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2098 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2099 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002100 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00002101 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2102 unsigned len = getSizeInBits(VT);
2103 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002104 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00002105 Tmp2 = DAG.getConstant(mask[i], VT);
2106 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002107 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002108 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
2109 DAG.getNode(ISD::AND, VT,
2110 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
2111 Tmp2));
2112 }
2113 Result = Tmp1;
2114 break;
2115 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002116 case ISD::CTLZ: {
2117 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00002118 x = x | (x >> 1);
2119 x = x | (x >> 2);
2120 ...
2121 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002122 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00002123 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002124
Chris Lattner56add052005-05-11 18:35:21 +00002125 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2126 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002127 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2128 unsigned len = getSizeInBits(VT);
2129 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2130 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002131 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002132 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2133 }
2134 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00002135 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00002136 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002137 }
2138 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002139 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002140 // unless the target has ctlz but not ctpop, in which case we use:
2141 // { return 32 - nlz(~x & (x-1)); }
2142 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00002143 MVT::ValueType VT = Tmp1.getValueType();
2144 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002145 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00002146 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2147 DAG.getNode(ISD::SUB, VT, Tmp1,
2148 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002149 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002150 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2151 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002152 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002153 DAG.getConstant(getSizeInBits(VT), VT),
2154 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2155 } else {
2156 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2157 }
Chris Lattner72473242005-05-11 05:27:09 +00002158 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002159 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002160 default:
2161 assert(0 && "Cannot expand this yet!");
2162 break;
2163 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002164 break;
2165 }
2166 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002167
Chris Lattner13fe99c2005-04-02 05:00:07 +00002168 // Unary operators
2169 case ISD::FABS:
2170 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002171 case ISD::FSQRT:
2172 case ISD::FSIN:
2173 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002174 Tmp1 = LegalizeOp(Node->getOperand(0));
2175 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2176 case TargetLowering::Legal:
2177 if (Tmp1 != Node->getOperand(0))
2178 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2179 break;
2180 case TargetLowering::Promote:
2181 case TargetLowering::Custom:
2182 assert(0 && "Cannot promote/custom handle this yet!");
2183 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00002184 switch(Node->getOpcode()) {
2185 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00002186 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2187 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00002188 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00002189 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00002190 break;
2191 }
2192 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002193 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2194 MVT::ValueType VT = Node->getValueType(0);
2195 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002196 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002197 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2198 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2199 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00002200 break;
2201 }
2202 case ISD::FSQRT:
2203 case ISD::FSIN:
2204 case ISD::FCOS: {
2205 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002206 const char *FnName = 0;
2207 switch(Node->getOpcode()) {
2208 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2209 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2210 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2211 default: assert(0 && "Unreachable!");
2212 }
Nate Begeman77558da2005-08-04 21:43:28 +00002213 SDOperand Dummy;
2214 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002215 break;
2216 }
2217 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002218 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002219 }
2220 break;
2221 }
2222 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002223
2224 case ISD::BIT_CONVERT:
2225 if (!isTypeLegal(Node->getOperand(0).getValueType()))
2226 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2227 else {
2228 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2229 Node->getOperand(0).getValueType())) {
2230 default: assert(0 && "Unknown operation action!");
2231 case TargetLowering::Expand:
2232 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2233 break;
2234 case TargetLowering::Legal:
2235 Tmp1 = LegalizeOp(Node->getOperand(0));
2236 if (Tmp1 != Node->getOperand(0))
2237 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Tmp1);
2238 break;
2239 }
2240 }
2241 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002242 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002243 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002244 case ISD::UINT_TO_FP: {
2245 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002246 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2247 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002248 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002249 Node->getOperand(0).getValueType())) {
2250 default: assert(0 && "Unknown operation action!");
2251 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002252 Result = ExpandLegalINT_TO_FP(isSigned,
2253 LegalizeOp(Node->getOperand(0)),
2254 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002255 AddLegalizedOperand(Op, Result);
2256 return Result;
2257 case TargetLowering::Promote:
2258 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2259 Node->getValueType(0),
2260 isSigned);
2261 AddLegalizedOperand(Op, Result);
2262 return Result;
2263 case TargetLowering::Legal:
2264 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002265 case TargetLowering::Custom: {
2266 Tmp1 = LegalizeOp(Node->getOperand(0));
2267 SDOperand Tmp =
2268 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2269 Tmp = TLI.LowerOperation(Tmp, DAG);
2270 if (Tmp.Val) {
Chris Lattner2af3ee42005-12-20 00:53:54 +00002271 Tmp = LegalizeOp(Tmp); // Relegalize input.
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002272 AddLegalizedOperand(Op, Tmp);
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002273 return Tmp;
2274 } else {
2275 assert(0 && "Target Must Lower this");
2276 }
2277 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002278 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002279
Chris Lattnerdc750592005-01-07 07:47:09 +00002280 Tmp1 = LegalizeOp(Node->getOperand(0));
2281 if (Tmp1 != Node->getOperand(0))
2282 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2283 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002284 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002285 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2286 Node->getValueType(0), Node->getOperand(0));
2287 break;
2288 case Promote:
2289 if (isSigned) {
2290 Result = PromoteOp(Node->getOperand(0));
2291 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2292 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2293 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2294 } else {
2295 Result = PromoteOp(Node->getOperand(0));
2296 Result = DAG.getZeroExtendInReg(Result,
2297 Node->getOperand(0).getValueType());
2298 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002299 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002300 break;
2301 }
2302 break;
2303 }
2304 case ISD::TRUNCATE:
2305 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2306 case Legal:
2307 Tmp1 = LegalizeOp(Node->getOperand(0));
2308 if (Tmp1 != Node->getOperand(0))
2309 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2310 break;
2311 case Expand:
2312 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2313
2314 // Since the result is legal, we should just be able to truncate the low
2315 // part of the source.
2316 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2317 break;
2318 case Promote:
2319 Result = PromoteOp(Node->getOperand(0));
2320 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2321 break;
2322 }
2323 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002324
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002325 case ISD::FP_TO_SINT:
2326 case ISD::FP_TO_UINT:
2327 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2328 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002329 Tmp1 = LegalizeOp(Node->getOperand(0));
2330
Chris Lattner44fe26f2005-07-29 00:11:56 +00002331 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2332 default: assert(0 && "Unknown operation action!");
2333 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002334 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2335 SDOperand True, False;
2336 MVT::ValueType VT = Node->getOperand(0).getValueType();
2337 MVT::ValueType NVT = Node->getValueType(0);
2338 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2339 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2340 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2341 Node->getOperand(0), Tmp2, ISD::SETLT);
2342 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2343 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002344 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002345 Tmp2));
2346 False = DAG.getNode(ISD::XOR, NVT, False,
2347 DAG.getConstant(1ULL << ShiftAmt, NVT));
2348 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Chris Lattner2af3ee42005-12-20 00:53:54 +00002349 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemand5e739d2005-08-14 18:38:32 +00002350 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002351 } else {
2352 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2353 }
2354 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002355 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002356 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002357 Node->getOpcode() == ISD::FP_TO_SINT);
2358 AddLegalizedOperand(Op, Result);
2359 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002360 case TargetLowering::Custom: {
2361 SDOperand Tmp =
2362 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2363 Tmp = TLI.LowerOperation(Tmp, DAG);
2364 if (Tmp.Val) {
Chris Lattner2af3ee42005-12-20 00:53:54 +00002365 Tmp = LegalizeOp(Tmp);
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002366 AddLegalizedOperand(Op, Tmp);
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002367 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002368 } else {
2369 // The target thinks this is legal afterall.
2370 break;
2371 }
2372 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002373 case TargetLowering::Legal:
2374 break;
2375 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002376
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002377 if (Tmp1 != Node->getOperand(0))
2378 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2379 break;
2380 case Expand:
2381 assert(0 && "Shouldn't need to expand other operators here!");
2382 case Promote:
2383 Result = PromoteOp(Node->getOperand(0));
2384 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2385 break;
2386 }
2387 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002388
Chris Lattner7753f172005-09-02 00:18:10 +00002389 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002390 case ISD::ZERO_EXTEND:
2391 case ISD::SIGN_EXTEND:
2392 case ISD::FP_EXTEND:
2393 case ISD::FP_ROUND:
2394 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2395 case Legal:
2396 Tmp1 = LegalizeOp(Node->getOperand(0));
2397 if (Tmp1 != Node->getOperand(0))
2398 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2399 break;
2400 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002401 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002402
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002403 case Promote:
2404 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002405 case ISD::ANY_EXTEND:
2406 Result = PromoteOp(Node->getOperand(0));
2407 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2408 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002409 case ISD::ZERO_EXTEND:
2410 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002411 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002412 Result = DAG.getZeroExtendInReg(Result,
2413 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002414 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002415 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002416 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002417 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002418 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002419 Result,
2420 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002421 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002422 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002423 Result = PromoteOp(Node->getOperand(0));
2424 if (Result.getValueType() != Op.getValueType())
2425 // Dynamically dead while we have only 2 FP types.
2426 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2427 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002428 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002429 Result = PromoteOp(Node->getOperand(0));
2430 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2431 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002432 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002433 }
2434 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002435 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002436 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002437 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002438 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002439
2440 // If this operation is not supported, convert it to a shl/shr or load/store
2441 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002442 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2443 default: assert(0 && "This action not supported for this op yet!");
2444 case TargetLowering::Legal:
2445 if (Tmp1 != Node->getOperand(0))
2446 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002447 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002448 break;
2449 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002450 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002451 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002452 // NOTE: we could fall back on load/store here too for targets without
2453 // SAR. However, it is doubtful that any exist.
2454 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2455 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002456 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002457 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2458 Node->getOperand(0), ShiftCst);
2459 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2460 Result, ShiftCst);
2461 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2462 // The only way we can lower this is to turn it into a STORETRUNC,
2463 // EXTLOAD pair, targetting a temporary location (a stack slot).
2464
2465 // NOTE: there is a choice here between constantly creating new stack
2466 // slots and always reusing the same one. We currently always create
2467 // new ones, as reuse may inhibit scheduling.
2468 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2469 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2470 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2471 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002472 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002473 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2474 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2475 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002476 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002477 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002478 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2479 Result, StackSlot, DAG.getSrcValue(NULL),
2480 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002481 } else {
2482 assert(0 && "Unknown op");
2483 }
2484 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002485 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002486 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002487 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002488 }
Chris Lattner99222f72005-01-15 07:15:18 +00002489 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002490
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002491 // Note that LegalizeOp may be reentered even from single-use nodes, which
2492 // means that we always must cache transformed nodes.
2493 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002494 return Result;
2495}
2496
Chris Lattner4d978642005-01-15 22:16:26 +00002497/// PromoteOp - Given an operation that produces a value in an invalid type,
2498/// promote it to compute the value into a larger type. The produced value will
2499/// have the correct bits for the low portion of the register, but no guarantee
2500/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002501SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2502 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002503 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002504 assert(getTypeAction(VT) == Promote &&
2505 "Caller should expand or legalize operands that are not promotable!");
2506 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2507 "Cannot promote to smaller type!");
2508
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002509 SDOperand Tmp1, Tmp2, Tmp3;
2510
2511 SDOperand Result;
2512 SDNode *Node = Op.Val;
2513
Chris Lattner1a570f12005-09-02 20:32:45 +00002514 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2515 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002516
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002517 // Promotion needs an optimization step to clean up after it, and is not
2518 // careful to avoid operations the target does not support. Make sure that
2519 // all generated operations are legalized in the next iteration.
2520 NeedsAnotherIteration = true;
2521
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002522 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002523 case ISD::CopyFromReg:
2524 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002525 default:
2526 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2527 assert(0 && "Do not know how to promote this operator!");
2528 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002529 case ISD::UNDEF:
2530 Result = DAG.getNode(ISD::UNDEF, NVT);
2531 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002532 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002533 if (VT != MVT::i1)
2534 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2535 else
2536 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002537 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2538 break;
2539 case ISD::ConstantFP:
2540 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2541 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2542 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002543
Chris Lattner2cb338d2005-01-18 02:59:52 +00002544 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002545 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002546 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2547 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002548 Result = LegalizeOp(Result);
2549 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002550
2551 case ISD::TRUNCATE:
2552 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2553 case Legal:
2554 Result = LegalizeOp(Node->getOperand(0));
2555 assert(Result.getValueType() >= NVT &&
2556 "This truncation doesn't make sense!");
2557 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2558 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2559 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002560 case Promote:
2561 // The truncation is not required, because we don't guarantee anything
2562 // about high bits anyway.
2563 Result = PromoteOp(Node->getOperand(0));
2564 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002565 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002566 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2567 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002568 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002569 }
2570 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002571 case ISD::SIGN_EXTEND:
2572 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002573 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002574 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2575 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2576 case Legal:
2577 // Input is legal? Just do extend all the way to the larger type.
2578 Result = LegalizeOp(Node->getOperand(0));
2579 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2580 break;
2581 case Promote:
2582 // Promote the reg if it's smaller.
2583 Result = PromoteOp(Node->getOperand(0));
2584 // The high bits are not guaranteed to be anything. Insert an extend.
2585 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002586 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002587 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002588 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002589 Result = DAG.getZeroExtendInReg(Result,
2590 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002591 break;
2592 }
2593 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002594 case ISD::BIT_CONVERT:
2595 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2596 Result = PromoteOp(Result);
2597 break;
2598
Chris Lattner4d978642005-01-15 22:16:26 +00002599 case ISD::FP_EXTEND:
2600 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2601 case ISD::FP_ROUND:
2602 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2603 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2604 case Promote: assert(0 && "Unreachable with 2 FP types!");
2605 case Legal:
2606 // Input is legal? Do an FP_ROUND_INREG.
2607 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002608 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2609 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002610 break;
2611 }
2612 break;
2613
2614 case ISD::SINT_TO_FP:
2615 case ISD::UINT_TO_FP:
2616 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2617 case Legal:
2618 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002619 // No extra round required here.
2620 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002621 break;
2622
2623 case Promote:
2624 Result = PromoteOp(Node->getOperand(0));
2625 if (Node->getOpcode() == ISD::SINT_TO_FP)
2626 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002627 Result,
2628 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002629 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002630 Result = DAG.getZeroExtendInReg(Result,
2631 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002632 // No extra round required here.
2633 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002634 break;
2635 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002636 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2637 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002638 // Round if we cannot tolerate excess precision.
2639 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002640 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2641 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002642 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002643 }
Chris Lattner4d978642005-01-15 22:16:26 +00002644 break;
2645
Chris Lattner268d4572005-12-09 17:32:47 +00002646 case ISD::SIGN_EXTEND_INREG:
2647 Result = PromoteOp(Node->getOperand(0));
2648 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2649 Node->getOperand(1));
2650 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002651 case ISD::FP_TO_SINT:
2652 case ISD::FP_TO_UINT:
2653 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2654 case Legal:
2655 Tmp1 = LegalizeOp(Node->getOperand(0));
2656 break;
2657 case Promote:
2658 // The input result is prerounded, so we don't have to do anything
2659 // special.
2660 Tmp1 = PromoteOp(Node->getOperand(0));
2661 break;
2662 case Expand:
2663 assert(0 && "not implemented");
2664 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002665 // If we're promoting a UINT to a larger size, check to see if the new node
2666 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2667 // we can use that instead. This allows us to generate better code for
2668 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2669 // legal, such as PowerPC.
2670 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002671 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002672 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2673 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002674 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2675 } else {
2676 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2677 }
Chris Lattner4d978642005-01-15 22:16:26 +00002678 break;
2679
Chris Lattner13fe99c2005-04-02 05:00:07 +00002680 case ISD::FABS:
2681 case ISD::FNEG:
2682 Tmp1 = PromoteOp(Node->getOperand(0));
2683 assert(Tmp1.getValueType() == NVT);
2684 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2685 // NOTE: we do not have to do any extra rounding here for
2686 // NoExcessFPPrecision, because we know the input will have the appropriate
2687 // precision, and these operations don't modify precision at all.
2688 break;
2689
Chris Lattner9d6fa982005-04-28 21:44:33 +00002690 case ISD::FSQRT:
2691 case ISD::FSIN:
2692 case ISD::FCOS:
2693 Tmp1 = PromoteOp(Node->getOperand(0));
2694 assert(Tmp1.getValueType() == NVT);
2695 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2696 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002697 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2698 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002699 break;
2700
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002701 case ISD::AND:
2702 case ISD::OR:
2703 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002704 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002705 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002706 case ISD::MUL:
2707 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002708 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002709 // that too is okay if they are integer operations.
2710 Tmp1 = PromoteOp(Node->getOperand(0));
2711 Tmp2 = PromoteOp(Node->getOperand(1));
2712 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2713 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002714 break;
2715 case ISD::FADD:
2716 case ISD::FSUB:
2717 case ISD::FMUL:
2718 // The input may have strange things in the top bits of the registers, but
2719 // these operations don't care.
2720 Tmp1 = PromoteOp(Node->getOperand(0));
2721 Tmp2 = PromoteOp(Node->getOperand(1));
2722 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2723 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2724
2725 // Floating point operations will give excess precision that we may not be
2726 // able to tolerate. If we DO allow excess precision, just leave it,
2727 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002728 // FIXME: Why would we need to round FP ops more than integer ones?
2729 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002730 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002731 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2732 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002733 break;
2734
Chris Lattner4d978642005-01-15 22:16:26 +00002735 case ISD::SDIV:
2736 case ISD::SREM:
2737 // These operators require that their input be sign extended.
2738 Tmp1 = PromoteOp(Node->getOperand(0));
2739 Tmp2 = PromoteOp(Node->getOperand(1));
2740 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002741 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2742 DAG.getValueType(VT));
2743 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2744 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002745 }
2746 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2747
2748 // Perform FP_ROUND: this is probably overly pessimistic.
2749 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002750 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2751 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002752 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002753 case ISD::FDIV:
2754 case ISD::FREM:
2755 // These operators require that their input be fp extended.
2756 Tmp1 = PromoteOp(Node->getOperand(0));
2757 Tmp2 = PromoteOp(Node->getOperand(1));
2758 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2759
2760 // Perform FP_ROUND: this is probably overly pessimistic.
2761 if (NoExcessFPPrecision)
2762 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2763 DAG.getValueType(VT));
2764 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002765
2766 case ISD::UDIV:
2767 case ISD::UREM:
2768 // These operators require that their input be zero extended.
2769 Tmp1 = PromoteOp(Node->getOperand(0));
2770 Tmp2 = PromoteOp(Node->getOperand(1));
2771 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002772 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2773 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002774 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2775 break;
2776
2777 case ISD::SHL:
2778 Tmp1 = PromoteOp(Node->getOperand(0));
2779 Tmp2 = LegalizeOp(Node->getOperand(1));
2780 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2781 break;
2782 case ISD::SRA:
2783 // The input value must be properly sign extended.
2784 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002785 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2786 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002787 Tmp2 = LegalizeOp(Node->getOperand(1));
2788 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2789 break;
2790 case ISD::SRL:
2791 // The input value must be properly zero extended.
2792 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002793 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002794 Tmp2 = LegalizeOp(Node->getOperand(1));
2795 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2796 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002797 case ISD::LOAD:
2798 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2799 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002800 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2801 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002802 // Remember that we legalized the chain.
2803 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2804 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002805 case ISD::SEXTLOAD:
2806 case ISD::ZEXTLOAD:
2807 case ISD::EXTLOAD:
2808 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2809 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002810 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2811 Node->getOperand(2),
2812 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002813 // Remember that we legalized the chain.
2814 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2815 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002816 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002817 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2818 case Expand: assert(0 && "It's impossible to expand bools");
2819 case Legal:
2820 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2821 break;
2822 case Promote:
2823 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2824 break;
2825 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002826 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2827 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2828 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2829 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002830 case ISD::SELECT_CC:
2831 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2832 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2833 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2834 Node->getOperand(1), Tmp2, Tmp3,
2835 Node->getOperand(4));
2836 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002837 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002838 case ISD::CALL: {
2839 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2840 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2841
Chris Lattner3d95c142005-01-19 20:24:35 +00002842 std::vector<SDOperand> Ops;
2843 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2844 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2845
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002846 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2847 "Can only promote single result calls");
2848 std::vector<MVT::ValueType> RetTyVTs;
2849 RetTyVTs.reserve(2);
2850 RetTyVTs.push_back(NVT);
2851 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002852 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2853 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002854 Result = SDOperand(NC, 0);
2855
2856 // Insert the new chain mapping.
2857 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2858 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002859 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002860 case ISD::CTPOP:
2861 case ISD::CTTZ:
2862 case ISD::CTLZ:
2863 Tmp1 = Node->getOperand(0);
2864 //Zero extend the argument
2865 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2866 // Perform the larger operation, then subtract if needed.
2867 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2868 switch(Node->getOpcode())
2869 {
2870 case ISD::CTPOP:
2871 Result = Tmp1;
2872 break;
2873 case ISD::CTTZ:
2874 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002875 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002876 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002877 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002878 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2879 break;
2880 case ISD::CTLZ:
2881 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002882 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2883 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002884 getSizeInBits(VT), NVT));
2885 break;
2886 }
2887 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002888 }
2889
2890 assert(Result.Val && "Didn't set a result!");
2891 AddPromotedOperand(Op, Result);
2892 return Result;
2893}
Chris Lattnerdc750592005-01-07 07:47:09 +00002894
Chris Lattner36e663d2005-12-23 00:16:34 +00002895/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002896/// The resultant code need not be legal. Note that SrcOp is the input operand
2897/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00002898SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2899 SDOperand SrcOp) {
2900 // Create the stack frame object.
2901 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2902 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002903 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner36e663d2005-12-23 00:16:34 +00002904 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2905
2906 // Emit a store to the stack slot.
2907 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00002908 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00002909 // Result is a load from the stack slot.
2910 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2911}
2912
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002913/// ExpandAddSub - Find a clever way to expand this add operation into
2914/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002915void SelectionDAGLegalize::
2916ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2917 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002918 // Expand the subcomponents.
2919 SDOperand LHSL, LHSH, RHSL, RHSH;
2920 ExpandOp(LHS, LHSL, LHSH);
2921 ExpandOp(RHS, RHSL, RHSH);
2922
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002923 std::vector<SDOperand> Ops;
2924 Ops.push_back(LHSL);
2925 Ops.push_back(LHSH);
2926 Ops.push_back(RHSL);
2927 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002928 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2929 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002930 Hi = Lo.getValue(1);
2931}
2932
Chris Lattner4157c412005-04-02 04:00:59 +00002933void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2934 SDOperand Op, SDOperand Amt,
2935 SDOperand &Lo, SDOperand &Hi) {
2936 // Expand the subcomponents.
2937 SDOperand LHSL, LHSH;
2938 ExpandOp(Op, LHSL, LHSH);
2939
2940 std::vector<SDOperand> Ops;
2941 Ops.push_back(LHSL);
2942 Ops.push_back(LHSH);
2943 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002944 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002945 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002946 Hi = Lo.getValue(1);
2947}
2948
2949
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002950/// ExpandShift - Try to find a clever way to expand this shift operation out to
2951/// smaller elements. If we can't find a way that is more efficient than a
2952/// libcall on this target, return false. Otherwise, return true with the
2953/// low-parts expanded into Lo and Hi.
2954bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2955 SDOperand &Lo, SDOperand &Hi) {
2956 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2957 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002958
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002959 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002960 SDOperand ShAmt = LegalizeOp(Amt);
2961 MVT::ValueType ShTy = ShAmt.getValueType();
2962 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2963 unsigned NVTBits = MVT::getSizeInBits(NVT);
2964
2965 // Handle the case when Amt is an immediate. Other cases are currently broken
2966 // and are disabled.
2967 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2968 unsigned Cst = CN->getValue();
2969 // Expand the incoming operand to be shifted, so that we have its parts
2970 SDOperand InL, InH;
2971 ExpandOp(Op, InL, InH);
2972 switch(Opc) {
2973 case ISD::SHL:
2974 if (Cst > VTBits) {
2975 Lo = DAG.getConstant(0, NVT);
2976 Hi = DAG.getConstant(0, NVT);
2977 } else if (Cst > NVTBits) {
2978 Lo = DAG.getConstant(0, NVT);
2979 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002980 } else if (Cst == NVTBits) {
2981 Lo = DAG.getConstant(0, NVT);
2982 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002983 } else {
2984 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2985 Hi = DAG.getNode(ISD::OR, NVT,
2986 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2987 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2988 }
2989 return true;
2990 case ISD::SRL:
2991 if (Cst > VTBits) {
2992 Lo = DAG.getConstant(0, NVT);
2993 Hi = DAG.getConstant(0, NVT);
2994 } else if (Cst > NVTBits) {
2995 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2996 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002997 } else if (Cst == NVTBits) {
2998 Lo = InH;
2999 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003000 } else {
3001 Lo = DAG.getNode(ISD::OR, NVT,
3002 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3003 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3004 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3005 }
3006 return true;
3007 case ISD::SRA:
3008 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003009 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003010 DAG.getConstant(NVTBits-1, ShTy));
3011 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003012 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003013 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003014 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003015 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003016 } else if (Cst == NVTBits) {
3017 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003018 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003019 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003020 } else {
3021 Lo = DAG.getNode(ISD::OR, NVT,
3022 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3023 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3024 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3025 }
3026 return true;
3027 }
3028 }
3029 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
3030 // so disable it for now. Currently targets are handling this via SHL_PARTS
3031 // and friends.
3032 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003033
3034 // If we have an efficient select operation (or if the selects will all fold
3035 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003036 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003037 return false;
3038
3039 SDOperand InL, InH;
3040 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003041 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
3042 DAG.getConstant(NVTBits, ShTy), ShAmt);
3043
Chris Lattner4d25c042005-01-20 20:29:23 +00003044 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00003045 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
3046 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00003047
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003048 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
3049 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
3050 DAG.getConstant(NVTBits-1, ShTy));
3051 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
3052 DAG.getConstant(NVTBits-1, ShTy));
3053 }
3054
3055 if (Opc == ISD::SHL) {
3056 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
3057 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
3058 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00003059 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00003060
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003061 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
3062 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
3063 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00003064 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00003065 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
3066 DAG.getConstant(32, ShTy),
3067 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00003068 DAG.getConstant(0, NVT),
3069 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003070 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00003071 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003072 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00003073 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003074
3075 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00003076 if (Opc == ISD::SRA)
3077 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
3078 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003079 else
3080 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003081 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00003082 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003083 }
3084 return true;
3085}
Chris Lattneraac464e2005-01-21 06:05:23 +00003086
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003087/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
3088/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00003089/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003090static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003091 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00003092
Chris Lattner2dce7032005-05-12 23:24:06 +00003093 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00003094 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00003095 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003096 Found = Node;
3097 return;
3098 }
3099
3100 // Otherwise, scan the operands of Node to see if any of them is a call.
3101 assert(Node->getNumOperands() != 0 &&
3102 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00003103 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003104 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00003105
3106 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003107 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00003108 Found);
3109}
3110
3111
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003112/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
3113/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00003114/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00003115static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
3116 std::set<SDNode*> &Visited) {
3117 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
3118 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00003119
Chris Lattner2dce7032005-05-12 23:24:06 +00003120 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00003121 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00003122 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003123 Found = Node;
3124 return;
3125 }
3126
3127 // Otherwise, scan the operands of Node to see if any of them is a call.
3128 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
3129 if (UI == E) return;
3130 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00003131 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003132
3133 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00003134 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003135}
3136
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003137/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003138/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003139static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00003140 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00003141 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00003142 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003143 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00003144
Chris Lattner4add7e32005-01-23 04:42:50 +00003145 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00003146 if (TheChain.getValueType() != MVT::Other)
3147 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00003148 if (TheChain.getValueType() != MVT::Other)
3149 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00003150
3151 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00003152 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00003153
Chris Lattner4add7e32005-01-23 04:42:50 +00003154 // Make sure to only follow users of our token chain.
3155 SDNode *User = *UI;
3156 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3157 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00003158 if (SDNode *Result = FindCallSeqEnd(User))
3159 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00003160 }
Chris Lattnercabdc342005-08-05 16:23:57 +00003161 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00003162}
3163
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003164/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003165/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003166static SDNode *FindCallSeqStart(SDNode *Node) {
3167 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00003168 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003169
3170 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3171 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003172 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003173}
3174
3175
Chris Lattner4add7e32005-01-23 04:42:50 +00003176/// FindInputOutputChains - If we are replacing an operation with a call we need
3177/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00003178/// properly serialize the calls in the block. The returned operand is the
3179/// input chain value for the new call (e.g. the entry node or the previous
3180/// call), and OutChain is set to be the chain node to update to point to the
3181/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00003182static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3183 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003184 SDNode *LatestCallSeqStart = Entry.Val;
3185 SDNode *LatestCallSeqEnd = 0;
3186 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
3187 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00003188
Chris Lattner2dce7032005-05-12 23:24:06 +00003189 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00003190 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00003191 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3192 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00003193 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003194 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00003195 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3196 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003197 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00003198 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003199 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00003200
Chris Lattner06bbeb62005-05-11 19:02:11 +00003201 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003202 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003203 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00003204 std::set<SDNode*> Visited;
3205 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003206
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003207 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003208 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003209 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00003210
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003211 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00003212}
3213
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003214/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00003215void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3216 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00003217 // Nothing to splice it into?
3218 if (OutChain == 0) return;
3219
3220 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3221 //OutChain->dump();
3222
3223 // Form a token factor node merging the old inval and the new inval.
3224 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3225 OutChain->getOperand(0));
3226 // Change the node to refer to the new token.
3227 OutChain->setAdjCallChain(InToken);
3228}
Chris Lattner4add7e32005-01-23 04:42:50 +00003229
3230
Chris Lattneraac464e2005-01-21 06:05:23 +00003231// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3232// does not fit into a register, return the lo part and set the hi part to the
3233// by-reg argument. If it does fit into a single register, return the result
3234// and leave the Hi part unset.
3235SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3236 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003237 SDNode *OutChain;
3238 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3239 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00003240 if (InChain.Val == 0)
3241 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00003242
Chris Lattneraac464e2005-01-21 06:05:23 +00003243 TargetLowering::ArgListTy Args;
3244 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3245 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3246 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3247 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3248 }
3249 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003250
Chris Lattner06bbeb62005-05-11 19:02:11 +00003251 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003252 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003253 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003254 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3255 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003256
Chris Lattner63022662005-09-02 20:26:58 +00003257 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003258 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003259 default: assert(0 && "Unknown thing");
3260 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00003261 Result = CallInfo.first;
3262 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003263 case Promote:
3264 assert(0 && "Cannot promote this yet!");
3265 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003266 ExpandOp(CallInfo.first, Result, Hi);
3267 CallInfo.second = LegalizeOp(CallInfo.second);
3268 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003269 }
Chris Lattner63022662005-09-02 20:26:58 +00003270
3271 SpliceCallInto(CallInfo.second, OutChain);
3272 NeedsAnotherIteration = true;
3273 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003274}
3275
Chris Lattner4add7e32005-01-23 04:42:50 +00003276
Chris Lattneraac464e2005-01-21 06:05:23 +00003277/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3278/// destination type is legal.
3279SDOperand SelectionDAGLegalize::
3280ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003281 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003282 assert(getTypeAction(Source.getValueType()) == Expand &&
3283 "This is not an expansion!");
3284 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3285
Chris Lattner06bbeb62005-05-11 19:02:11 +00003286 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003287 assert(Source.getValueType() == MVT::i64 &&
3288 "This only works for 64-bit -> FP");
3289 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3290 // incoming integer is set. To handle this, we dynamically test to see if
3291 // it is set, and, if so, add a fudge factor.
3292 SDOperand Lo, Hi;
3293 ExpandOp(Source, Lo, Hi);
3294
Chris Lattner2a4f7312005-05-13 04:45:13 +00003295 // If this is unsigned, and not supported, first perform the conversion to
3296 // signed, then adjust the result if the sign bit is set.
3297 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3298 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3299
Chris Lattnerd47675e2005-08-09 20:20:18 +00003300 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3301 DAG.getConstant(0, Hi.getValueType()),
3302 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003303 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3304 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3305 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003306 uint64_t FF = 0x5f800000ULL;
3307 if (TLI.isLittleEndian()) FF <<= 32;
3308 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003309
Chris Lattnerc30405e2005-08-26 17:15:30 +00003310 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003311 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3312 SDOperand FudgeInReg;
3313 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003314 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3315 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003316 else {
3317 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003318 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3319 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003320 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003321 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003322 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003323
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003324 // Check to see if the target has a custom way to lower this. If so, use it.
3325 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3326 default: assert(0 && "This action not implemented for this operation!");
3327 case TargetLowering::Legal:
3328 case TargetLowering::Expand:
3329 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003330 case TargetLowering::Custom: {
3331 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3332 Source), DAG);
3333 if (NV.Val)
3334 return LegalizeOp(NV);
3335 break; // The target decided this was legal after all
3336 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003337 }
3338
Chris Lattner153587e2005-05-12 07:00:44 +00003339 // Expand the source, then glue it back together for the call. We must expand
3340 // the source in case it is shared (this pass of legalize must traverse it).
3341 SDOperand SrcLo, SrcHi;
3342 ExpandOp(Source, SrcLo, SrcHi);
3343 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3344
Chris Lattner06bbeb62005-05-11 19:02:11 +00003345 SDNode *OutChain = 0;
3346 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3347 DAG.getEntryNode());
3348 const char *FnName = 0;
3349 if (DestTy == MVT::f32)
3350 FnName = "__floatdisf";
3351 else {
3352 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3353 FnName = "__floatdidf";
3354 }
3355
Chris Lattneraac464e2005-01-21 06:05:23 +00003356 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3357
3358 TargetLowering::ArgListTy Args;
3359 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003360
Chris Lattneraac464e2005-01-21 06:05:23 +00003361 Args.push_back(std::make_pair(Source, ArgTy));
3362
3363 // We don't care about token chains for libcalls. We just use the entry
3364 // node as our input and ignore the output chain. This allows us to place
3365 // calls wherever we need them to satisfy data dependences.
3366 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003367
3368 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003369 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3370 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003371
Chris Lattnera5bf1032005-05-12 04:49:08 +00003372 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003373 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003374}
Misha Brukman835702a2005-04-21 22:36:52 +00003375
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003376
3377
Chris Lattnerdc750592005-01-07 07:47:09 +00003378/// ExpandOp - Expand the specified SDOperand into its two component pieces
3379/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3380/// LegalizeNodes map is filled in for any results that are not expanded, the
3381/// ExpandedNodes map is filled in for any results that are expanded, and the
3382/// Lo/Hi values are returned.
3383void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3384 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003385 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003386 SDNode *Node = Op.Val;
3387 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003388 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3389 "Cannot expand FP values!");
3390 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003391 "Cannot expand to FP value or to larger int value!");
3392
Chris Lattner1a570f12005-09-02 20:32:45 +00003393 // See if we already expanded it.
3394 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3395 = ExpandedNodes.find(Op);
3396 if (I != ExpandedNodes.end()) {
3397 Lo = I->second.first;
3398 Hi = I->second.second;
3399 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003400 }
3401
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003402 // Expanding to multiple registers needs to perform an optimization step, and
3403 // is not careful to avoid operations the target does not support. Make sure
3404 // that all generated operations are legalized in the next iteration.
3405 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003406
Chris Lattnerdc750592005-01-07 07:47:09 +00003407 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003408 case ISD::CopyFromReg:
3409 assert(0 && "CopyFromReg must be legal!");
3410 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003411 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3412 assert(0 && "Do not know how to expand this operator!");
3413 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003414 case ISD::UNDEF:
3415 Lo = DAG.getNode(ISD::UNDEF, NVT);
3416 Hi = DAG.getNode(ISD::UNDEF, NVT);
3417 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003418 case ISD::Constant: {
3419 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3420 Lo = DAG.getConstant(Cst, NVT);
3421 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3422 break;
3423 }
Nate Begemanae89d862005-12-07 19:48:11 +00003424 case ISD::ConstantVec: {
3425 unsigned NumElements = Node->getNumOperands();
3426 // If we only have two elements left in the constant vector, just break it
3427 // apart into the two scalar constants it contains. Otherwise, bisect the
3428 // ConstantVec, and return each half as a new ConstantVec.
3429 // FIXME: this is hard coded as big endian, it may have to change to support
3430 // SSE and Alpha MVI
3431 if (NumElements == 2) {
3432 Hi = Node->getOperand(0);
3433 Lo = Node->getOperand(1);
3434 } else {
3435 NumElements /= 2;
3436 std::vector<SDOperand> LoOps, HiOps;
3437 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3438 HiOps.push_back(Node->getOperand(I));
3439 LoOps.push_back(Node->getOperand(I+NumElements));
3440 }
3441 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3442 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3443 }
3444 break;
3445 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003446
Chris Lattner32e08b72005-03-28 22:03:13 +00003447 case ISD::BUILD_PAIR:
3448 // Legalize both operands. FIXME: in the future we should handle the case
3449 // where the two elements are not legal.
3450 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3451 Lo = LegalizeOp(Node->getOperand(0));
3452 Hi = LegalizeOp(Node->getOperand(1));
3453 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003454
3455 case ISD::SIGN_EXTEND_INREG:
3456 ExpandOp(Node->getOperand(0), Lo, Hi);
3457 // Sign extend the lo-part.
3458 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3459 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3460 TLI.getShiftAmountTy()));
3461 // sext_inreg the low part if needed.
3462 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3463 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003464
Chris Lattner55e9cde2005-05-11 04:51:16 +00003465 case ISD::CTPOP:
3466 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003467 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3468 DAG.getNode(ISD::CTPOP, NVT, Lo),
3469 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003470 Hi = DAG.getConstant(0, NVT);
3471 break;
3472
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003473 case ISD::CTLZ: {
3474 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003475 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003476 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3477 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003478 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3479 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003480 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3481 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3482
3483 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3484 Hi = DAG.getConstant(0, NVT);
3485 break;
3486 }
3487
3488 case ISD::CTTZ: {
3489 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003490 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003491 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3492 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003493 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3494 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003495 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3496 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3497
3498 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3499 Hi = DAG.getConstant(0, NVT);
3500 break;
3501 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003502
Chris Lattnerdc750592005-01-07 07:47:09 +00003503 case ISD::LOAD: {
3504 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3505 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003506 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003507
3508 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003509 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003510 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3511 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003512 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003513 //are from the same instruction?
3514 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003515
3516 // Build a factor node to remember that this load is independent of the
3517 // other one.
3518 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3519 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003520
Chris Lattnerdc750592005-01-07 07:47:09 +00003521 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003522 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003523 if (!TLI.isLittleEndian())
3524 std::swap(Lo, Hi);
3525 break;
3526 }
Nate Begemand37c1312005-11-22 18:16:00 +00003527 case ISD::VLOAD: {
3528 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3529 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3530 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3531 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3532
3533 // If we only have two elements, turn into a pair of scalar loads.
3534 // FIXME: handle case where a vector of two elements is fine, such as
3535 // 2 x double on SSE2.
3536 if (NumElements == 2) {
3537 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3538 // Increment the pointer to the other half.
3539 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3540 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3541 getIntPtrConstant(IncrementSize));
3542 //Is this safe? declaring that the two parts of the split load
3543 //are from the same instruction?
3544 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3545 } else {
3546 NumElements /= 2; // Split the vector in half
3547 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3548 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3549 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3550 getIntPtrConstant(IncrementSize));
3551 //Is this safe? declaring that the two parts of the split load
3552 //are from the same instruction?
3553 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3554 }
3555
3556 // Build a factor node to remember that this load is independent of the
3557 // other one.
3558 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3559 Hi.getValue(1));
3560
3561 // Remember that we legalized the chain.
3562 AddLegalizedOperand(Op.getValue(1), TF);
3563 if (!TLI.isLittleEndian())
3564 std::swap(Lo, Hi);
3565 break;
3566 }
3567 case ISD::VADD:
3568 case ISD::VSUB:
3569 case ISD::VMUL: {
3570 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3571 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3572 SDOperand LL, LH, RL, RH;
3573
3574 ExpandOp(Node->getOperand(0), LL, LH);
3575 ExpandOp(Node->getOperand(1), RL, RH);
3576
3577 // If we only have two elements, turn into a pair of scalar loads.
3578 // FIXME: handle case where a vector of two elements is fine, such as
3579 // 2 x double on SSE2.
3580 if (NumElements == 2) {
3581 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3582 Lo = DAG.getNode(Opc, EVT, LL, RL);
3583 Hi = DAG.getNode(Opc, EVT, LH, RH);
3584 } else {
3585 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3586 LL.getOperand(3));
3587 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3588 LH.getOperand(3));
3589 }
3590 break;
3591 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003592 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003593 case ISD::CALL: {
3594 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3595 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3596
Chris Lattner3d95c142005-01-19 20:24:35 +00003597 bool Changed = false;
3598 std::vector<SDOperand> Ops;
3599 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3600 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3601 Changed |= Ops.back() != Node->getOperand(i);
3602 }
3603
Chris Lattnerdc750592005-01-07 07:47:09 +00003604 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3605 "Can only expand a call once so far, not i64 -> i16!");
3606
3607 std::vector<MVT::ValueType> RetTyVTs;
3608 RetTyVTs.reserve(3);
3609 RetTyVTs.push_back(NVT);
3610 RetTyVTs.push_back(NVT);
3611 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003612 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3613 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003614 Lo = SDOperand(NC, 0);
3615 Hi = SDOperand(NC, 1);
3616
3617 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003618 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003619 break;
3620 }
3621 case ISD::AND:
3622 case ISD::OR:
3623 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3624 SDOperand LL, LH, RL, RH;
3625 ExpandOp(Node->getOperand(0), LL, LH);
3626 ExpandOp(Node->getOperand(1), RL, RH);
3627 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3628 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3629 break;
3630 }
3631 case ISD::SELECT: {
3632 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003633
3634 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3635 case Expand: assert(0 && "It's impossible to expand bools");
3636 case Legal:
3637 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3638 break;
3639 case Promote:
3640 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3641 break;
3642 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003643 ExpandOp(Node->getOperand(1), LL, LH);
3644 ExpandOp(Node->getOperand(2), RL, RH);
3645 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3646 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3647 break;
3648 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003649 case ISD::SELECT_CC: {
3650 SDOperand TL, TH, FL, FH;
3651 ExpandOp(Node->getOperand(2), TL, TH);
3652 ExpandOp(Node->getOperand(3), FL, FH);
3653 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3654 Node->getOperand(1), TL, FL, Node->getOperand(4));
3655 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3656 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003657 Lo = LegalizeOp(Lo);
3658 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003659 break;
3660 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003661 case ISD::SEXTLOAD: {
3662 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3663 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3664 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3665
3666 if (EVT == NVT)
3667 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3668 else
3669 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3670 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003671
3672 // Remember that we legalized the chain.
3673 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3674
Nate Begemanc3a89c52005-10-13 17:15:37 +00003675 // The high part is obtained by SRA'ing all but one of the bits of the lo
3676 // part.
3677 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3678 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3679 TLI.getShiftAmountTy()));
3680 Lo = LegalizeOp(Lo);
3681 Hi = LegalizeOp(Hi);
3682 break;
3683 }
3684 case ISD::ZEXTLOAD: {
3685 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3686 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3687 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3688
3689 if (EVT == NVT)
3690 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3691 else
3692 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3693 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003694
3695 // Remember that we legalized the chain.
3696 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3697
Nate Begemanc3a89c52005-10-13 17:15:37 +00003698 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003699 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003700 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003701 break;
3702 }
3703 case ISD::EXTLOAD: {
3704 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3705 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3706 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3707
3708 if (EVT == NVT)
3709 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3710 else
3711 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3712 EVT);
3713
3714 // Remember that we legalized the chain.
3715 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3716
3717 // The high part is undefined.
3718 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3719 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003720 break;
3721 }
Chris Lattner7753f172005-09-02 00:18:10 +00003722 case ISD::ANY_EXTEND: {
3723 SDOperand In;
3724 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3725 case Expand: assert(0 && "expand-expand not implemented yet!");
3726 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3727 case Promote:
3728 In = PromoteOp(Node->getOperand(0));
3729 break;
3730 }
3731
3732 // The low part is any extension of the input (which degenerates to a copy).
3733 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3734 // The high part is undefined.
3735 Hi = DAG.getNode(ISD::UNDEF, NVT);
3736 break;
3737 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003738 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003739 SDOperand In;
3740 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3741 case Expand: assert(0 && "expand-expand not implemented yet!");
3742 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3743 case Promote:
3744 In = PromoteOp(Node->getOperand(0));
3745 // Emit the appropriate sign_extend_inreg to get the value we want.
3746 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003747 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003748 break;
3749 }
3750
Chris Lattnerdc750592005-01-07 07:47:09 +00003751 // The low part is just a sign extension of the input (which degenerates to
3752 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003753 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003754
Chris Lattnerdc750592005-01-07 07:47:09 +00003755 // The high part is obtained by SRA'ing all but one of the bits of the lo
3756 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003757 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003758 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3759 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003760 break;
3761 }
Chris Lattner47844892005-04-03 23:41:52 +00003762 case ISD::ZERO_EXTEND: {
3763 SDOperand In;
3764 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3765 case Expand: assert(0 && "expand-expand not implemented yet!");
3766 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3767 case Promote:
3768 In = PromoteOp(Node->getOperand(0));
3769 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003770 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003771 break;
3772 }
3773
Chris Lattnerdc750592005-01-07 07:47:09 +00003774 // The low part is just a zero extension of the input (which degenerates to
3775 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003776 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003777
Chris Lattnerdc750592005-01-07 07:47:09 +00003778 // The high part is just a zero.
3779 Hi = DAG.getConstant(0, NVT);
3780 break;
Chris Lattner47844892005-04-03 23:41:52 +00003781 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003782
3783 case ISD::BIT_CONVERT: {
3784 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3785 Node->getOperand(0));
3786 ExpandOp(Tmp, Lo, Hi);
3787 break;
3788 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003789
Chris Lattner44c28c22005-11-20 22:56:56 +00003790 case ISD::READCYCLECOUNTER: {
3791 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3792 TargetLowering::Custom &&
3793 "Must custom expand ReadCycleCounter");
3794 SDOperand T = TLI.LowerOperation(Op, DAG);
3795 assert(T.Val && "Node must be custom expanded!");
3796 Lo = LegalizeOp(T.getValue(0));
3797 Hi = LegalizeOp(T.getValue(1));
3798 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3799 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003800 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003801 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003802
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003803 // These operators cannot be expanded directly, emit them as calls to
3804 // library functions.
3805 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003806 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003807 SDOperand Op;
3808 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3809 case Expand: assert(0 && "cannot expand FP!");
3810 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3811 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3812 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003813
Chris Lattner941d84a2005-07-30 01:40:57 +00003814 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3815
Chris Lattnerfe68d752005-07-29 00:33:32 +00003816 // Now that the custom expander is done, expand the result, which is still
3817 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003818 if (Op.Val) {
3819 ExpandOp(Op, Lo, Hi);
3820 break;
3821 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003822 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003823
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003824 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003825 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003826 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003827 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003828 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003829
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003830 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003831 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3832 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3833 LegalizeOp(Node->getOperand(0)));
3834 // Now that the custom expander is done, expand the result, which is still
3835 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003836 Op = TLI.LowerOperation(Op, DAG);
3837 if (Op.Val) {
3838 ExpandOp(Op, Lo, Hi);
3839 break;
3840 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003841 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003842
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003843 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003844 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003845 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003846 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003847 break;
3848
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003849 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003850 // If the target wants custom lowering, do so.
3851 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3852 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3853 LegalizeOp(Node->getOperand(1)));
3854 Op = TLI.LowerOperation(Op, DAG);
3855 if (Op.Val) {
3856 // Now that the custom expander is done, expand the result, which is
3857 // still VT.
3858 ExpandOp(Op, Lo, Hi);
3859 break;
3860 }
3861 }
3862
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003863 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003864 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003865 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003866
3867 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003868 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003869 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3870 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003871 break;
3872 }
3873
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003874 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003875 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003876 break;
3877
3878 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003879 // If the target wants custom lowering, do so.
3880 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3881 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3882 LegalizeOp(Node->getOperand(1)));
3883 Op = TLI.LowerOperation(Op, DAG);
3884 if (Op.Val) {
3885 // Now that the custom expander is done, expand the result, which is
3886 // still VT.
3887 ExpandOp(Op, Lo, Hi);
3888 break;
3889 }
3890 }
3891
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003892 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003893 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003894 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003895
3896 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003897 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003898 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3899 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003900 break;
3901 }
3902
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003903 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003904 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003905 break;
3906 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003907 // If the target wants custom lowering, do so.
3908 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3909 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3910 LegalizeOp(Node->getOperand(1)));
3911 Op = TLI.LowerOperation(Op, DAG);
3912 if (Op.Val) {
3913 // Now that the custom expander is done, expand the result, which is
3914 // still VT.
3915 ExpandOp(Op, Lo, Hi);
3916 break;
3917 }
3918 }
3919
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003920 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003921 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003922 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003923
3924 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003925 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003926 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3927 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003928 break;
3929 }
3930
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003931 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003932 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003933 break;
3934
Misha Brukman835702a2005-04-21 22:36:52 +00003935 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003936 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3937 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003938 break;
3939 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003940 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3941 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003942 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003943 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003944 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003945 SDOperand LL, LH, RL, RH;
3946 ExpandOp(Node->getOperand(0), LL, LH);
3947 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003948 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3949 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3950 // extended the sign bit of the low half through the upper half, and if so
3951 // emit a MULHS instead of the alternate sequence that is valid for any
3952 // i64 x i64 multiply.
3953 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3954 // is RH an extension of the sign bit of RL?
3955 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3956 RH.getOperand(1).getOpcode() == ISD::Constant &&
3957 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3958 // is LH an extension of the sign bit of LL?
3959 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3960 LH.getOperand(1).getOpcode() == ISD::Constant &&
3961 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3962 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3963 } else {
3964 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3965 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3966 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3967 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3968 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3969 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003970 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3971 } else {
3972 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3973 }
3974 break;
3975 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003976 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3977 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3978 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3979 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003980 }
3981
3982 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003983 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3984 std::make_pair(Lo, Hi))).second;
3985 assert(isNew && "Value already expanded?!?");
Chris Lattner2af3ee42005-12-20 00:53:54 +00003986
Chris Lattnerac12f682005-12-21 18:02:52 +00003987 // Make sure the resultant values have been legalized themselves, unless this
3988 // is a type that requires multi-step expansion.
3989 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
3990 Lo = LegalizeOp(Lo);
3991 Hi = LegalizeOp(Hi);
3992 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003993}
3994
3995
3996// SelectionDAG::Legalize - This is the entry point for the file.
3997//
Chris Lattner4add7e32005-01-23 04:42:50 +00003998void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003999 /// run - This is the main entry point to this class.
4000 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00004001 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00004002}
4003