blob: 0690c641db6d8d8b6e32397c8e0cc1903fd3231c [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) {
80 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
82 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000083 void AddPromotedOperand(SDOperand From, SDOperand To) {
84 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
85 assert(isNew && "Got into the map somehow?");
86 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000087
Chris Lattnerdc750592005-01-07 07:47:09 +000088public:
89
Chris Lattner4add7e32005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000091
92 /// Run - While there is still lowering to do, perform a pass over the DAG.
93 /// Most regularization can be done in a single pass, but targets that require
94 /// large values to be split into registers multiple times (e.g. i64 -> 4x
95 /// i16) require iteration for these values (the first iteration will demote
96 /// to i32, the second will demote to i16).
97 void Run() {
98 do {
99 NeedsAnotherIteration = false;
100 LegalizeDAG();
101 } while (NeedsAnotherIteration);
102 }
103
104 /// getTypeAction - Return how we should legalize values of this type, either
105 /// it is already legal or we need to expand it into multiple registers of
106 /// smaller integer type, or we need to promote it to a larger type.
107 LegalizeAction getTypeAction(MVT::ValueType VT) const {
108 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
109 }
110
111 /// isTypeLegal - Return true if this type is legal on this target.
112 ///
113 bool isTypeLegal(MVT::ValueType VT) const {
114 return getTypeAction(VT) == Legal;
115 }
116
117private:
118 void LegalizeDAG();
119
120 SDOperand LegalizeOp(SDOperand O);
121 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000123
Chris Lattneraac464e2005-01-21 06:05:23 +0000124 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
125 SDOperand &Hi);
126 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
127 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000128
Jim Laskeyf2516a92005-08-17 00:39:29 +0000129 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
130 SDOperand LegalOp,
131 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000132 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000134 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000136
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000137 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000139 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
140 SDOperand &Lo, SDOperand &Hi);
141 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000142 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000143
Chris Lattnera5bf1032005-05-12 04:49:08 +0000144 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
145
Chris Lattnerdc750592005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
Chris Lattner301015a2005-11-19 05:51:46 +0000152static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000153 switch (VecOp) {
154 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-11-19 00:36:38 +0000155 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
156 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
157 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
158 }
159}
Chris Lattnerdc750592005-01-07 07:47:09 +0000160
Chris Lattner4add7e32005-01-23 04:42:50 +0000161SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
162 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
163 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000164 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000165 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000166}
167
Jim Laskeyf2516a92005-08-17 00:39:29 +0000168/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
169/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000170/// we expand it. At this point, we know that the result and operand types are
171/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000172SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
173 SDOperand Op0,
174 MVT::ValueType DestVT) {
175 if (Op0.getValueType() == MVT::i32) {
176 // simple 32-bit [signed|unsigned] integer to float/double expansion
177
178 // get the stack frame index of a 8 byte buffer
179 MachineFunction &MF = DAG.getMachineFunction();
180 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
181 // get address of 8 byte buffer
182 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
183 // word offset constant for Hi/Lo address computation
184 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
185 // set up Hi and Lo (into buffer) address based on endian
186 SDOperand Hi, Lo;
187 if (TLI.isLittleEndian()) {
188 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
189 Lo = StackSlot;
190 } else {
191 Hi = StackSlot;
192 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
193 }
194 // if signed map to unsigned space
195 SDOperand Op0Mapped;
196 if (isSigned) {
197 // constant used to invert sign bit (signed to unsigned mapping)
198 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
199 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
200 } else {
201 Op0Mapped = Op0;
202 }
203 // store the lo of the constructed double - based on integer input
204 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
205 Op0Mapped, Lo, DAG.getSrcValue(NULL));
206 // initial hi portion of constructed double
207 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
208 // store the hi of the constructed double - biased exponent
209 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
210 InitialHi, Hi, DAG.getSrcValue(NULL));
211 // load the constructed double
212 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
213 DAG.getSrcValue(NULL));
214 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000215 SDOperand Bias = DAG.getConstantFP(isSigned ?
216 BitsToDouble(0x4330000080000000ULL)
217 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000218 MVT::f64);
219 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000220 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000221 // final result
222 SDOperand Result;
223 // handle final rounding
224 if (DestVT == MVT::f64) {
225 // do nothing
226 Result = Sub;
227 } else {
228 // if f32 then cast to f32
229 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
230 }
231 NeedsAnotherIteration = true;
232 return Result;
233 }
234 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000235 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000236
Chris Lattnerd47675e2005-08-09 20:20:18 +0000237 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
238 DAG.getConstant(0, Op0.getValueType()),
239 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000240 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
241 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
242 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000243
Jim Laskeyf2516a92005-08-17 00:39:29 +0000244 // If the sign bit of the integer is set, the large number will be treated
245 // as a negative number. To counteract this, the dynamic code adds an
246 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000247 uint64_t FF;
248 switch (Op0.getValueType()) {
249 default: assert(0 && "Unsupported integer type!");
250 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
251 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
252 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
253 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
254 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000255 if (TLI.isLittleEndian()) FF <<= 32;
256 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000257
Chris Lattnerc30405e2005-08-26 17:15:30 +0000258 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-07-16 00:19:57 +0000259 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
260 SDOperand FudgeInReg;
261 if (DestVT == MVT::f32)
262 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
263 DAG.getSrcValue(NULL));
264 else {
265 assert(DestVT == MVT::f64 && "Unexpected conversion");
266 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
267 DAG.getEntryNode(), CPIdx,
268 DAG.getSrcValue(NULL), MVT::f32));
269 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000270
Chris Lattnere3e847b2005-07-16 00:19:57 +0000271 NeedsAnotherIteration = true;
Chris Lattner5b2be1f2005-09-29 06:44:39 +0000272 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000273}
274
Chris Lattner19732782005-08-16 18:17:10 +0000275/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000276/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000277/// we promote it. At this point, we know that the result and operand types are
278/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
279/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000280SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
281 MVT::ValueType DestVT,
282 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000283 // First step, figure out the appropriate *INT_TO_FP operation to use.
284 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000285
Chris Lattnere3e847b2005-07-16 00:19:57 +0000286 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000287
Chris Lattnere3e847b2005-07-16 00:19:57 +0000288 // Scan for the appropriate larger type to use.
289 while (1) {
290 NewInTy = (MVT::ValueType)(NewInTy+1);
291 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000292
Chris Lattnere3e847b2005-07-16 00:19:57 +0000293 // If the target supports SINT_TO_FP of this type, use it.
294 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
295 default: break;
296 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000297 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000298 break; // Can't use this datatype.
299 // FALL THROUGH.
300 case TargetLowering::Custom:
301 OpToUse = ISD::SINT_TO_FP;
302 break;
303 }
304 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000305 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000306
Chris Lattnere3e847b2005-07-16 00:19:57 +0000307 // If the target supports UINT_TO_FP of this type, use it.
308 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
309 default: break;
310 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000311 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000312 break; // Can't use this datatype.
313 // FALL THROUGH.
314 case TargetLowering::Custom:
315 OpToUse = ISD::UINT_TO_FP;
316 break;
317 }
318 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000319
Chris Lattnere3e847b2005-07-16 00:19:57 +0000320 // Otherwise, try a larger type.
321 }
322
323 // Make sure to legalize any nodes we create here in the next pass.
324 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000325
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.
328 return 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 Lattnere3e847b2005-07-16 00:19:57 +0000331}
332
Chris Lattner44fe26f2005-07-29 00:11:56 +0000333/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
334/// FP_TO_*INT operation of the specified operand when the target requests that
335/// we promote it. At this point, we know that the result and operand types are
336/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
337/// operation that returns a larger result.
338SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
339 MVT::ValueType DestVT,
340 bool isSigned) {
341 // First step, figure out the appropriate FP_TO*INT operation to use.
342 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000343
Chris Lattner44fe26f2005-07-29 00:11:56 +0000344 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-07-29 00:11:56 +0000346 // Scan for the appropriate larger type to use.
347 while (1) {
348 NewOutTy = (MVT::ValueType)(NewOutTy+1);
349 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000350
Chris Lattner44fe26f2005-07-29 00:11:56 +0000351 // If the target supports FP_TO_SINT returning this type, use it.
352 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
353 default: break;
354 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000355 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000356 break; // Can't use this datatype.
357 // FALL THROUGH.
358 case TargetLowering::Custom:
359 OpToUse = ISD::FP_TO_SINT;
360 break;
361 }
362 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000363
Chris Lattner44fe26f2005-07-29 00:11:56 +0000364 // If the target supports FP_TO_UINT of this type, use it.
365 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
366 default: break;
367 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000368 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000369 break; // Can't use this datatype.
370 // FALL THROUGH.
371 case TargetLowering::Custom:
372 OpToUse = ISD::FP_TO_UINT;
373 break;
374 }
375 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000376
Chris Lattner44fe26f2005-07-29 00:11:56 +0000377 // Otherwise, try a larger type.
378 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000379
Chris Lattner44fe26f2005-07-29 00:11:56 +0000380 // Make sure to legalize any nodes we create here in the next pass.
381 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000382
Chris Lattner44fe26f2005-07-29 00:11:56 +0000383 // Okay, we found the operation and type to use. Truncate the result of the
384 // extended FP_TO_*INT operation to the desired size.
385 return DAG.getNode(ISD::TRUNCATE, DestVT,
386 DAG.getNode(OpToUse, NewOutTy, LegalOp));
387}
388
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000389/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
390/// not been visited yet and if all of its operands have already been visited.
391static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
392 std::map<SDNode*, unsigned> &Visited) {
393 if (++Visited[N] != N->getNumOperands())
394 return; // Haven't visited all operands yet
395
396 Order.push_back(N);
397
398 if (N->hasOneUse()) { // Tail recurse in common case.
399 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
400 return;
401 }
402
403 // Now that we have N in, add anything that uses it if all of their operands
404 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000405 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
406 ComputeTopDownOrdering(*UI, Order, Visited);
407}
408
Chris Lattner44fe26f2005-07-29 00:11:56 +0000409
Chris Lattnerdc750592005-01-07 07:47:09 +0000410void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000411 // The legalize process is inherently a bottom-up recursive process (users
412 // legalize their uses before themselves). Given infinite stack space, we
413 // could just start legalizing on the root and traverse the whole graph. In
414 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000415 // blocks. To avoid this problem, compute an ordering of the nodes where each
416 // node is only legalized after all of its operands are legalized.
417 std::map<SDNode*, unsigned> Visited;
418 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000419
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000420 // Compute ordering from all of the leaves in the graphs, those (like the
421 // entry node) that have no operands.
422 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
423 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000424 if (I->getNumOperands() == 0) {
425 Visited[I] = 0 - 1U;
426 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000427 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000428 }
429
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000430 assert(Order.size() == Visited.size() &&
431 Order.size() ==
432 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000433 "Error: DAG is cyclic!");
434 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000435
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000436 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
437 SDNode *N = Order[i];
438 switch (getTypeAction(N->getValueType(0))) {
439 default: assert(0 && "Bad type action!");
440 case Legal:
441 LegalizeOp(SDOperand(N, 0));
442 break;
443 case Promote:
444 PromoteOp(SDOperand(N, 0));
445 break;
446 case Expand: {
447 SDOperand X, Y;
448 ExpandOp(SDOperand(N, 0), X, Y);
449 break;
450 }
451 }
452 }
453
454 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000455 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000456 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
457 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000458
459 ExpandedNodes.clear();
460 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000461 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000462
463 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000464 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000465}
466
467SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000468 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000469 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000470 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000471
Chris Lattnerdc750592005-01-07 07:47:09 +0000472 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000473 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000474 if (Node->getNumValues() > 1) {
475 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
476 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000477 case Legal: break; // Nothing to do.
478 case Expand: {
479 SDOperand T1, T2;
480 ExpandOp(Op.getValue(i), T1, T2);
481 assert(LegalizedNodes.count(Op) &&
482 "Expansion didn't add legal operands!");
483 return LegalizedNodes[Op];
484 }
485 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000486 PromoteOp(Op.getValue(i));
487 assert(LegalizedNodes.count(Op) &&
488 "Expansion didn't add legal operands!");
489 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000490 }
491 }
492
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000493 // Note that LegalizeOp may be reentered even from single-use nodes, which
494 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000495 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
496 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000497
Nate Begemane5b86d72005-08-10 20:51:12 +0000498 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000499
500 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000501
502 switch (Node->getOpcode()) {
503 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000504 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
505 // If this is a target node, legalize it by legalizing the operands then
506 // passing it through.
507 std::vector<SDOperand> Ops;
508 bool Changed = false;
509 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
510 Ops.push_back(LegalizeOp(Node->getOperand(i)));
511 Changed = Changed || Node->getOperand(i) != Ops.back();
512 }
513 if (Changed)
514 if (Node->getNumValues() == 1)
515 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
516 else {
517 std::vector<MVT::ValueType> VTs(Node->value_begin(),
518 Node->value_end());
519 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
520 }
521
522 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
523 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
524 return Result.getValue(Op.ResNo);
525 }
526 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000527 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
528 assert(0 && "Do not know how to legalize this operator!");
529 abort();
530 case ISD::EntryToken:
531 case ISD::FrameIndex:
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000532 case ISD::TargetFrameIndex:
533 case ISD::Register:
534 case ISD::TargetConstant:
Chris Lattnerdc750592005-01-07 07:47:09 +0000535 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000536 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000537 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000538 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000539 case ISD::BasicBlock:
540 case ISD::CONDCODE:
541 case ISD::VALUETYPE:
542 case ISD::SRCVALUE:
Chris Lattner435b4022005-11-29 06:21:05 +0000543 case ISD::STRING:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000544 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
545 default: assert(0 && "This action is not supported yet!");
546 case TargetLowering::Custom: {
547 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
548 if (Tmp.Val) {
549 Result = LegalizeOp(Tmp);
550 break;
551 }
552 } // FALLTHROUGH if the target doesn't want to lower this op after all.
553 case TargetLowering::Legal:
554 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
555 break;
556 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000557 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000558 case ISD::AssertSext:
559 case ISD::AssertZext:
560 Tmp1 = LegalizeOp(Node->getOperand(0));
561 if (Tmp1 != Node->getOperand(0))
562 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
563 Node->getOperand(1));
564 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000565 case ISD::MERGE_VALUES:
566 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000567 case ISD::CopyFromReg:
568 Tmp1 = LegalizeOp(Node->getOperand(0));
569 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000570 Result = DAG.getCopyFromReg(Tmp1,
571 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
572 Node->getValueType(0));
Chris Lattnereb6614d2005-01-28 06:27:38 +0000573 else
574 Result = Op.getValue(0);
575
576 // Since CopyFromReg produces two values, make sure to remember that we
577 // legalized both of them.
578 AddLegalizedOperand(Op.getValue(0), Result);
579 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
580 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000581 case ISD::ImplicitDef:
582 Tmp1 = LegalizeOp(Node->getOperand(0));
583 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000584 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
585 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000586 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000587 case ISD::UNDEF: {
588 MVT::ValueType VT = Op.getValueType();
589 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000590 default: assert(0 && "This action is not supported yet!");
591 case TargetLowering::Expand:
592 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000593 if (MVT::isInteger(VT))
594 Result = DAG.getConstant(0, VT);
595 else if (MVT::isFloatingPoint(VT))
596 Result = DAG.getConstantFP(0, VT);
597 else
598 assert(0 && "Unknown value type!");
599 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000600 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000601 break;
602 }
603 break;
604 }
Chris Lattner435b4022005-11-29 06:21:05 +0000605
606 case ISD::LOCATION:
607 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
608 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
609
610 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
611 case TargetLowering::Promote:
612 default: assert(0 && "This action is not supported yet!");
613 case TargetLowering::Expand:
614 // If the target doesn't support line numbers, ignore this node.
615 Result = Tmp1;
616 break;
617 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000618 if (Tmp1 != Node->getOperand(0) ||
619 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000620 std::vector<SDOperand> Ops;
621 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000622 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
623 Ops.push_back(Node->getOperand(1)); // line # must be legal.
624 Ops.push_back(Node->getOperand(2)); // col # must be legal.
625 } else {
626 // Otherwise promote them.
627 Ops.push_back(PromoteOp(Node->getOperand(1)));
628 Ops.push_back(PromoteOp(Node->getOperand(2)));
629 }
Chris Lattner435b4022005-11-29 06:21:05 +0000630 Ops.push_back(Node->getOperand(3)); // filename must be legal.
631 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
632 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
633 }
634 break;
635 }
636 break;
637
Chris Lattnerdc750592005-01-07 07:47:09 +0000638 case ISD::Constant:
639 // We know we don't need to expand constants here, constants only have one
640 // value and we check that it is fine above.
641
642 // FIXME: Maybe we should handle things like targets that don't support full
643 // 32-bit immediates?
644 break;
645 case ISD::ConstantFP: {
646 // Spill FP immediates to the constant pool if the target cannot directly
647 // codegen them. Targets often have some immediate values that can be
648 // efficiently generated into an FP register without a load. We explicitly
649 // leave these constants as ConstantFP nodes for the target to deal with.
650
651 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
652
653 // Check to see if this FP immediate is already legal.
654 bool isLegal = false;
655 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
656 E = TLI.legal_fpimm_end(); I != E; ++I)
657 if (CFP->isExactlyValue(*I)) {
658 isLegal = true;
659 break;
660 }
661
662 if (!isLegal) {
663 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000664 bool Extend = false;
665
666 // If a FP immediate is precise when represented as a float, we put it
667 // into the constant pool as a float, even if it's is statically typed
668 // as a double.
669 MVT::ValueType VT = CFP->getValueType(0);
670 bool isDouble = VT == MVT::f64;
671 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
672 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000673 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
674 // Only do this if the target has a native EXTLOAD instruction from
675 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000676 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000677 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
678 VT = MVT::f32;
679 Extend = true;
680 }
Misha Brukman835702a2005-04-21 22:36:52 +0000681
Chris Lattnerc30405e2005-08-26 17:15:30 +0000682 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000683 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000684 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
685 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000686 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000687 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
688 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000689 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000690 }
691 break;
692 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000693 case ISD::TokenFactor:
694 if (Node->getNumOperands() == 2) {
695 bool Changed = false;
696 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
697 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
698 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
699 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
700 } else {
701 std::vector<SDOperand> Ops;
702 bool Changed = false;
703 // Legalize the operands.
704 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
705 SDOperand Op = Node->getOperand(i);
706 Ops.push_back(LegalizeOp(Op));
707 Changed |= Ops[i] != Op;
708 }
709 if (Changed)
710 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000711 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000712 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000713
Chris Lattner2dce7032005-05-12 23:24:06 +0000714 case ISD::CALLSEQ_START:
715 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000716 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000717 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000718 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000719 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000720 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000721
Chris Lattner2dce7032005-05-12 23:24:06 +0000722 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000723 // nodes are treated specially and are mutated in place. This makes the dag
724 // legalization process more efficient and also makes libcall insertion
725 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000726 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000727 case ISD::DYNAMIC_STACKALLOC:
728 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
729 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
730 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
731 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000732 Tmp3 != Node->getOperand(2)) {
733 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
734 std::vector<SDOperand> Ops;
735 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
736 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
737 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000738 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000739
740 // Since this op produces two values, make sure to remember that we
741 // legalized both of them.
742 AddLegalizedOperand(SDOperand(Node, 0), Result);
743 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
744 return Result.getValue(Op.ResNo);
745
Chris Lattnerd0feb642005-05-13 18:43:43 +0000746 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000747 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000748 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
749 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000750
751 bool Changed = false;
752 std::vector<SDOperand> Ops;
753 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
754 Ops.push_back(LegalizeOp(Node->getOperand(i)));
755 Changed |= Ops.back() != Node->getOperand(i);
756 }
757
758 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000759 std::vector<MVT::ValueType> RetTyVTs;
760 RetTyVTs.reserve(Node->getNumValues());
761 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000762 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000763 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
764 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000765 } else {
766 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000767 }
Chris Lattner9242c502005-01-09 19:43:23 +0000768 // Since calls produce multiple values, make sure to remember that we
769 // legalized all of them.
770 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
771 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
772 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000773 }
Chris Lattner68a12142005-01-07 22:12:08 +0000774 case ISD::BR:
775 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
776 if (Tmp1 != Node->getOperand(0))
777 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
778 break;
779
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000780 case ISD::BRCOND:
781 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000782
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000783 switch (getTypeAction(Node->getOperand(1).getValueType())) {
784 case Expand: assert(0 && "It's impossible to expand bools");
785 case Legal:
786 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
787 break;
788 case Promote:
789 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
790 break;
791 }
Nate Begeman371e4952005-08-16 19:49:35 +0000792
793 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
794 default: assert(0 && "This action is not supported yet!");
795 case TargetLowering::Expand:
796 // Expand brcond's setcc into its constituent parts and create a BR_CC
797 // Node.
798 if (Tmp2.getOpcode() == ISD::SETCC) {
799 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
800 Tmp2.getOperand(0), Tmp2.getOperand(1),
801 Node->getOperand(2));
802 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000803 // Make sure the condition is either zero or one. It may have been
804 // promoted from something else.
805 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
806
Nate Begeman371e4952005-08-16 19:49:35 +0000807 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
808 DAG.getCondCode(ISD::SETNE), Tmp2,
809 DAG.getConstant(0, Tmp2.getValueType()),
810 Node->getOperand(2));
811 }
812 break;
813 case TargetLowering::Legal:
814 // Basic block destination (Op#2) is always legal.
815 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
816 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
817 Node->getOperand(2));
818 break;
819 }
820 break;
821 case ISD::BR_CC:
822 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
823
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000824 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000825 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
826 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
827 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
828 Tmp3 != Node->getOperand(3)) {
829 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
830 Tmp2, Tmp3, Node->getOperand(4));
831 }
832 break;
833 } else {
834 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
835 Node->getOperand(2), // LHS
836 Node->getOperand(3), // RHS
837 Node->getOperand(1)));
838 // If we get a SETCC back from legalizing the SETCC node we just
839 // created, then use its LHS, RHS, and CC directly in creating a new
840 // node. Otherwise, select between the true and false value based on
841 // comparing the result of the legalized with zero.
842 if (Tmp2.getOpcode() == ISD::SETCC) {
843 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
844 Tmp2.getOperand(0), Tmp2.getOperand(1),
845 Node->getOperand(4));
846 } else {
847 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
848 DAG.getCondCode(ISD::SETNE),
849 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
850 Node->getOperand(4));
851 }
852 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000853 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000854 case ISD::BRCONDTWOWAY:
855 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
856 switch (getTypeAction(Node->getOperand(1).getValueType())) {
857 case Expand: assert(0 && "It's impossible to expand bools");
858 case Legal:
859 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
860 break;
861 case Promote:
862 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
863 break;
864 }
865 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
866 // pair.
867 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
868 case TargetLowering::Promote:
869 default: assert(0 && "This action is not supported yet!");
870 case TargetLowering::Legal:
871 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
872 std::vector<SDOperand> Ops;
873 Ops.push_back(Tmp1);
874 Ops.push_back(Tmp2);
875 Ops.push_back(Node->getOperand(2));
876 Ops.push_back(Node->getOperand(3));
877 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
878 }
879 break;
880 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000881 // If BRTWOWAY_CC is legal for this target, then simply expand this node
882 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
883 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000884 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000885 if (Tmp2.getOpcode() == ISD::SETCC) {
886 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
887 Tmp2.getOperand(0), Tmp2.getOperand(1),
888 Node->getOperand(2), Node->getOperand(3));
889 } else {
890 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
891 DAG.getConstant(0, Tmp2.getValueType()),
892 Node->getOperand(2), Node->getOperand(3));
893 }
894 } else {
895 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000896 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000897 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
898 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000899 break;
900 }
901 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000902 case ISD::BRTWOWAY_CC:
903 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000904 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000905 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
906 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
907 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
908 Tmp3 != Node->getOperand(3)) {
909 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
910 Node->getOperand(4), Node->getOperand(5));
911 }
912 break;
913 } else {
914 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
915 Node->getOperand(2), // LHS
916 Node->getOperand(3), // RHS
917 Node->getOperand(1)));
918 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
919 // pair.
920 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
921 default: assert(0 && "This action is not supported yet!");
922 case TargetLowering::Legal:
923 // If we get a SETCC back from legalizing the SETCC node we just
924 // created, then use its LHS, RHS, and CC directly in creating a new
925 // node. Otherwise, select between the true and false value based on
926 // comparing the result of the legalized with zero.
927 if (Tmp2.getOpcode() == ISD::SETCC) {
928 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
929 Tmp2.getOperand(0), Tmp2.getOperand(1),
930 Node->getOperand(4), Node->getOperand(5));
931 } else {
932 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
933 DAG.getConstant(0, Tmp2.getValueType()),
934 Node->getOperand(4), Node->getOperand(5));
935 }
936 break;
937 case TargetLowering::Expand:
938 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
939 Node->getOperand(4));
940 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
941 break;
942 }
943 }
944 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000945 case ISD::LOAD:
946 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
947 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000948
Chris Lattnerdc750592005-01-07 07:47:09 +0000949 if (Tmp1 != Node->getOperand(0) ||
950 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000951 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
952 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000953 else
954 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000955
Chris Lattnerea4ca942005-01-07 22:28:47 +0000956 // Since loads produce two values, make sure to remember that we legalized
957 // both of them.
958 AddLegalizedOperand(SDOperand(Node, 0), Result);
959 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
960 return Result.getValue(Op.ResNo);
Nate Begemanb2e089c2005-11-19 00:36:38 +0000961
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000962 case ISD::EXTLOAD:
963 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000964 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000965 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
966 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000967
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000968 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000969 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000970 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000971 case TargetLowering::Promote:
972 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000973 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
974 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000975 // Since loads produce two values, make sure to remember that we legalized
976 // both of them.
977 AddLegalizedOperand(SDOperand(Node, 0), Result);
978 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
979 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000980
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000981 case TargetLowering::Legal:
982 if (Tmp1 != Node->getOperand(0) ||
983 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000984 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
985 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000986 else
987 Result = SDOperand(Node, 0);
988
989 // Since loads produce two values, make sure to remember that we legalized
990 // both of them.
991 AddLegalizedOperand(SDOperand(Node, 0), Result);
992 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
993 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000994 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000995 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
996 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
997 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000998 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000999 if (Op.ResNo)
1000 return Load.getValue(1);
1001 return Result;
1002 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001003 assert(Node->getOpcode() != ISD::EXTLOAD &&
1004 "EXTLOAD should always be supported!");
1005 // Turn the unsupported load into an EXTLOAD followed by an explicit
1006 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001007 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1008 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001009 SDOperand ValRes;
1010 if (Node->getOpcode() == ISD::SEXTLOAD)
1011 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001012 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001013 else
1014 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001015 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1016 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1017 if (Op.ResNo)
1018 return Result.getValue(1);
1019 return ValRes;
1020 }
1021 assert(0 && "Unreachable");
1022 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001023 case ISD::EXTRACT_ELEMENT: {
1024 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1025 switch (getTypeAction(OpTy)) {
1026 default:
1027 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1028 break;
1029 case Legal:
1030 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1031 // 1 -> Hi
1032 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1033 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1034 TLI.getShiftAmountTy()));
1035 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1036 } else {
1037 // 0 -> Lo
1038 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1039 Node->getOperand(0));
1040 }
1041 Result = LegalizeOp(Result);
1042 break;
1043 case Expand:
1044 // Get both the low and high parts.
1045 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1046 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1047 Result = Tmp2; // 1 -> Hi
1048 else
1049 Result = Tmp1; // 0 -> Lo
1050 break;
1051 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001052 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001053 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001054
1055 case ISD::CopyToReg:
1056 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001057
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001058 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001059 "Register type must be legal!");
1060 // Legalize the incoming value (must be legal).
1061 Tmp2 = LegalizeOp(Node->getOperand(2));
1062 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1063 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1064 Node->getOperand(1), Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001065 break;
1066
1067 case ISD::RET:
1068 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1069 switch (Node->getNumOperands()) {
1070 case 2: // ret val
1071 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1072 case Legal:
1073 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001074 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001075 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1076 break;
1077 case Expand: {
1078 SDOperand Lo, Hi;
1079 ExpandOp(Node->getOperand(1), Lo, Hi);
1080 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001081 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001082 }
1083 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001084 Tmp2 = PromoteOp(Node->getOperand(1));
1085 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1086 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001087 }
1088 break;
1089 case 1: // ret void
1090 if (Tmp1 != Node->getOperand(0))
1091 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1092 break;
1093 default: { // ret <values>
1094 std::vector<SDOperand> NewValues;
1095 NewValues.push_back(Tmp1);
1096 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1097 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1098 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001099 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001100 break;
1101 case Expand: {
1102 SDOperand Lo, Hi;
1103 ExpandOp(Node->getOperand(i), Lo, Hi);
1104 NewValues.push_back(Lo);
1105 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001106 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001107 }
1108 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001109 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001110 }
1111 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1112 break;
1113 }
1114 }
1115 break;
1116 case ISD::STORE:
1117 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1118 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1119
Chris Lattnere69daaf2005-01-08 06:25:56 +00001120 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001121 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001122 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001123 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001124 DAG.getConstant(FloatToBits(CFP->getValue()),
1125 MVT::i32),
1126 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001127 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001128 } else {
1129 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001130 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001131 DAG.getConstant(DoubleToBits(CFP->getValue()),
1132 MVT::i64),
1133 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001134 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001135 }
Chris Lattnera4743132005-02-22 07:23:39 +00001136 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001137 }
1138
Chris Lattnerdc750592005-01-07 07:47:09 +00001139 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1140 case Legal: {
1141 SDOperand Val = LegalizeOp(Node->getOperand(1));
1142 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1143 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001144 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1145 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001146 break;
1147 }
1148 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001149 // Truncate the value and store the result.
1150 Tmp3 = PromoteOp(Node->getOperand(1));
1151 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001152 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001153 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001154 break;
1155
Chris Lattnerdc750592005-01-07 07:47:09 +00001156 case Expand:
1157 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001158 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001159 ExpandOp(Node->getOperand(1), Lo, Hi);
1160
1161 if (!TLI.isLittleEndian())
1162 std::swap(Lo, Hi);
1163
Chris Lattner55e9cde2005-05-11 04:51:16 +00001164 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1165 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001166 // If this is a vector type, then we have to calculate the increment as
1167 // the product of the element size in bytes, and the number of elements
1168 // in the high half of the vector.
1169 if (MVT::Vector == Hi.getValueType()) {
1170 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1171 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1172 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1173 } else {
1174 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1175 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001176 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1177 getIntPtrConstant(IncrementSize));
1178 assert(isTypeLegal(Tmp2.getValueType()) &&
1179 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001180 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001181 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1182 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001183 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1184 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001185 }
1186 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001187 case ISD::PCMARKER:
1188 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001189 if (Tmp1 != Node->getOperand(0))
1190 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001191 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001192 case ISD::READCYCLECOUNTER:
1193 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
1194 if (Tmp1 != Node->getOperand(0))
1195 Result = DAG.getNode(ISD::READCYCLECOUNTER, MVT::i64, Tmp1);
1196 break;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001197
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001198 case ISD::TRUNCSTORE:
1199 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1200 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1201
1202 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1203 case Legal:
1204 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001205
1206 // The only promote case we handle is TRUNCSTORE:i1 X into
1207 // -> TRUNCSTORE:i8 (and X, 1)
1208 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1209 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1210 TargetLowering::Promote) {
1211 // Promote the bool to a mask then store.
1212 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1213 DAG.getConstant(1, Tmp2.getValueType()));
1214 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1215 Node->getOperand(3), DAG.getValueType(MVT::i8));
1216
1217 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1218 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001219 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001220 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001221 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001222 break;
1223 case Promote:
1224 case Expand:
1225 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1226 }
1227 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001228 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001229 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1230 case Expand: assert(0 && "It's impossible to expand bools");
1231 case Legal:
1232 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1233 break;
1234 case Promote:
1235 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1236 break;
1237 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001238 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001239 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001240
Nate Begeman987121a2005-08-23 04:29:48 +00001241 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001242 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001243 case TargetLowering::Expand:
1244 if (Tmp1.getOpcode() == ISD::SETCC) {
1245 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1246 Tmp2, Tmp3,
1247 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1248 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001249 // Make sure the condition is either zero or one. It may have been
1250 // promoted from something else.
1251 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001252 Result = DAG.getSelectCC(Tmp1,
1253 DAG.getConstant(0, Tmp1.getValueType()),
1254 Tmp2, Tmp3, ISD::SETNE);
1255 }
1256 break;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001257 case TargetLowering::Legal:
1258 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1259 Tmp3 != Node->getOperand(2))
1260 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1261 Tmp1, Tmp2, Tmp3);
1262 break;
1263 case TargetLowering::Promote: {
1264 MVT::ValueType NVT =
1265 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1266 unsigned ExtOp, TruncOp;
1267 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001268 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001269 TruncOp = ISD::TRUNCATE;
1270 } else {
1271 ExtOp = ISD::FP_EXTEND;
1272 TruncOp = ISD::FP_ROUND;
1273 }
1274 // Promote each of the values to the new type.
1275 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1276 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1277 // Perform the larger operation, then round down.
1278 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1279 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1280 break;
1281 }
1282 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001283 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001284 case ISD::SELECT_CC:
1285 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1286 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1287
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001288 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001289 // Everything is legal, see if we should expand this op or something.
1290 switch (TLI.getOperationAction(ISD::SELECT_CC,
1291 Node->getOperand(0).getValueType())) {
1292 default: assert(0 && "This action is not supported yet!");
1293 case TargetLowering::Custom: {
1294 SDOperand Tmp =
1295 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1296 Node->getOperand(0),
1297 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001298 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001299 if (Tmp.Val) {
1300 Result = LegalizeOp(Tmp);
1301 break;
1302 }
1303 } // FALLTHROUGH if the target can't lower this operation after all.
1304 case TargetLowering::Legal:
1305 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1306 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1307 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1308 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1309 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1310 Tmp3, Tmp4, Node->getOperand(4));
1311 }
1312 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001313 }
1314 break;
1315 } else {
1316 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1317 Node->getOperand(0), // LHS
1318 Node->getOperand(1), // RHS
1319 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001320 // If we get a SETCC back from legalizing the SETCC node we just
1321 // created, then use its LHS, RHS, and CC directly in creating a new
1322 // node. Otherwise, select between the true and false value based on
1323 // comparing the result of the legalized with zero.
1324 if (Tmp1.getOpcode() == ISD::SETCC) {
1325 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1326 Tmp1.getOperand(0), Tmp1.getOperand(1),
1327 Tmp3, Tmp4, Tmp1.getOperand(2));
1328 } else {
1329 Result = DAG.getSelectCC(Tmp1,
1330 DAG.getConstant(0, Tmp1.getValueType()),
1331 Tmp3, Tmp4, ISD::SETNE);
1332 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001333 }
1334 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001335 case ISD::SETCC:
1336 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1337 case Legal:
1338 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1339 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001340 break;
1341 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001342 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1343 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1344
1345 // If this is an FP compare, the operands have already been extended.
1346 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1347 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001348 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001349
1350 // Otherwise, we have to insert explicit sign or zero extends. Note
1351 // that we could insert sign extends for ALL conditions, but zero extend
1352 // is cheaper on many machines (an AND instead of two shifts), so prefer
1353 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001354 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001355 default: assert(0 && "Unknown integer comparison!");
1356 case ISD::SETEQ:
1357 case ISD::SETNE:
1358 case ISD::SETUGE:
1359 case ISD::SETUGT:
1360 case ISD::SETULE:
1361 case ISD::SETULT:
1362 // ALL of these operations will work if we either sign or zero extend
1363 // the operands (including the unsigned comparisons!). Zero extend is
1364 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001365 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1366 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001367 break;
1368 case ISD::SETGE:
1369 case ISD::SETGT:
1370 case ISD::SETLT:
1371 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001372 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1373 DAG.getValueType(VT));
1374 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1375 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001376 break;
1377 }
Chris Lattner4d978642005-01-15 22:16:26 +00001378 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001379 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001380 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001381 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1382 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1383 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001384 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001385 case ISD::SETEQ:
1386 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001387 if (RHSLo == RHSHi)
1388 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1389 if (RHSCST->isAllOnesValue()) {
1390 // Comparison to -1.
1391 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001392 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001393 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001394 }
1395
Chris Lattnerdc750592005-01-07 07:47:09 +00001396 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1397 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1398 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001399 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001400 break;
1401 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001402 // If this is a comparison of the sign bit, just look at the top part.
1403 // X > -1, x < 0
1404 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001405 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001406 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001407 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001408 (CST->isAllOnesValue()))) { // X > -1
1409 Tmp1 = LHSHi;
1410 Tmp2 = RHSHi;
1411 break;
1412 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001413
Chris Lattnerdc750592005-01-07 07:47:09 +00001414 // FIXME: This generated code sucks.
1415 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001416 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001417 default: assert(0 && "Unknown integer setcc!");
1418 case ISD::SETLT:
1419 case ISD::SETULT: LowCC = ISD::SETULT; break;
1420 case ISD::SETGT:
1421 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1422 case ISD::SETLE:
1423 case ISD::SETULE: LowCC = ISD::SETULE; break;
1424 case ISD::SETGE:
1425 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1426 }
Misha Brukman835702a2005-04-21 22:36:52 +00001427
Chris Lattnerdc750592005-01-07 07:47:09 +00001428 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1429 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1430 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1431
1432 // NOTE: on targets without efficient SELECT of bools, we can always use
1433 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001434 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1435 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1436 Node->getOperand(2));
1437 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001438 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1439 Result, Tmp1, Tmp2));
1440 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001441 }
1442 }
Nate Begeman987121a2005-08-23 04:29:48 +00001443
1444 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1445 default:
1446 assert(0 && "Cannot handle this action for SETCC yet!");
1447 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001448 case TargetLowering::Promote: {
1449 // First step, figure out the appropriate operation to use.
1450 // Allow SETCC to not be supported for all legal data types
1451 // Mostly this targets FP
1452 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1453 MVT::ValueType OldVT = NewInTy;
1454
1455 // Scan for the appropriate larger type to use.
1456 while (1) {
1457 NewInTy = (MVT::ValueType)(NewInTy+1);
1458
1459 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1460 "Fell off of the edge of the integer world");
1461 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1462 "Fell off of the edge of the floating point world");
1463
1464 // If the target supports SETCC of this type, use it.
1465 if (TLI.getOperationAction(ISD::SETCC, NewInTy) == TargetLowering::Legal)
1466 break;
1467 }
1468 if (MVT::isInteger(NewInTy))
1469 assert(0 && "Cannot promote Legal Integer SETCC yet");
1470 else {
1471 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1472 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1473 }
1474
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001475 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1476 Node->getOperand(2));
1477 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001478 }
Nate Begeman987121a2005-08-23 04:29:48 +00001479 case TargetLowering::Legal:
1480 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1481 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1482 Node->getOperand(2));
1483 break;
1484 case TargetLowering::Expand:
1485 // Expand a setcc node into a select_cc of the same condition, lhs, and
1486 // rhs that selects between const 1 (true) and const 0 (false).
1487 MVT::ValueType VT = Node->getValueType(0);
1488 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1489 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1490 Node->getOperand(2));
1491 Result = LegalizeOp(Result);
1492 break;
1493 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001494 break;
1495
Chris Lattner85d70c62005-01-11 05:57:22 +00001496 case ISD::MEMSET:
1497 case ISD::MEMCPY:
1498 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001499 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001500 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1501
1502 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1503 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1504 case Expand: assert(0 && "Cannot expand a byte!");
1505 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001506 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001507 break;
1508 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001509 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001510 break;
1511 }
1512 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001513 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001514 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001515
1516 SDOperand Tmp4;
1517 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001518 case Expand: {
1519 // Length is too big, just take the lo-part of the length.
1520 SDOperand HiPart;
1521 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1522 break;
1523 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001524 case Legal:
1525 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001526 break;
1527 case Promote:
1528 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001529 break;
1530 }
1531
1532 SDOperand Tmp5;
1533 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1534 case Expand: assert(0 && "Cannot expand this yet!");
1535 case Legal:
1536 Tmp5 = LegalizeOp(Node->getOperand(4));
1537 break;
1538 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001539 Tmp5 = PromoteOp(Node->getOperand(4));
1540 break;
1541 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001542
1543 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1544 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001545 case TargetLowering::Custom: {
1546 SDOperand Tmp =
1547 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1548 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1549 if (Tmp.Val) {
1550 Result = LegalizeOp(Tmp);
1551 break;
1552 }
1553 // FALLTHROUGH if the target thinks it is legal.
1554 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001555 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001556 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1557 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1558 Tmp5 != Node->getOperand(4)) {
1559 std::vector<SDOperand> Ops;
1560 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1561 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1562 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1563 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001564 break;
1565 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001566 // Otherwise, the target does not support this operation. Lower the
1567 // operation to an explicit libcall as appropriate.
1568 MVT::ValueType IntPtr = TLI.getPointerTy();
1569 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1570 std::vector<std::pair<SDOperand, const Type*> > Args;
1571
Reid Spencer6dced922005-01-12 14:53:45 +00001572 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001573 if (Node->getOpcode() == ISD::MEMSET) {
1574 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1575 // Extend the ubyte argument to be an int value for the call.
1576 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1577 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1578 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1579
1580 FnName = "memset";
1581 } else if (Node->getOpcode() == ISD::MEMCPY ||
1582 Node->getOpcode() == ISD::MEMMOVE) {
1583 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1584 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1585 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1586 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1587 } else {
1588 assert(0 && "Unknown op!");
1589 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001590
Chris Lattner85d70c62005-01-11 05:57:22 +00001591 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001592 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001593 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001594 Result = CallResult.second;
1595 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001596 break;
1597 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001598 }
1599 break;
1600 }
Chris Lattner5385db52005-05-09 20:23:03 +00001601
1602 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001603 Tmp1 = LegalizeOp(Node->getOperand(0));
1604 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001605
Chris Lattner86535992005-05-14 07:45:46 +00001606 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1607 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1608 std::vector<SDOperand> Ops;
1609 Ops.push_back(Tmp1);
1610 Ops.push_back(Tmp2);
1611 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1612 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001613 Result = SDOperand(Node, 0);
1614 // Since these produce two values, make sure to remember that we legalized
1615 // both of them.
1616 AddLegalizedOperand(SDOperand(Node, 0), Result);
1617 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1618 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001619 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001620 Tmp1 = LegalizeOp(Node->getOperand(0));
1621 Tmp2 = LegalizeOp(Node->getOperand(1));
1622 Tmp3 = LegalizeOp(Node->getOperand(2));
1623 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1624 Tmp3 != Node->getOperand(2))
1625 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1626 break;
1627
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001628 case ISD::READIO:
1629 Tmp1 = LegalizeOp(Node->getOperand(0));
1630 Tmp2 = LegalizeOp(Node->getOperand(1));
1631
1632 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1633 case TargetLowering::Custom:
1634 default: assert(0 && "This action not implemented for this operation!");
1635 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001636 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1637 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1638 std::vector<SDOperand> Ops;
1639 Ops.push_back(Tmp1);
1640 Ops.push_back(Tmp2);
1641 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1642 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001643 Result = SDOperand(Node, 0);
1644 break;
1645 case TargetLowering::Expand:
1646 // Replace this with a load from memory.
1647 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1648 Node->getOperand(1), DAG.getSrcValue(NULL));
1649 Result = LegalizeOp(Result);
1650 break;
1651 }
1652
1653 // Since these produce two values, make sure to remember that we legalized
1654 // both of them.
1655 AddLegalizedOperand(SDOperand(Node, 0), Result);
1656 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1657 return Result.getValue(Op.ResNo);
1658
1659 case ISD::WRITEIO:
1660 Tmp1 = LegalizeOp(Node->getOperand(0));
1661 Tmp2 = LegalizeOp(Node->getOperand(1));
1662 Tmp3 = LegalizeOp(Node->getOperand(2));
1663
1664 switch (TLI.getOperationAction(Node->getOpcode(),
1665 Node->getOperand(1).getValueType())) {
1666 case TargetLowering::Custom:
1667 default: assert(0 && "This action not implemented for this operation!");
1668 case TargetLowering::Legal:
1669 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1670 Tmp3 != Node->getOperand(2))
1671 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1672 break;
1673 case TargetLowering::Expand:
1674 // Replace this with a store to memory.
1675 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1676 Node->getOperand(1), Node->getOperand(2),
1677 DAG.getSrcValue(NULL));
1678 Result = LegalizeOp(Result);
1679 break;
1680 }
1681 break;
1682
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001683 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001684 case ISD::SUB_PARTS:
1685 case ISD::SHL_PARTS:
1686 case ISD::SRA_PARTS:
1687 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001688 std::vector<SDOperand> Ops;
1689 bool Changed = false;
1690 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1691 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1692 Changed |= Ops.back() != Node->getOperand(i);
1693 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001694 if (Changed) {
1695 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1696 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1697 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001698
1699 // Since these produce multiple values, make sure to remember that we
1700 // legalized all of them.
1701 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1702 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1703 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001704 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001705
1706 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001707 case ISD::ADD:
1708 case ISD::SUB:
1709 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001710 case ISD::MULHS:
1711 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001712 case ISD::UDIV:
1713 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001714 case ISD::AND:
1715 case ISD::OR:
1716 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001717 case ISD::SHL:
1718 case ISD::SRL:
1719 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001720 case ISD::FADD:
1721 case ISD::FSUB:
1722 case ISD::FMUL:
1723 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001724 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001725 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1726 case Expand: assert(0 && "Not possible");
1727 case Legal:
1728 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1729 break;
1730 case Promote:
1731 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1732 break;
1733 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001734 if (Tmp1 != Node->getOperand(0) ||
1735 Tmp2 != Node->getOperand(1))
1736 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1737 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001738
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001739 case ISD::BUILD_PAIR: {
1740 MVT::ValueType PairTy = Node->getValueType(0);
1741 // TODO: handle the case where the Lo and Hi operands are not of legal type
1742 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1743 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1744 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1745 case TargetLowering::Legal:
1746 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1747 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1748 break;
1749 case TargetLowering::Promote:
1750 case TargetLowering::Custom:
1751 assert(0 && "Cannot promote/custom this yet!");
1752 case TargetLowering::Expand:
1753 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1754 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1755 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1756 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1757 TLI.getShiftAmountTy()));
1758 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1759 break;
1760 }
1761 break;
1762 }
1763
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001764 case ISD::UREM:
1765 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001766 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001767 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1768 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1769 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1770 case TargetLowering::Legal:
1771 if (Tmp1 != Node->getOperand(0) ||
1772 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001773 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001774 Tmp2);
1775 break;
1776 case TargetLowering::Promote:
1777 case TargetLowering::Custom:
1778 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001779 case TargetLowering::Expand:
1780 if (MVT::isInteger(Node->getValueType(0))) {
1781 MVT::ValueType VT = Node->getValueType(0);
1782 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1783 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1784 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1785 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1786 } else {
1787 // Floating point mod -> fmod libcall.
1788 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1789 SDOperand Dummy;
1790 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001791 }
1792 break;
1793 }
1794 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001795
Andrew Lenharth5e177822005-05-03 17:19:30 +00001796 case ISD::CTPOP:
1797 case ISD::CTTZ:
1798 case ISD::CTLZ:
1799 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1800 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1801 case TargetLowering::Legal:
1802 if (Tmp1 != Node->getOperand(0))
1803 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1804 break;
1805 case TargetLowering::Promote: {
1806 MVT::ValueType OVT = Tmp1.getValueType();
1807 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001808
1809 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001810 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1811 // Perform the larger operation, then subtract if needed.
1812 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1813 switch(Node->getOpcode())
1814 {
1815 case ISD::CTPOP:
1816 Result = Tmp1;
1817 break;
1818 case ISD::CTTZ:
1819 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001820 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1821 DAG.getConstant(getSizeInBits(NVT), NVT),
1822 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001823 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001824 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1825 break;
1826 case ISD::CTLZ:
1827 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001828 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1829 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001830 getSizeInBits(OVT), NVT));
1831 break;
1832 }
1833 break;
1834 }
1835 case TargetLowering::Custom:
1836 assert(0 && "Cannot custom handle this yet!");
1837 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001838 switch(Node->getOpcode())
1839 {
1840 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001841 static const uint64_t mask[6] = {
1842 0x5555555555555555ULL, 0x3333333333333333ULL,
1843 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1844 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1845 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001846 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001847 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1848 unsigned len = getSizeInBits(VT);
1849 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001850 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001851 Tmp2 = DAG.getConstant(mask[i], VT);
1852 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001853 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001854 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1855 DAG.getNode(ISD::AND, VT,
1856 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1857 Tmp2));
1858 }
1859 Result = Tmp1;
1860 break;
1861 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001862 case ISD::CTLZ: {
1863 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001864 x = x | (x >> 1);
1865 x = x | (x >> 2);
1866 ...
1867 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001868 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001869 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001870
Chris Lattner56add052005-05-11 18:35:21 +00001871 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1872 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001873 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1874 unsigned len = getSizeInBits(VT);
1875 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1876 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001877 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001878 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1879 }
1880 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001881 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001882 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001883 }
1884 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001885 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001886 // unless the target has ctlz but not ctpop, in which case we use:
1887 // { return 32 - nlz(~x & (x-1)); }
1888 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001889 MVT::ValueType VT = Tmp1.getValueType();
1890 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001891 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001892 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1893 DAG.getNode(ISD::SUB, VT, Tmp1,
1894 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001895 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001896 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1897 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001898 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001899 DAG.getConstant(getSizeInBits(VT), VT),
1900 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1901 } else {
1902 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1903 }
Chris Lattner72473242005-05-11 05:27:09 +00001904 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001905 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001906 default:
1907 assert(0 && "Cannot expand this yet!");
1908 break;
1909 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001910 break;
1911 }
1912 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001913
Chris Lattner13fe99c2005-04-02 05:00:07 +00001914 // Unary operators
1915 case ISD::FABS:
1916 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001917 case ISD::FSQRT:
1918 case ISD::FSIN:
1919 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001920 Tmp1 = LegalizeOp(Node->getOperand(0));
1921 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1922 case TargetLowering::Legal:
1923 if (Tmp1 != Node->getOperand(0))
1924 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1925 break;
1926 case TargetLowering::Promote:
1927 case TargetLowering::Custom:
1928 assert(0 && "Cannot promote/custom handle this yet!");
1929 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001930 switch(Node->getOpcode()) {
1931 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001932 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1933 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00001934 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00001935 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001936 break;
1937 }
1938 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001939 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1940 MVT::ValueType VT = Node->getValueType(0);
1941 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001942 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001943 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1944 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1945 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001946 break;
1947 }
1948 case ISD::FSQRT:
1949 case ISD::FSIN:
1950 case ISD::FCOS: {
1951 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00001952 const char *FnName = 0;
1953 switch(Node->getOpcode()) {
1954 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1955 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1956 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1957 default: assert(0 && "Unreachable!");
1958 }
Nate Begeman77558da2005-08-04 21:43:28 +00001959 SDOperand Dummy;
1960 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00001961 break;
1962 }
1963 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001964 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001965 }
1966 break;
1967 }
1968 break;
1969
1970 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001971 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001972 case ISD::UINT_TO_FP: {
1973 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001974 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1975 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00001976 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001977 Node->getOperand(0).getValueType())) {
1978 default: assert(0 && "Unknown operation action!");
1979 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00001980 Result = ExpandLegalINT_TO_FP(isSigned,
1981 LegalizeOp(Node->getOperand(0)),
1982 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001983 AddLegalizedOperand(Op, Result);
1984 return Result;
1985 case TargetLowering::Promote:
1986 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1987 Node->getValueType(0),
1988 isSigned);
1989 AddLegalizedOperand(Op, Result);
1990 return Result;
1991 case TargetLowering::Legal:
1992 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00001993 case TargetLowering::Custom: {
1994 Tmp1 = LegalizeOp(Node->getOperand(0));
1995 SDOperand Tmp =
1996 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1997 Tmp = TLI.LowerOperation(Tmp, DAG);
1998 if (Tmp.Val) {
1999 AddLegalizedOperand(Op, Tmp);
2000 NeedsAnotherIteration = true;
2001 return Tmp;
2002 } else {
2003 assert(0 && "Target Must Lower this");
2004 }
2005 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002006 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002007
Chris Lattnerdc750592005-01-07 07:47:09 +00002008 Tmp1 = LegalizeOp(Node->getOperand(0));
2009 if (Tmp1 != Node->getOperand(0))
2010 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2011 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002012 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002013 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2014 Node->getValueType(0), Node->getOperand(0));
2015 break;
2016 case Promote:
2017 if (isSigned) {
2018 Result = PromoteOp(Node->getOperand(0));
2019 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2020 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2021 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2022 } else {
2023 Result = PromoteOp(Node->getOperand(0));
2024 Result = DAG.getZeroExtendInReg(Result,
2025 Node->getOperand(0).getValueType());
2026 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002027 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002028 break;
2029 }
2030 break;
2031 }
2032 case ISD::TRUNCATE:
2033 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2034 case Legal:
2035 Tmp1 = LegalizeOp(Node->getOperand(0));
2036 if (Tmp1 != Node->getOperand(0))
2037 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2038 break;
2039 case Expand:
2040 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2041
2042 // Since the result is legal, we should just be able to truncate the low
2043 // part of the source.
2044 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2045 break;
2046 case Promote:
2047 Result = PromoteOp(Node->getOperand(0));
2048 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2049 break;
2050 }
2051 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002052
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002053 case ISD::FP_TO_SINT:
2054 case ISD::FP_TO_UINT:
2055 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2056 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002057 Tmp1 = LegalizeOp(Node->getOperand(0));
2058
Chris Lattner44fe26f2005-07-29 00:11:56 +00002059 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2060 default: assert(0 && "Unknown operation action!");
2061 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002062 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2063 SDOperand True, False;
2064 MVT::ValueType VT = Node->getOperand(0).getValueType();
2065 MVT::ValueType NVT = Node->getValueType(0);
2066 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2067 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2068 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2069 Node->getOperand(0), Tmp2, ISD::SETLT);
2070 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2071 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002072 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002073 Tmp2));
2074 False = DAG.getNode(ISD::XOR, NVT, False,
2075 DAG.getConstant(1ULL << ShiftAmt, NVT));
2076 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00002077 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002078 } else {
2079 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2080 }
2081 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002082 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002083 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002084 Node->getOpcode() == ISD::FP_TO_SINT);
2085 AddLegalizedOperand(Op, Result);
2086 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002087 case TargetLowering::Custom: {
2088 SDOperand Tmp =
2089 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2090 Tmp = TLI.LowerOperation(Tmp, DAG);
2091 if (Tmp.Val) {
2092 AddLegalizedOperand(Op, Tmp);
2093 NeedsAnotherIteration = true;
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002094 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002095 } else {
2096 // The target thinks this is legal afterall.
2097 break;
2098 }
2099 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002100 case TargetLowering::Legal:
2101 break;
2102 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002103
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002104 if (Tmp1 != Node->getOperand(0))
2105 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2106 break;
2107 case Expand:
2108 assert(0 && "Shouldn't need to expand other operators here!");
2109 case Promote:
2110 Result = PromoteOp(Node->getOperand(0));
2111 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2112 break;
2113 }
2114 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002115
Chris Lattner7753f172005-09-02 00:18:10 +00002116 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002117 case ISD::ZERO_EXTEND:
2118 case ISD::SIGN_EXTEND:
2119 case ISD::FP_EXTEND:
2120 case ISD::FP_ROUND:
2121 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2122 case Legal:
2123 Tmp1 = LegalizeOp(Node->getOperand(0));
2124 if (Tmp1 != Node->getOperand(0))
2125 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2126 break;
2127 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002128 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002129
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002130 case Promote:
2131 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002132 case ISD::ANY_EXTEND:
2133 Result = PromoteOp(Node->getOperand(0));
2134 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2135 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002136 case ISD::ZERO_EXTEND:
2137 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002138 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002139 Result = DAG.getZeroExtendInReg(Result,
2140 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002141 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002142 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002143 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002144 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002145 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002146 Result,
2147 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002148 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002149 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002150 Result = PromoteOp(Node->getOperand(0));
2151 if (Result.getValueType() != Op.getValueType())
2152 // Dynamically dead while we have only 2 FP types.
2153 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2154 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002155 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002156 Result = PromoteOp(Node->getOperand(0));
2157 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2158 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002159 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002160 }
2161 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002162 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002163 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002164 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002165 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002166
2167 // If this operation is not supported, convert it to a shl/shr or load/store
2168 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002169 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2170 default: assert(0 && "This action not supported for this op yet!");
2171 case TargetLowering::Legal:
2172 if (Tmp1 != Node->getOperand(0))
2173 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002174 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002175 break;
2176 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002177 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002178 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002179 // NOTE: we could fall back on load/store here too for targets without
2180 // SAR. However, it is doubtful that any exist.
2181 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2182 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002183 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002184 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2185 Node->getOperand(0), ShiftCst);
2186 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2187 Result, ShiftCst);
2188 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2189 // The only way we can lower this is to turn it into a STORETRUNC,
2190 // EXTLOAD pair, targetting a temporary location (a stack slot).
2191
2192 // NOTE: there is a choice here between constantly creating new stack
2193 // slots and always reusing the same one. We currently always create
2194 // new ones, as reuse may inhibit scheduling.
2195 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2196 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2197 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2198 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002199 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002200 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2201 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2202 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002203 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002204 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002205 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2206 Result, StackSlot, DAG.getSrcValue(NULL),
2207 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002208 } else {
2209 assert(0 && "Unknown op");
2210 }
2211 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002212 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002213 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002214 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002215 }
Chris Lattner99222f72005-01-15 07:15:18 +00002216 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002217
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002218 // Note that LegalizeOp may be reentered even from single-use nodes, which
2219 // means that we always must cache transformed nodes.
2220 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002221 return Result;
2222}
2223
Chris Lattner4d978642005-01-15 22:16:26 +00002224/// PromoteOp - Given an operation that produces a value in an invalid type,
2225/// promote it to compute the value into a larger type. The produced value will
2226/// have the correct bits for the low portion of the register, but no guarantee
2227/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002228SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2229 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002230 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002231 assert(getTypeAction(VT) == Promote &&
2232 "Caller should expand or legalize operands that are not promotable!");
2233 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2234 "Cannot promote to smaller type!");
2235
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002236 SDOperand Tmp1, Tmp2, Tmp3;
2237
2238 SDOperand Result;
2239 SDNode *Node = Op.Val;
2240
Chris Lattner1a570f12005-09-02 20:32:45 +00002241 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2242 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002243
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002244 // Promotion needs an optimization step to clean up after it, and is not
2245 // careful to avoid operations the target does not support. Make sure that
2246 // all generated operations are legalized in the next iteration.
2247 NeedsAnotherIteration = true;
2248
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002249 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002250 case ISD::CopyFromReg:
2251 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002252 default:
2253 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2254 assert(0 && "Do not know how to promote this operator!");
2255 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002256 case ISD::UNDEF:
2257 Result = DAG.getNode(ISD::UNDEF, NVT);
2258 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002259 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002260 if (VT != MVT::i1)
2261 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2262 else
2263 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002264 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2265 break;
2266 case ISD::ConstantFP:
2267 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2268 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2269 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002270
Chris Lattner2cb338d2005-01-18 02:59:52 +00002271 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002272 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002273 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2274 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002275 Result = LegalizeOp(Result);
2276 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002277
2278 case ISD::TRUNCATE:
2279 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2280 case Legal:
2281 Result = LegalizeOp(Node->getOperand(0));
2282 assert(Result.getValueType() >= NVT &&
2283 "This truncation doesn't make sense!");
2284 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2285 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2286 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002287 case Promote:
2288 // The truncation is not required, because we don't guarantee anything
2289 // about high bits anyway.
2290 Result = PromoteOp(Node->getOperand(0));
2291 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002292 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002293 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2294 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002295 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002296 }
2297 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002298 case ISD::SIGN_EXTEND:
2299 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002300 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002301 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2302 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2303 case Legal:
2304 // Input is legal? Just do extend all the way to the larger type.
2305 Result = LegalizeOp(Node->getOperand(0));
2306 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2307 break;
2308 case Promote:
2309 // Promote the reg if it's smaller.
2310 Result = PromoteOp(Node->getOperand(0));
2311 // The high bits are not guaranteed to be anything. Insert an extend.
2312 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002313 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002314 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002315 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002316 Result = DAG.getZeroExtendInReg(Result,
2317 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002318 break;
2319 }
2320 break;
2321
2322 case ISD::FP_EXTEND:
2323 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2324 case ISD::FP_ROUND:
2325 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2326 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2327 case Promote: assert(0 && "Unreachable with 2 FP types!");
2328 case Legal:
2329 // Input is legal? Do an FP_ROUND_INREG.
2330 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002331 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2332 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002333 break;
2334 }
2335 break;
2336
2337 case ISD::SINT_TO_FP:
2338 case ISD::UINT_TO_FP:
2339 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2340 case Legal:
2341 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002342 // No extra round required here.
2343 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002344 break;
2345
2346 case Promote:
2347 Result = PromoteOp(Node->getOperand(0));
2348 if (Node->getOpcode() == ISD::SINT_TO_FP)
2349 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002350 Result,
2351 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002352 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002353 Result = DAG.getZeroExtendInReg(Result,
2354 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002355 // No extra round required here.
2356 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002357 break;
2358 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002359 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2360 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002361 // Round if we cannot tolerate excess precision.
2362 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002363 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2364 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002365 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002366 }
Chris Lattner4d978642005-01-15 22:16:26 +00002367 break;
2368
2369 case ISD::FP_TO_SINT:
2370 case ISD::FP_TO_UINT:
2371 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2372 case Legal:
2373 Tmp1 = LegalizeOp(Node->getOperand(0));
2374 break;
2375 case Promote:
2376 // The input result is prerounded, so we don't have to do anything
2377 // special.
2378 Tmp1 = PromoteOp(Node->getOperand(0));
2379 break;
2380 case Expand:
2381 assert(0 && "not implemented");
2382 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002383 // If we're promoting a UINT to a larger size, check to see if the new node
2384 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2385 // we can use that instead. This allows us to generate better code for
2386 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2387 // legal, such as PowerPC.
2388 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002389 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002390 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2391 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002392 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2393 } else {
2394 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2395 }
Chris Lattner4d978642005-01-15 22:16:26 +00002396 break;
2397
Chris Lattner13fe99c2005-04-02 05:00:07 +00002398 case ISD::FABS:
2399 case ISD::FNEG:
2400 Tmp1 = PromoteOp(Node->getOperand(0));
2401 assert(Tmp1.getValueType() == NVT);
2402 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2403 // NOTE: we do not have to do any extra rounding here for
2404 // NoExcessFPPrecision, because we know the input will have the appropriate
2405 // precision, and these operations don't modify precision at all.
2406 break;
2407
Chris Lattner9d6fa982005-04-28 21:44:33 +00002408 case ISD::FSQRT:
2409 case ISD::FSIN:
2410 case ISD::FCOS:
2411 Tmp1 = PromoteOp(Node->getOperand(0));
2412 assert(Tmp1.getValueType() == NVT);
2413 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2414 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002415 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2416 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002417 break;
2418
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002419 case ISD::AND:
2420 case ISD::OR:
2421 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002422 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002423 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002424 case ISD::MUL:
2425 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002426 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002427 // that too is okay if they are integer operations.
2428 Tmp1 = PromoteOp(Node->getOperand(0));
2429 Tmp2 = PromoteOp(Node->getOperand(1));
2430 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2431 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002432 break;
2433 case ISD::FADD:
2434 case ISD::FSUB:
2435 case ISD::FMUL:
2436 // The input may have strange things in the top bits of the registers, but
2437 // these operations don't care.
2438 Tmp1 = PromoteOp(Node->getOperand(0));
2439 Tmp2 = PromoteOp(Node->getOperand(1));
2440 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2441 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2442
2443 // Floating point operations will give excess precision that we may not be
2444 // able to tolerate. If we DO allow excess precision, just leave it,
2445 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002446 // FIXME: Why would we need to round FP ops more than integer ones?
2447 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002448 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002449 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2450 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002451 break;
2452
Chris Lattner4d978642005-01-15 22:16:26 +00002453 case ISD::SDIV:
2454 case ISD::SREM:
2455 // These operators require that their input be sign extended.
2456 Tmp1 = PromoteOp(Node->getOperand(0));
2457 Tmp2 = PromoteOp(Node->getOperand(1));
2458 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002459 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2460 DAG.getValueType(VT));
2461 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2462 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002463 }
2464 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2465
2466 // Perform FP_ROUND: this is probably overly pessimistic.
2467 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002468 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2469 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002470 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002471 case ISD::FDIV:
2472 case ISD::FREM:
2473 // These operators require that their input be fp extended.
2474 Tmp1 = PromoteOp(Node->getOperand(0));
2475 Tmp2 = PromoteOp(Node->getOperand(1));
2476 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2477
2478 // Perform FP_ROUND: this is probably overly pessimistic.
2479 if (NoExcessFPPrecision)
2480 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2481 DAG.getValueType(VT));
2482 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002483
2484 case ISD::UDIV:
2485 case ISD::UREM:
2486 // These operators require that their input be zero extended.
2487 Tmp1 = PromoteOp(Node->getOperand(0));
2488 Tmp2 = PromoteOp(Node->getOperand(1));
2489 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002490 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2491 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002492 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2493 break;
2494
2495 case ISD::SHL:
2496 Tmp1 = PromoteOp(Node->getOperand(0));
2497 Tmp2 = LegalizeOp(Node->getOperand(1));
2498 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2499 break;
2500 case ISD::SRA:
2501 // The input value must be properly sign extended.
2502 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002503 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2504 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002505 Tmp2 = LegalizeOp(Node->getOperand(1));
2506 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2507 break;
2508 case ISD::SRL:
2509 // The input value must be properly zero extended.
2510 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002511 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002512 Tmp2 = LegalizeOp(Node->getOperand(1));
2513 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2514 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002515 case ISD::LOAD:
2516 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2517 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002518 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2519 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002520 // Remember that we legalized the chain.
2521 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2522 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002523 case ISD::SEXTLOAD:
2524 case ISD::ZEXTLOAD:
2525 case ISD::EXTLOAD:
2526 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2527 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002528 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2529 Node->getOperand(2),
2530 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002531 // Remember that we legalized the chain.
2532 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2533 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002534 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002535 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2536 case Expand: assert(0 && "It's impossible to expand bools");
2537 case Legal:
2538 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2539 break;
2540 case Promote:
2541 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2542 break;
2543 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002544 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2545 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2546 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2547 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002548 case ISD::SELECT_CC:
2549 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2550 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2551 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2552 Node->getOperand(1), Tmp2, Tmp3,
2553 Node->getOperand(4));
2554 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002555 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002556 case ISD::CALL: {
2557 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2558 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2559
Chris Lattner3d95c142005-01-19 20:24:35 +00002560 std::vector<SDOperand> Ops;
2561 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2562 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2563
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002564 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2565 "Can only promote single result calls");
2566 std::vector<MVT::ValueType> RetTyVTs;
2567 RetTyVTs.reserve(2);
2568 RetTyVTs.push_back(NVT);
2569 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002570 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2571 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002572 Result = SDOperand(NC, 0);
2573
2574 // Insert the new chain mapping.
2575 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2576 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002577 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002578 case ISD::CTPOP:
2579 case ISD::CTTZ:
2580 case ISD::CTLZ:
2581 Tmp1 = Node->getOperand(0);
2582 //Zero extend the argument
2583 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2584 // Perform the larger operation, then subtract if needed.
2585 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2586 switch(Node->getOpcode())
2587 {
2588 case ISD::CTPOP:
2589 Result = Tmp1;
2590 break;
2591 case ISD::CTTZ:
2592 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002593 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002594 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002595 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002596 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2597 break;
2598 case ISD::CTLZ:
2599 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002600 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2601 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002602 getSizeInBits(VT), NVT));
2603 break;
2604 }
2605 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002606 }
2607
2608 assert(Result.Val && "Didn't set a result!");
2609 AddPromotedOperand(Op, Result);
2610 return Result;
2611}
Chris Lattnerdc750592005-01-07 07:47:09 +00002612
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002613/// ExpandAddSub - Find a clever way to expand this add operation into
2614/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002615void SelectionDAGLegalize::
2616ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2617 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002618 // Expand the subcomponents.
2619 SDOperand LHSL, LHSH, RHSL, RHSH;
2620 ExpandOp(LHS, LHSL, LHSH);
2621 ExpandOp(RHS, RHSL, RHSH);
2622
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002623 std::vector<SDOperand> Ops;
2624 Ops.push_back(LHSL);
2625 Ops.push_back(LHSH);
2626 Ops.push_back(RHSL);
2627 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002628 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2629 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002630 Hi = Lo.getValue(1);
2631}
2632
Chris Lattner4157c412005-04-02 04:00:59 +00002633void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2634 SDOperand Op, SDOperand Amt,
2635 SDOperand &Lo, SDOperand &Hi) {
2636 // Expand the subcomponents.
2637 SDOperand LHSL, LHSH;
2638 ExpandOp(Op, LHSL, LHSH);
2639
2640 std::vector<SDOperand> Ops;
2641 Ops.push_back(LHSL);
2642 Ops.push_back(LHSH);
2643 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002644 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002645 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002646 Hi = Lo.getValue(1);
2647}
2648
2649
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002650/// ExpandShift - Try to find a clever way to expand this shift operation out to
2651/// smaller elements. If we can't find a way that is more efficient than a
2652/// libcall on this target, return false. Otherwise, return true with the
2653/// low-parts expanded into Lo and Hi.
2654bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2655 SDOperand &Lo, SDOperand &Hi) {
2656 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2657 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002658
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002659 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002660 SDOperand ShAmt = LegalizeOp(Amt);
2661 MVT::ValueType ShTy = ShAmt.getValueType();
2662 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2663 unsigned NVTBits = MVT::getSizeInBits(NVT);
2664
2665 // Handle the case when Amt is an immediate. Other cases are currently broken
2666 // and are disabled.
2667 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2668 unsigned Cst = CN->getValue();
2669 // Expand the incoming operand to be shifted, so that we have its parts
2670 SDOperand InL, InH;
2671 ExpandOp(Op, InL, InH);
2672 switch(Opc) {
2673 case ISD::SHL:
2674 if (Cst > VTBits) {
2675 Lo = DAG.getConstant(0, NVT);
2676 Hi = DAG.getConstant(0, NVT);
2677 } else if (Cst > NVTBits) {
2678 Lo = DAG.getConstant(0, NVT);
2679 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002680 } else if (Cst == NVTBits) {
2681 Lo = DAG.getConstant(0, NVT);
2682 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002683 } else {
2684 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2685 Hi = DAG.getNode(ISD::OR, NVT,
2686 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2687 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2688 }
2689 return true;
2690 case ISD::SRL:
2691 if (Cst > VTBits) {
2692 Lo = DAG.getConstant(0, NVT);
2693 Hi = DAG.getConstant(0, NVT);
2694 } else if (Cst > NVTBits) {
2695 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2696 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002697 } else if (Cst == NVTBits) {
2698 Lo = InH;
2699 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002700 } else {
2701 Lo = DAG.getNode(ISD::OR, NVT,
2702 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2703 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2704 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2705 }
2706 return true;
2707 case ISD::SRA:
2708 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002709 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002710 DAG.getConstant(NVTBits-1, ShTy));
2711 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002712 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002713 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002714 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002715 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002716 } else if (Cst == NVTBits) {
2717 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002718 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002719 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002720 } else {
2721 Lo = DAG.getNode(ISD::OR, NVT,
2722 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2723 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2724 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2725 }
2726 return true;
2727 }
2728 }
2729 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2730 // so disable it for now. Currently targets are handling this via SHL_PARTS
2731 // and friends.
2732 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002733
2734 // If we have an efficient select operation (or if the selects will all fold
2735 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002736 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002737 return false;
2738
2739 SDOperand InL, InH;
2740 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002741 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2742 DAG.getConstant(NVTBits, ShTy), ShAmt);
2743
Chris Lattner4d25c042005-01-20 20:29:23 +00002744 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002745 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2746 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002747
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002748 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2749 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2750 DAG.getConstant(NVTBits-1, ShTy));
2751 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2752 DAG.getConstant(NVTBits-1, ShTy));
2753 }
2754
2755 if (Opc == ISD::SHL) {
2756 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2757 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2758 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002759 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002760
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002761 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2762 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2763 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002764 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002765 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2766 DAG.getConstant(32, ShTy),
2767 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002768 DAG.getConstant(0, NVT),
2769 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002770 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002771 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002772 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002773 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002774
2775 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002776 if (Opc == ISD::SRA)
2777 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2778 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002779 else
2780 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002781 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002782 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002783 }
2784 return true;
2785}
Chris Lattneraac464e2005-01-21 06:05:23 +00002786
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002787/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2788/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002789/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002790static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002791 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002792
Chris Lattner2dce7032005-05-12 23:24:06 +00002793 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002794 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002795 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002796 Found = Node;
2797 return;
2798 }
2799
2800 // Otherwise, scan the operands of Node to see if any of them is a call.
2801 assert(Node->getNumOperands() != 0 &&
2802 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002803 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002804 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002805
2806 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002807 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002808 Found);
2809}
2810
2811
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002812/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2813/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002814/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002815static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2816 std::set<SDNode*> &Visited) {
2817 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2818 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002819
Chris Lattner2dce7032005-05-12 23:24:06 +00002820 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002821 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002822 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002823 Found = Node;
2824 return;
2825 }
2826
2827 // Otherwise, scan the operands of Node to see if any of them is a call.
2828 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2829 if (UI == E) return;
2830 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002831 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002832
2833 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002834 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002835}
2836
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002837/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002838/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002839static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002840 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002841 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002842 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002843 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002844
Chris Lattner4add7e32005-01-23 04:42:50 +00002845 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002846 if (TheChain.getValueType() != MVT::Other)
2847 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00002848 if (TheChain.getValueType() != MVT::Other)
2849 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00002850
2851 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002852 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002853
Chris Lattner4add7e32005-01-23 04:42:50 +00002854 // Make sure to only follow users of our token chain.
2855 SDNode *User = *UI;
2856 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2857 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002858 if (SDNode *Result = FindCallSeqEnd(User))
2859 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002860 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002861 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002862}
2863
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002864/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002865/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002866static SDNode *FindCallSeqStart(SDNode *Node) {
2867 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002868 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002869
2870 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2871 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002872 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002873}
2874
2875
Chris Lattner4add7e32005-01-23 04:42:50 +00002876/// FindInputOutputChains - If we are replacing an operation with a call we need
2877/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002878/// properly serialize the calls in the block. The returned operand is the
2879/// input chain value for the new call (e.g. the entry node or the previous
2880/// call), and OutChain is set to be the chain node to update to point to the
2881/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002882static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2883 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002884 SDNode *LatestCallSeqStart = Entry.Val;
2885 SDNode *LatestCallSeqEnd = 0;
2886 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2887 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002888
Chris Lattner2dce7032005-05-12 23:24:06 +00002889 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002890 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002891 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2892 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00002893 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002894 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00002895 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2896 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002897 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00002898 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002899 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002900
Chris Lattner06bbeb62005-05-11 19:02:11 +00002901 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002902 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002903 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002904 std::set<SDNode*> Visited;
2905 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002906
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002907 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002908 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002909 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002910
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002911 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002912}
2913
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002914/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002915void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2916 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002917 // Nothing to splice it into?
2918 if (OutChain == 0) return;
2919
2920 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2921 //OutChain->dump();
2922
2923 // Form a token factor node merging the old inval and the new inval.
2924 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2925 OutChain->getOperand(0));
2926 // Change the node to refer to the new token.
2927 OutChain->setAdjCallChain(InToken);
2928}
Chris Lattner4add7e32005-01-23 04:42:50 +00002929
2930
Chris Lattneraac464e2005-01-21 06:05:23 +00002931// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2932// does not fit into a register, return the lo part and set the hi part to the
2933// by-reg argument. If it does fit into a single register, return the result
2934// and leave the Hi part unset.
2935SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2936 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002937 SDNode *OutChain;
2938 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2939 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002940 if (InChain.Val == 0)
2941 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002942
Chris Lattneraac464e2005-01-21 06:05:23 +00002943 TargetLowering::ArgListTy Args;
2944 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2945 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2946 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2947 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2948 }
2949 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002950
Chris Lattner06bbeb62005-05-11 19:02:11 +00002951 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002952 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002953 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002954 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2955 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002956
Chris Lattner63022662005-09-02 20:26:58 +00002957 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002958 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002959 default: assert(0 && "Unknown thing");
2960 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00002961 Result = CallInfo.first;
2962 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00002963 case Promote:
2964 assert(0 && "Cannot promote this yet!");
2965 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00002966 ExpandOp(CallInfo.first, Result, Hi);
2967 CallInfo.second = LegalizeOp(CallInfo.second);
2968 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00002969 }
Chris Lattner63022662005-09-02 20:26:58 +00002970
2971 SpliceCallInto(CallInfo.second, OutChain);
2972 NeedsAnotherIteration = true;
2973 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00002974}
2975
Chris Lattner4add7e32005-01-23 04:42:50 +00002976
Chris Lattneraac464e2005-01-21 06:05:23 +00002977/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2978/// destination type is legal.
2979SDOperand SelectionDAGLegalize::
2980ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002981 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00002982 assert(getTypeAction(Source.getValueType()) == Expand &&
2983 "This is not an expansion!");
2984 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2985
Chris Lattner06bbeb62005-05-11 19:02:11 +00002986 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002987 assert(Source.getValueType() == MVT::i64 &&
2988 "This only works for 64-bit -> FP");
2989 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2990 // incoming integer is set. To handle this, we dynamically test to see if
2991 // it is set, and, if so, add a fudge factor.
2992 SDOperand Lo, Hi;
2993 ExpandOp(Source, Lo, Hi);
2994
Chris Lattner2a4f7312005-05-13 04:45:13 +00002995 // If this is unsigned, and not supported, first perform the conversion to
2996 // signed, then adjust the result if the sign bit is set.
2997 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2998 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2999
Chris Lattnerd47675e2005-08-09 20:20:18 +00003000 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3001 DAG.getConstant(0, Hi.getValueType()),
3002 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003003 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3004 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3005 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003006 uint64_t FF = 0x5f800000ULL;
3007 if (TLI.isLittleEndian()) FF <<= 32;
3008 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003009
Chris Lattnerc30405e2005-08-26 17:15:30 +00003010 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003011 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3012 SDOperand FudgeInReg;
3013 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003014 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3015 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003016 else {
3017 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003018 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3019 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003020 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003021 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003022 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003023
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003024 // Check to see if the target has a custom way to lower this. If so, use it.
3025 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3026 default: assert(0 && "This action not implemented for this operation!");
3027 case TargetLowering::Legal:
3028 case TargetLowering::Expand:
3029 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003030 case TargetLowering::Custom: {
3031 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3032 Source), DAG);
3033 if (NV.Val)
3034 return LegalizeOp(NV);
3035 break; // The target decided this was legal after all
3036 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003037 }
3038
Chris Lattner153587e2005-05-12 07:00:44 +00003039 // Expand the source, then glue it back together for the call. We must expand
3040 // the source in case it is shared (this pass of legalize must traverse it).
3041 SDOperand SrcLo, SrcHi;
3042 ExpandOp(Source, SrcLo, SrcHi);
3043 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3044
Chris Lattner06bbeb62005-05-11 19:02:11 +00003045 SDNode *OutChain = 0;
3046 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3047 DAG.getEntryNode());
3048 const char *FnName = 0;
3049 if (DestTy == MVT::f32)
3050 FnName = "__floatdisf";
3051 else {
3052 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3053 FnName = "__floatdidf";
3054 }
3055
Chris Lattneraac464e2005-01-21 06:05:23 +00003056 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3057
3058 TargetLowering::ArgListTy Args;
3059 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003060
Chris Lattneraac464e2005-01-21 06:05:23 +00003061 Args.push_back(std::make_pair(Source, ArgTy));
3062
3063 // We don't care about token chains for libcalls. We just use the entry
3064 // node as our input and ignore the output chain. This allows us to place
3065 // calls wherever we need them to satisfy data dependences.
3066 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003067
3068 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003069 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3070 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003071
Chris Lattnera5bf1032005-05-12 04:49:08 +00003072 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003073 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003074}
Misha Brukman835702a2005-04-21 22:36:52 +00003075
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003076
3077
Chris Lattnerdc750592005-01-07 07:47:09 +00003078/// ExpandOp - Expand the specified SDOperand into its two component pieces
3079/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3080/// LegalizeNodes map is filled in for any results that are not expanded, the
3081/// ExpandedNodes map is filled in for any results that are expanded, and the
3082/// Lo/Hi values are returned.
3083void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3084 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003085 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003086 SDNode *Node = Op.Val;
3087 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003088 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3089 "Cannot expand FP values!");
3090 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003091 "Cannot expand to FP value or to larger int value!");
3092
Chris Lattner1a570f12005-09-02 20:32:45 +00003093 // See if we already expanded it.
3094 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3095 = ExpandedNodes.find(Op);
3096 if (I != ExpandedNodes.end()) {
3097 Lo = I->second.first;
3098 Hi = I->second.second;
3099 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003100 }
3101
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003102 // Expanding to multiple registers needs to perform an optimization step, and
3103 // is not careful to avoid operations the target does not support. Make sure
3104 // that all generated operations are legalized in the next iteration.
3105 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003106
Chris Lattnerdc750592005-01-07 07:47:09 +00003107 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003108 case ISD::CopyFromReg:
3109 assert(0 && "CopyFromReg must be legal!");
3110 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003111 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3112 assert(0 && "Do not know how to expand this operator!");
3113 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003114 case ISD::UNDEF:
3115 Lo = DAG.getNode(ISD::UNDEF, NVT);
3116 Hi = DAG.getNode(ISD::UNDEF, NVT);
3117 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003118 case ISD::Constant: {
3119 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3120 Lo = DAG.getConstant(Cst, NVT);
3121 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3122 break;
3123 }
3124
Chris Lattner32e08b72005-03-28 22:03:13 +00003125 case ISD::BUILD_PAIR:
3126 // Legalize both operands. FIXME: in the future we should handle the case
3127 // where the two elements are not legal.
3128 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3129 Lo = LegalizeOp(Node->getOperand(0));
3130 Hi = LegalizeOp(Node->getOperand(1));
3131 break;
3132
Chris Lattner55e9cde2005-05-11 04:51:16 +00003133 case ISD::CTPOP:
3134 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003135 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3136 DAG.getNode(ISD::CTPOP, NVT, Lo),
3137 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003138 Hi = DAG.getConstant(0, NVT);
3139 break;
3140
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003141 case ISD::CTLZ: {
3142 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003143 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003144 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3145 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003146 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3147 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003148 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3149 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3150
3151 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3152 Hi = DAG.getConstant(0, NVT);
3153 break;
3154 }
3155
3156 case ISD::CTTZ: {
3157 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003158 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003159 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3160 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003161 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3162 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003163 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3164 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3165
3166 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3167 Hi = DAG.getConstant(0, NVT);
3168 break;
3169 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003170
Chris Lattnerdc750592005-01-07 07:47:09 +00003171 case ISD::LOAD: {
3172 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3173 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003174 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003175
3176 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003177 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003178 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3179 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003180 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003181 //are from the same instruction?
3182 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003183
3184 // Build a factor node to remember that this load is independent of the
3185 // other one.
3186 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3187 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003188
Chris Lattnerdc750592005-01-07 07:47:09 +00003189 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003190 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003191 if (!TLI.isLittleEndian())
3192 std::swap(Lo, Hi);
3193 break;
3194 }
Nate Begemand37c1312005-11-22 18:16:00 +00003195 case ISD::VLOAD: {
3196 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3197 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3198 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3199 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3200
3201 // If we only have two elements, turn into a pair of scalar loads.
3202 // FIXME: handle case where a vector of two elements is fine, such as
3203 // 2 x double on SSE2.
3204 if (NumElements == 2) {
3205 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3206 // Increment the pointer to the other half.
3207 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3208 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3209 getIntPtrConstant(IncrementSize));
3210 //Is this safe? declaring that the two parts of the split load
3211 //are from the same instruction?
3212 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3213 } else {
3214 NumElements /= 2; // Split the vector in half
3215 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3216 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3217 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3218 getIntPtrConstant(IncrementSize));
3219 //Is this safe? declaring that the two parts of the split load
3220 //are from the same instruction?
3221 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3222 }
3223
3224 // Build a factor node to remember that this load is independent of the
3225 // other one.
3226 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3227 Hi.getValue(1));
3228
3229 // Remember that we legalized the chain.
3230 AddLegalizedOperand(Op.getValue(1), TF);
3231 if (!TLI.isLittleEndian())
3232 std::swap(Lo, Hi);
3233 break;
3234 }
3235 case ISD::VADD:
3236 case ISD::VSUB:
3237 case ISD::VMUL: {
3238 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3239 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3240 SDOperand LL, LH, RL, RH;
3241
3242 ExpandOp(Node->getOperand(0), LL, LH);
3243 ExpandOp(Node->getOperand(1), RL, RH);
3244
3245 // If we only have two elements, turn into a pair of scalar loads.
3246 // FIXME: handle case where a vector of two elements is fine, such as
3247 // 2 x double on SSE2.
3248 if (NumElements == 2) {
3249 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3250 Lo = DAG.getNode(Opc, EVT, LL, RL);
3251 Hi = DAG.getNode(Opc, EVT, LH, RH);
3252 } else {
3253 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3254 LL.getOperand(3));
3255 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3256 LH.getOperand(3));
3257 }
3258 break;
3259 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003260 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003261 case ISD::CALL: {
3262 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3263 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3264
Chris Lattner3d95c142005-01-19 20:24:35 +00003265 bool Changed = false;
3266 std::vector<SDOperand> Ops;
3267 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3268 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3269 Changed |= Ops.back() != Node->getOperand(i);
3270 }
3271
Chris Lattnerdc750592005-01-07 07:47:09 +00003272 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3273 "Can only expand a call once so far, not i64 -> i16!");
3274
3275 std::vector<MVT::ValueType> RetTyVTs;
3276 RetTyVTs.reserve(3);
3277 RetTyVTs.push_back(NVT);
3278 RetTyVTs.push_back(NVT);
3279 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003280 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3281 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003282 Lo = SDOperand(NC, 0);
3283 Hi = SDOperand(NC, 1);
3284
3285 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003286 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003287 break;
3288 }
3289 case ISD::AND:
3290 case ISD::OR:
3291 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3292 SDOperand LL, LH, RL, RH;
3293 ExpandOp(Node->getOperand(0), LL, LH);
3294 ExpandOp(Node->getOperand(1), RL, RH);
3295 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3296 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3297 break;
3298 }
3299 case ISD::SELECT: {
3300 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003301
3302 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3303 case Expand: assert(0 && "It's impossible to expand bools");
3304 case Legal:
3305 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3306 break;
3307 case Promote:
3308 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3309 break;
3310 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003311 ExpandOp(Node->getOperand(1), LL, LH);
3312 ExpandOp(Node->getOperand(2), RL, RH);
3313 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3314 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3315 break;
3316 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003317 case ISD::SELECT_CC: {
3318 SDOperand TL, TH, FL, FH;
3319 ExpandOp(Node->getOperand(2), TL, TH);
3320 ExpandOp(Node->getOperand(3), FL, FH);
3321 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3322 Node->getOperand(1), TL, FL, Node->getOperand(4));
3323 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3324 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003325 Lo = LegalizeOp(Lo);
3326 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003327 break;
3328 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003329 case ISD::SEXTLOAD: {
3330 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3331 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3332 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3333
3334 if (EVT == NVT)
3335 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3336 else
3337 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3338 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003339
3340 // Remember that we legalized the chain.
3341 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3342
Nate Begemanc3a89c52005-10-13 17:15:37 +00003343 // The high part is obtained by SRA'ing all but one of the bits of the lo
3344 // part.
3345 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3346 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3347 TLI.getShiftAmountTy()));
3348 Lo = LegalizeOp(Lo);
3349 Hi = LegalizeOp(Hi);
3350 break;
3351 }
3352 case ISD::ZEXTLOAD: {
3353 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3354 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3355 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3356
3357 if (EVT == NVT)
3358 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3359 else
3360 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3361 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003362
3363 // Remember that we legalized the chain.
3364 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3365
Nate Begemanc3a89c52005-10-13 17:15:37 +00003366 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003367 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003368 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003369 break;
3370 }
3371 case ISD::EXTLOAD: {
3372 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3373 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3374 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3375
3376 if (EVT == NVT)
3377 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3378 else
3379 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3380 EVT);
3381
3382 // Remember that we legalized the chain.
3383 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3384
3385 // The high part is undefined.
3386 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3387 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003388 break;
3389 }
Chris Lattner7753f172005-09-02 00:18:10 +00003390 case ISD::ANY_EXTEND: {
3391 SDOperand In;
3392 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3393 case Expand: assert(0 && "expand-expand not implemented yet!");
3394 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3395 case Promote:
3396 In = PromoteOp(Node->getOperand(0));
3397 break;
3398 }
3399
3400 // The low part is any extension of the input (which degenerates to a copy).
3401 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3402 // The high part is undefined.
3403 Hi = DAG.getNode(ISD::UNDEF, NVT);
3404 break;
3405 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003406 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003407 SDOperand In;
3408 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3409 case Expand: assert(0 && "expand-expand not implemented yet!");
3410 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3411 case Promote:
3412 In = PromoteOp(Node->getOperand(0));
3413 // Emit the appropriate sign_extend_inreg to get the value we want.
3414 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003415 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003416 break;
3417 }
3418
Chris Lattnerdc750592005-01-07 07:47:09 +00003419 // The low part is just a sign extension of the input (which degenerates to
3420 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003421 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003422
Chris Lattnerdc750592005-01-07 07:47:09 +00003423 // The high part is obtained by SRA'ing all but one of the bits of the lo
3424 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003425 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003426 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3427 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003428 break;
3429 }
Chris Lattner47844892005-04-03 23:41:52 +00003430 case ISD::ZERO_EXTEND: {
3431 SDOperand In;
3432 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3433 case Expand: assert(0 && "expand-expand not implemented yet!");
3434 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3435 case Promote:
3436 In = PromoteOp(Node->getOperand(0));
3437 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003438 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003439 break;
3440 }
3441
Chris Lattnerdc750592005-01-07 07:47:09 +00003442 // The low part is just a zero extension of the input (which degenerates to
3443 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003444 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003445
Chris Lattnerdc750592005-01-07 07:47:09 +00003446 // The high part is just a zero.
3447 Hi = DAG.getConstant(0, NVT);
3448 break;
Chris Lattner47844892005-04-03 23:41:52 +00003449 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003450
Chris Lattner44c28c22005-11-20 22:56:56 +00003451 case ISD::READCYCLECOUNTER: {
3452 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3453 TargetLowering::Custom &&
3454 "Must custom expand ReadCycleCounter");
3455 SDOperand T = TLI.LowerOperation(Op, DAG);
3456 assert(T.Val && "Node must be custom expanded!");
3457 Lo = LegalizeOp(T.getValue(0));
3458 Hi = LegalizeOp(T.getValue(1));
3459 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3460 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003461 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003462 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003463
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003464 // These operators cannot be expanded directly, emit them as calls to
3465 // library functions.
3466 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003467 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003468 SDOperand Op;
3469 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3470 case Expand: assert(0 && "cannot expand FP!");
3471 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3472 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3473 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003474
Chris Lattner941d84a2005-07-30 01:40:57 +00003475 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3476
Chris Lattnerfe68d752005-07-29 00:33:32 +00003477 // Now that the custom expander is done, expand the result, which is still
3478 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003479 if (Op.Val) {
3480 ExpandOp(Op, Lo, Hi);
3481 break;
3482 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003483 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003484
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003485 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003486 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003487 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003488 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003489 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003490
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003491 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003492 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3493 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3494 LegalizeOp(Node->getOperand(0)));
3495 // Now that the custom expander is done, expand the result, which is still
3496 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003497 Op = TLI.LowerOperation(Op, DAG);
3498 if (Op.Val) {
3499 ExpandOp(Op, Lo, Hi);
3500 break;
3501 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003502 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003503
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003504 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003505 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003506 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003507 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003508 break;
3509
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003510 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003511 // If the target wants custom lowering, do so.
3512 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3513 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3514 LegalizeOp(Node->getOperand(1)));
3515 Op = TLI.LowerOperation(Op, DAG);
3516 if (Op.Val) {
3517 // Now that the custom expander is done, expand the result, which is
3518 // still VT.
3519 ExpandOp(Op, Lo, Hi);
3520 break;
3521 }
3522 }
3523
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003524 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003525 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003526 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003527
3528 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003529 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003530 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3531 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003532 break;
3533 }
3534
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003535 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003536 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003537 break;
3538
3539 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003540 // If the target wants custom lowering, do so.
3541 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3542 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3543 LegalizeOp(Node->getOperand(1)));
3544 Op = TLI.LowerOperation(Op, DAG);
3545 if (Op.Val) {
3546 // Now that the custom expander is done, expand the result, which is
3547 // still VT.
3548 ExpandOp(Op, Lo, Hi);
3549 break;
3550 }
3551 }
3552
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003553 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003554 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003555 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003556
3557 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003558 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003559 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3560 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003561 break;
3562 }
3563
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003564 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003565 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003566 break;
3567 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003568 // If the target wants custom lowering, do so.
3569 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3570 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3571 LegalizeOp(Node->getOperand(1)));
3572 Op = TLI.LowerOperation(Op, DAG);
3573 if (Op.Val) {
3574 // Now that the custom expander is done, expand the result, which is
3575 // still VT.
3576 ExpandOp(Op, Lo, Hi);
3577 break;
3578 }
3579 }
3580
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003581 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003582 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003583 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003584
3585 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003586 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003587 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3588 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003589 break;
3590 }
3591
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003592 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003593 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003594 break;
3595
Misha Brukman835702a2005-04-21 22:36:52 +00003596 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003597 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3598 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003599 break;
3600 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003601 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3602 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003603 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003604 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003605 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003606 SDOperand LL, LH, RL, RH;
3607 ExpandOp(Node->getOperand(0), LL, LH);
3608 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003609 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3610 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3611 // extended the sign bit of the low half through the upper half, and if so
3612 // emit a MULHS instead of the alternate sequence that is valid for any
3613 // i64 x i64 multiply.
3614 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3615 // is RH an extension of the sign bit of RL?
3616 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3617 RH.getOperand(1).getOpcode() == ISD::Constant &&
3618 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3619 // is LH an extension of the sign bit of LL?
3620 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3621 LH.getOperand(1).getOpcode() == ISD::Constant &&
3622 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3623 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3624 } else {
3625 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3626 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3627 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3628 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3629 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3630 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003631 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3632 } else {
3633 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3634 }
3635 break;
3636 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003637 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3638 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3639 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3640 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003641 }
3642
3643 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003644 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3645 std::make_pair(Lo, Hi))).second;
3646 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00003647}
3648
3649
3650// SelectionDAG::Legalize - This is the entry point for the file.
3651//
Chris Lattner4add7e32005-01-23 04:42:50 +00003652void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003653 /// run - This is the main entry point to this class.
3654 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003655 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003656}
3657