blob: 6ff7bccd61aacd24b57d45cb22bbbbd361e1f312 [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)".
55 unsigned ValueTypeActions;
56
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
152
Chris Lattner4add7e32005-01-23 04:42:50 +0000153SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
154 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
155 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000156 assert(MVT::LAST_VALUETYPE <= 16 &&
157 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000158}
159
Jim Laskeyf2516a92005-08-17 00:39:29 +0000160/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
161/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000162/// we expand it. At this point, we know that the result and operand types are
163/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000164SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
165 SDOperand Op0,
166 MVT::ValueType DestVT) {
167 if (Op0.getValueType() == MVT::i32) {
168 // simple 32-bit [signed|unsigned] integer to float/double expansion
169
170 // get the stack frame index of a 8 byte buffer
171 MachineFunction &MF = DAG.getMachineFunction();
172 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
173 // get address of 8 byte buffer
174 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
175 // word offset constant for Hi/Lo address computation
176 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
177 // set up Hi and Lo (into buffer) address based on endian
178 SDOperand Hi, Lo;
179 if (TLI.isLittleEndian()) {
180 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
181 Lo = StackSlot;
182 } else {
183 Hi = StackSlot;
184 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
185 }
186 // if signed map to unsigned space
187 SDOperand Op0Mapped;
188 if (isSigned) {
189 // constant used to invert sign bit (signed to unsigned mapping)
190 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
191 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
192 } else {
193 Op0Mapped = Op0;
194 }
195 // store the lo of the constructed double - based on integer input
196 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
197 Op0Mapped, Lo, DAG.getSrcValue(NULL));
198 // initial hi portion of constructed double
199 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
200 // store the hi of the constructed double - biased exponent
201 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
202 InitialHi, Hi, DAG.getSrcValue(NULL));
203 // load the constructed double
204 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
205 DAG.getSrcValue(NULL));
206 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000207 SDOperand Bias = DAG.getConstantFP(isSigned ?
208 BitsToDouble(0x4330000080000000ULL)
209 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000210 MVT::f64);
211 // subtract the bias
212 SDOperand Sub = DAG.getNode(ISD::SUB, MVT::f64, Load, Bias);
213 // final result
214 SDOperand Result;
215 // handle final rounding
216 if (DestVT == MVT::f64) {
217 // do nothing
218 Result = Sub;
219 } else {
220 // if f32 then cast to f32
221 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
222 }
223 NeedsAnotherIteration = true;
224 return Result;
225 }
226 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000227 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000228
Chris Lattnerd47675e2005-08-09 20:20:18 +0000229 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
230 DAG.getConstant(0, Op0.getValueType()),
231 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000232 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
233 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
234 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000235
Jim Laskeyf2516a92005-08-17 00:39:29 +0000236 // If the sign bit of the integer is set, the large number will be treated
237 // as a negative number. To counteract this, the dynamic code adds an
238 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000239 uint64_t FF;
240 switch (Op0.getValueType()) {
241 default: assert(0 && "Unsupported integer type!");
242 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
243 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
244 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
245 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
246 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000247 if (TLI.isLittleEndian()) FF <<= 32;
248 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000249
Chris Lattnerc30405e2005-08-26 17:15:30 +0000250 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-07-16 00:19:57 +0000251 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
252 SDOperand FudgeInReg;
253 if (DestVT == MVT::f32)
254 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
255 DAG.getSrcValue(NULL));
256 else {
257 assert(DestVT == MVT::f64 && "Unexpected conversion");
258 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
259 DAG.getEntryNode(), CPIdx,
260 DAG.getSrcValue(NULL), MVT::f32));
261 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000262
Chris Lattnere3e847b2005-07-16 00:19:57 +0000263 NeedsAnotherIteration = true;
264 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
265}
266
Chris Lattner19732782005-08-16 18:17:10 +0000267/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000268/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000269/// we promote it. At this point, we know that the result and operand types are
270/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
271/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000272SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
273 MVT::ValueType DestVT,
274 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000275 // First step, figure out the appropriate *INT_TO_FP operation to use.
276 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000277
Chris Lattnere3e847b2005-07-16 00:19:57 +0000278 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000279
Chris Lattnere3e847b2005-07-16 00:19:57 +0000280 // Scan for the appropriate larger type to use.
281 while (1) {
282 NewInTy = (MVT::ValueType)(NewInTy+1);
283 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000284
Chris Lattnere3e847b2005-07-16 00:19:57 +0000285 // If the target supports SINT_TO_FP of this type, use it.
286 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
287 default: break;
288 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000289 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000290 break; // Can't use this datatype.
291 // FALL THROUGH.
292 case TargetLowering::Custom:
293 OpToUse = ISD::SINT_TO_FP;
294 break;
295 }
296 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000297 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000298
Chris Lattnere3e847b2005-07-16 00:19:57 +0000299 // If the target supports UINT_TO_FP of this type, use it.
300 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
301 default: break;
302 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000303 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000304 break; // Can't use this datatype.
305 // FALL THROUGH.
306 case TargetLowering::Custom:
307 OpToUse = ISD::UINT_TO_FP;
308 break;
309 }
310 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000311
Chris Lattnere3e847b2005-07-16 00:19:57 +0000312 // Otherwise, try a larger type.
313 }
314
315 // Make sure to legalize any nodes we create here in the next pass.
316 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000317
Chris Lattnere3e847b2005-07-16 00:19:57 +0000318 // Okay, we found the operation and type to use. Zero extend our input to the
319 // desired type then run the operation on it.
320 return DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000321 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
322 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000323}
324
Chris Lattner44fe26f2005-07-29 00:11:56 +0000325/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
326/// FP_TO_*INT operation of the specified operand when the target requests that
327/// we promote it. At this point, we know that the result and operand types are
328/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
329/// operation that returns a larger result.
330SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
331 MVT::ValueType DestVT,
332 bool isSigned) {
333 // First step, figure out the appropriate FP_TO*INT operation to use.
334 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000335
Chris Lattner44fe26f2005-07-29 00:11:56 +0000336 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000337
Chris Lattner44fe26f2005-07-29 00:11:56 +0000338 // Scan for the appropriate larger type to use.
339 while (1) {
340 NewOutTy = (MVT::ValueType)(NewOutTy+1);
341 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000342
Chris Lattner44fe26f2005-07-29 00:11:56 +0000343 // If the target supports FP_TO_SINT returning this type, use it.
344 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
345 default: break;
346 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000347 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000348 break; // Can't use this datatype.
349 // FALL THROUGH.
350 case TargetLowering::Custom:
351 OpToUse = ISD::FP_TO_SINT;
352 break;
353 }
354 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000355
Chris Lattner44fe26f2005-07-29 00:11:56 +0000356 // If the target supports FP_TO_UINT of this type, use it.
357 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
358 default: break;
359 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000360 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000361 break; // Can't use this datatype.
362 // FALL THROUGH.
363 case TargetLowering::Custom:
364 OpToUse = ISD::FP_TO_UINT;
365 break;
366 }
367 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000368
Chris Lattner44fe26f2005-07-29 00:11:56 +0000369 // Otherwise, try a larger type.
370 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000371
Chris Lattner44fe26f2005-07-29 00:11:56 +0000372 // Make sure to legalize any nodes we create here in the next pass.
373 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000374
Chris Lattner44fe26f2005-07-29 00:11:56 +0000375 // Okay, we found the operation and type to use. Truncate the result of the
376 // extended FP_TO_*INT operation to the desired size.
377 return DAG.getNode(ISD::TRUNCATE, DestVT,
378 DAG.getNode(OpToUse, NewOutTy, LegalOp));
379}
380
381
Chris Lattnerdc750592005-01-07 07:47:09 +0000382void SelectionDAGLegalize::LegalizeDAG() {
383 SDOperand OldRoot = DAG.getRoot();
384 SDOperand NewRoot = LegalizeOp(OldRoot);
385 DAG.setRoot(NewRoot);
386
387 ExpandedNodes.clear();
388 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000389 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000390
391 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000392 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000393}
394
395SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000396 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000397 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000398 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000399
Chris Lattnerdc750592005-01-07 07:47:09 +0000400 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000401 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000402 if (Node->getNumValues() > 1) {
403 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
404 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000405 case Legal: break; // Nothing to do.
406 case Expand: {
407 SDOperand T1, T2;
408 ExpandOp(Op.getValue(i), T1, T2);
409 assert(LegalizedNodes.count(Op) &&
410 "Expansion didn't add legal operands!");
411 return LegalizedNodes[Op];
412 }
413 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000414 PromoteOp(Op.getValue(i));
415 assert(LegalizedNodes.count(Op) &&
416 "Expansion didn't add legal operands!");
417 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000418 }
419 }
420
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000421 // Note that LegalizeOp may be reentered even from single-use nodes, which
422 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000423 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
424 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000425
Nate Begemane5b86d72005-08-10 20:51:12 +0000426 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000427
428 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000429
430 switch (Node->getOpcode()) {
431 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000432 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
433 // If this is a target node, legalize it by legalizing the operands then
434 // passing it through.
435 std::vector<SDOperand> Ops;
436 bool Changed = false;
437 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
438 Ops.push_back(LegalizeOp(Node->getOperand(i)));
439 Changed = Changed || Node->getOperand(i) != Ops.back();
440 }
441 if (Changed)
442 if (Node->getNumValues() == 1)
443 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
444 else {
445 std::vector<MVT::ValueType> VTs(Node->value_begin(),
446 Node->value_end());
447 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
448 }
449
450 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
451 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
452 return Result.getValue(Op.ResNo);
453 }
454 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000455 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
456 assert(0 && "Do not know how to legalize this operator!");
457 abort();
458 case ISD::EntryToken:
459 case ISD::FrameIndex:
460 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000461 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000462 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000463 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000464 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000465 case ISD::CopyFromReg:
466 Tmp1 = LegalizeOp(Node->getOperand(0));
467 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000468 Result = DAG.getCopyFromReg(Tmp1,
469 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
470 Node->getValueType(0));
Chris Lattnereb6614d2005-01-28 06:27:38 +0000471 else
472 Result = Op.getValue(0);
473
474 // Since CopyFromReg produces two values, make sure to remember that we
475 // legalized both of them.
476 AddLegalizedOperand(Op.getValue(0), Result);
477 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
478 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000479 case ISD::ImplicitDef:
480 Tmp1 = LegalizeOp(Node->getOperand(0));
481 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000482 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
483 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000484 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000485 case ISD::UNDEF: {
486 MVT::ValueType VT = Op.getValueType();
487 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000488 default: assert(0 && "This action is not supported yet!");
489 case TargetLowering::Expand:
490 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000491 if (MVT::isInteger(VT))
492 Result = DAG.getConstant(0, VT);
493 else if (MVT::isFloatingPoint(VT))
494 Result = DAG.getConstantFP(0, VT);
495 else
496 assert(0 && "Unknown value type!");
497 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000498 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000499 break;
500 }
501 break;
502 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000503 case ISD::Constant:
504 // We know we don't need to expand constants here, constants only have one
505 // value and we check that it is fine above.
506
507 // FIXME: Maybe we should handle things like targets that don't support full
508 // 32-bit immediates?
509 break;
510 case ISD::ConstantFP: {
511 // Spill FP immediates to the constant pool if the target cannot directly
512 // codegen them. Targets often have some immediate values that can be
513 // efficiently generated into an FP register without a load. We explicitly
514 // leave these constants as ConstantFP nodes for the target to deal with.
515
516 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
517
518 // Check to see if this FP immediate is already legal.
519 bool isLegal = false;
520 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
521 E = TLI.legal_fpimm_end(); I != E; ++I)
522 if (CFP->isExactlyValue(*I)) {
523 isLegal = true;
524 break;
525 }
526
527 if (!isLegal) {
528 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000529 bool Extend = false;
530
531 // If a FP immediate is precise when represented as a float, we put it
532 // into the constant pool as a float, even if it's is statically typed
533 // as a double.
534 MVT::ValueType VT = CFP->getValueType(0);
535 bool isDouble = VT == MVT::f64;
536 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
537 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000538 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
539 // Only do this if the target has a native EXTLOAD instruction from
540 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000541 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000542 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
543 VT = MVT::f32;
544 Extend = true;
545 }
Misha Brukman835702a2005-04-21 22:36:52 +0000546
Chris Lattnerc30405e2005-08-26 17:15:30 +0000547 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000548 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000549 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
550 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000551 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000552 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
553 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000554 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000555 }
556 break;
557 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000558 case ISD::TokenFactor: {
559 std::vector<SDOperand> Ops;
560 bool Changed = false;
561 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000562 SDOperand Op = Node->getOperand(i);
563 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnerf09c0b42005-05-12 06:04:14 +0000564 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner55562fa2005-01-19 19:10:54 +0000565 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
566 Changed = true;
567 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
568 Ops.push_back(LegalizeOp(Op.getOperand(j)));
569 } else {
570 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
571 Changed |= Ops[i] != Op;
572 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000573 }
574 if (Changed)
575 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
576 break;
577 }
578
Chris Lattner2dce7032005-05-12 23:24:06 +0000579 case ISD::CALLSEQ_START:
580 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000581 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000582 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000583 Tmp2 = Node->getOperand(0);
584 if (Tmp1 != Tmp2) {
Chris Lattner8005e912005-05-12 00:17:04 +0000585 Node->setAdjCallChain(Tmp1);
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000586
587 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
588 // this will cause the maps used to memoize results to get confused.
589 // Create and add a dummy use, just to increase its use count. This will
590 // be removed at the end of legalize when dead nodes are removed.
591 if (Tmp2.Val->hasOneUse())
592 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
593 DAG.getConstant(0, MVT::i32));
594 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000595 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000596 // nodes are treated specially and are mutated in place. This makes the dag
597 // legalization process more efficient and also makes libcall insertion
598 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000599 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000600 case ISD::DYNAMIC_STACKALLOC:
601 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
602 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
603 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
604 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000605 Tmp3 != Node->getOperand(2)) {
606 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
607 std::vector<SDOperand> Ops;
608 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
609 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
610 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000611 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000612
613 // Since this op produces two values, make sure to remember that we
614 // legalized both of them.
615 AddLegalizedOperand(SDOperand(Node, 0), Result);
616 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
617 return Result.getValue(Op.ResNo);
618
Chris Lattnerd0feb642005-05-13 18:43:43 +0000619 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000620 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000621 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
622 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000623
624 bool Changed = false;
625 std::vector<SDOperand> Ops;
626 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
627 Ops.push_back(LegalizeOp(Node->getOperand(i)));
628 Changed |= Ops.back() != Node->getOperand(i);
629 }
630
631 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000632 std::vector<MVT::ValueType> RetTyVTs;
633 RetTyVTs.reserve(Node->getNumValues());
634 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000635 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000636 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
637 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000638 } else {
639 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000640 }
Chris Lattner9242c502005-01-09 19:43:23 +0000641 // Since calls produce multiple values, make sure to remember that we
642 // legalized all of them.
643 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
644 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
645 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000646 }
Chris Lattner68a12142005-01-07 22:12:08 +0000647 case ISD::BR:
648 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
649 if (Tmp1 != Node->getOperand(0))
650 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
651 break;
652
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000653 case ISD::BRCOND:
654 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000655
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000656 switch (getTypeAction(Node->getOperand(1).getValueType())) {
657 case Expand: assert(0 && "It's impossible to expand bools");
658 case Legal:
659 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
660 break;
661 case Promote:
662 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
663 break;
664 }
Nate Begeman371e4952005-08-16 19:49:35 +0000665
666 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
667 default: assert(0 && "This action is not supported yet!");
668 case TargetLowering::Expand:
669 // Expand brcond's setcc into its constituent parts and create a BR_CC
670 // Node.
671 if (Tmp2.getOpcode() == ISD::SETCC) {
672 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
673 Tmp2.getOperand(0), Tmp2.getOperand(1),
674 Node->getOperand(2));
675 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000676 // Make sure the condition is either zero or one. It may have been
677 // promoted from something else.
678 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
679
Nate Begeman371e4952005-08-16 19:49:35 +0000680 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
681 DAG.getCondCode(ISD::SETNE), Tmp2,
682 DAG.getConstant(0, Tmp2.getValueType()),
683 Node->getOperand(2));
684 }
685 break;
686 case TargetLowering::Legal:
687 // Basic block destination (Op#2) is always legal.
688 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
689 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
690 Node->getOperand(2));
691 break;
692 }
693 break;
694 case ISD::BR_CC:
695 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
696
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000697 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000698 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
699 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
700 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
701 Tmp3 != Node->getOperand(3)) {
702 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
703 Tmp2, Tmp3, Node->getOperand(4));
704 }
705 break;
706 } else {
707 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
708 Node->getOperand(2), // LHS
709 Node->getOperand(3), // RHS
710 Node->getOperand(1)));
711 // If we get a SETCC back from legalizing the SETCC node we just
712 // created, then use its LHS, RHS, and CC directly in creating a new
713 // node. Otherwise, select between the true and false value based on
714 // comparing the result of the legalized with zero.
715 if (Tmp2.getOpcode() == ISD::SETCC) {
716 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
717 Tmp2.getOperand(0), Tmp2.getOperand(1),
718 Node->getOperand(4));
719 } else {
720 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
721 DAG.getCondCode(ISD::SETNE),
722 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
723 Node->getOperand(4));
724 }
725 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000726 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000727 case ISD::BRCONDTWOWAY:
728 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
729 switch (getTypeAction(Node->getOperand(1).getValueType())) {
730 case Expand: assert(0 && "It's impossible to expand bools");
731 case Legal:
732 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
733 break;
734 case Promote:
735 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
736 break;
737 }
738 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
739 // pair.
740 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
741 case TargetLowering::Promote:
742 default: assert(0 && "This action is not supported yet!");
743 case TargetLowering::Legal:
744 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
745 std::vector<SDOperand> Ops;
746 Ops.push_back(Tmp1);
747 Ops.push_back(Tmp2);
748 Ops.push_back(Node->getOperand(2));
749 Ops.push_back(Node->getOperand(3));
750 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
751 }
752 break;
753 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000754 // If BRTWOWAY_CC is legal for this target, then simply expand this node
755 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
756 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000757 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000758 if (Tmp2.getOpcode() == ISD::SETCC) {
759 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
760 Tmp2.getOperand(0), Tmp2.getOperand(1),
761 Node->getOperand(2), Node->getOperand(3));
762 } else {
763 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
764 DAG.getConstant(0, Tmp2.getValueType()),
765 Node->getOperand(2), Node->getOperand(3));
766 }
767 } else {
768 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000769 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000770 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
771 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000772 break;
773 }
774 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000775 case ISD::BRTWOWAY_CC:
776 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000777 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000778 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
779 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
780 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
781 Tmp3 != Node->getOperand(3)) {
782 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
783 Node->getOperand(4), Node->getOperand(5));
784 }
785 break;
786 } else {
787 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
788 Node->getOperand(2), // LHS
789 Node->getOperand(3), // RHS
790 Node->getOperand(1)));
791 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
792 // pair.
793 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
794 default: assert(0 && "This action is not supported yet!");
795 case TargetLowering::Legal:
796 // If we get a SETCC back from legalizing the SETCC node we just
797 // created, then use its LHS, RHS, and CC directly in creating a new
798 // node. Otherwise, select between the true and false value based on
799 // comparing the result of the legalized with zero.
800 if (Tmp2.getOpcode() == ISD::SETCC) {
801 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
802 Tmp2.getOperand(0), Tmp2.getOperand(1),
803 Node->getOperand(4), Node->getOperand(5));
804 } else {
805 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
806 DAG.getConstant(0, Tmp2.getValueType()),
807 Node->getOperand(4), Node->getOperand(5));
808 }
809 break;
810 case TargetLowering::Expand:
811 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
812 Node->getOperand(4));
813 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
814 break;
815 }
816 }
817 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000818 case ISD::LOAD:
819 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
820 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000821
Chris Lattnerdc750592005-01-07 07:47:09 +0000822 if (Tmp1 != Node->getOperand(0) ||
823 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000824 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
825 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000826 else
827 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000828
Chris Lattnerea4ca942005-01-07 22:28:47 +0000829 // Since loads produce two values, make sure to remember that we legalized
830 // both of them.
831 AddLegalizedOperand(SDOperand(Node, 0), Result);
832 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
833 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000834
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000835 case ISD::EXTLOAD:
836 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000837 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000838 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
839 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000840
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000841 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000842 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000843 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000844 case TargetLowering::Promote:
845 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000846 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
847 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000848 // Since loads produce two values, make sure to remember that we legalized
849 // both of them.
850 AddLegalizedOperand(SDOperand(Node, 0), Result);
851 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
852 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000853
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000854 case TargetLowering::Legal:
855 if (Tmp1 != Node->getOperand(0) ||
856 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000857 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
858 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000859 else
860 Result = SDOperand(Node, 0);
861
862 // Since loads produce two values, make sure to remember that we legalized
863 // both of them.
864 AddLegalizedOperand(SDOperand(Node, 0), Result);
865 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
866 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000867 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000868 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
869 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
870 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000871 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000872 if (Op.ResNo)
873 return Load.getValue(1);
874 return Result;
875 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000876 assert(Node->getOpcode() != ISD::EXTLOAD &&
877 "EXTLOAD should always be supported!");
878 // Turn the unsupported load into an EXTLOAD followed by an explicit
879 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000880 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
881 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000882 SDOperand ValRes;
883 if (Node->getOpcode() == ISD::SEXTLOAD)
884 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000885 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000886 else
887 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000888 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
889 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
890 if (Op.ResNo)
891 return Result.getValue(1);
892 return ValRes;
893 }
894 assert(0 && "Unreachable");
895 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000896 case ISD::EXTRACT_ELEMENT:
897 // Get both the low and high parts.
898 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
899 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
900 Result = Tmp2; // 1 -> Hi
901 else
902 Result = Tmp1; // 0 -> Lo
903 break;
904
905 case ISD::CopyToReg:
906 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000907
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000908 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +0000909 "Register type must be legal!");
910 // Legalize the incoming value (must be legal).
911 Tmp2 = LegalizeOp(Node->getOperand(2));
912 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
913 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
914 Node->getOperand(1), Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000915 break;
916
917 case ISD::RET:
918 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
919 switch (Node->getNumOperands()) {
920 case 2: // ret val
921 switch (getTypeAction(Node->getOperand(1).getValueType())) {
922 case Legal:
923 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000924 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000925 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
926 break;
927 case Expand: {
928 SDOperand Lo, Hi;
929 ExpandOp(Node->getOperand(1), Lo, Hi);
930 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000931 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000932 }
933 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000934 Tmp2 = PromoteOp(Node->getOperand(1));
935 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
936 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000937 }
938 break;
939 case 1: // ret void
940 if (Tmp1 != Node->getOperand(0))
941 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
942 break;
943 default: { // ret <values>
944 std::vector<SDOperand> NewValues;
945 NewValues.push_back(Tmp1);
946 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
947 switch (getTypeAction(Node->getOperand(i).getValueType())) {
948 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000949 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000950 break;
951 case Expand: {
952 SDOperand Lo, Hi;
953 ExpandOp(Node->getOperand(i), Lo, Hi);
954 NewValues.push_back(Lo);
955 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000956 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000957 }
958 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000959 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000960 }
961 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
962 break;
963 }
964 }
965 break;
966 case ISD::STORE:
967 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
968 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
969
Chris Lattnere69daaf2005-01-08 06:25:56 +0000970 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000971 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000972 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000973 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +0000974 DAG.getConstant(FloatToBits(CFP->getValue()),
975 MVT::i32),
976 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000977 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000978 } else {
979 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000980 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +0000981 DAG.getConstant(DoubleToBits(CFP->getValue()),
982 MVT::i64),
983 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000984 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000985 }
Chris Lattnera4743132005-02-22 07:23:39 +0000986 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000987 }
988
Chris Lattnerdc750592005-01-07 07:47:09 +0000989 switch (getTypeAction(Node->getOperand(1).getValueType())) {
990 case Legal: {
991 SDOperand Val = LegalizeOp(Node->getOperand(1));
992 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
993 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +0000994 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
995 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +0000996 break;
997 }
998 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000999 // Truncate the value and store the result.
1000 Tmp3 = PromoteOp(Node->getOperand(1));
1001 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001002 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001003 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001004 break;
1005
Chris Lattnerdc750592005-01-07 07:47:09 +00001006 case Expand:
1007 SDOperand Lo, Hi;
1008 ExpandOp(Node->getOperand(1), Lo, Hi);
1009
1010 if (!TLI.isLittleEndian())
1011 std::swap(Lo, Hi);
1012
Chris Lattner55e9cde2005-05-11 04:51:16 +00001013 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1014 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001015 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001016 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1017 getIntPtrConstant(IncrementSize));
1018 assert(isTypeLegal(Tmp2.getValueType()) &&
1019 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001020 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001021 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1022 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001023 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1024 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001025 }
1026 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001027 case ISD::PCMARKER:
1028 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001029 if (Tmp1 != Node->getOperand(0))
1030 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001031 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001032 case ISD::TRUNCSTORE:
1033 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1034 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1035
1036 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1037 case Legal:
1038 Tmp2 = LegalizeOp(Node->getOperand(1));
1039 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1040 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +00001041 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001042 Node->getOperand(3), Node->getOperand(4));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001043 break;
1044 case Promote:
1045 case Expand:
1046 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1047 }
1048 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001049 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001050 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1051 case Expand: assert(0 && "It's impossible to expand bools");
1052 case Legal:
1053 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1054 break;
1055 case Promote:
1056 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1057 break;
1058 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001059 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001060 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001061
Nate Begeman987121a2005-08-23 04:29:48 +00001062 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001063 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001064 case TargetLowering::Expand:
1065 if (Tmp1.getOpcode() == ISD::SETCC) {
1066 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1067 Tmp2, Tmp3,
1068 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1069 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001070 // Make sure the condition is either zero or one. It may have been
1071 // promoted from something else.
1072 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001073 Result = DAG.getSelectCC(Tmp1,
1074 DAG.getConstant(0, Tmp1.getValueType()),
1075 Tmp2, Tmp3, ISD::SETNE);
1076 }
1077 break;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001078 case TargetLowering::Legal:
1079 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1080 Tmp3 != Node->getOperand(2))
1081 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1082 Tmp1, Tmp2, Tmp3);
1083 break;
1084 case TargetLowering::Promote: {
1085 MVT::ValueType NVT =
1086 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1087 unsigned ExtOp, TruncOp;
1088 if (MVT::isInteger(Tmp2.getValueType())) {
1089 ExtOp = ISD::ZERO_EXTEND;
1090 TruncOp = ISD::TRUNCATE;
1091 } else {
1092 ExtOp = ISD::FP_EXTEND;
1093 TruncOp = ISD::FP_ROUND;
1094 }
1095 // Promote each of the values to the new type.
1096 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1097 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1098 // Perform the larger operation, then round down.
1099 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1100 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1101 break;
1102 }
1103 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001104 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001105 case ISD::SELECT_CC:
1106 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1107 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1108
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001109 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001110 // Everything is legal, see if we should expand this op or something.
1111 switch (TLI.getOperationAction(ISD::SELECT_CC,
1112 Node->getOperand(0).getValueType())) {
1113 default: assert(0 && "This action is not supported yet!");
1114 case TargetLowering::Custom: {
1115 SDOperand Tmp =
1116 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1117 Node->getOperand(0),
1118 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001119 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001120 if (Tmp.Val) {
1121 Result = LegalizeOp(Tmp);
1122 break;
1123 }
1124 } // FALLTHROUGH if the target can't lower this operation after all.
1125 case TargetLowering::Legal:
1126 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1127 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1128 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1129 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1130 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1131 Tmp3, Tmp4, Node->getOperand(4));
1132 }
1133 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001134 }
1135 break;
1136 } else {
1137 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1138 Node->getOperand(0), // LHS
1139 Node->getOperand(1), // RHS
1140 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001141 // If we get a SETCC back from legalizing the SETCC node we just
1142 // created, then use its LHS, RHS, and CC directly in creating a new
1143 // node. Otherwise, select between the true and false value based on
1144 // comparing the result of the legalized with zero.
1145 if (Tmp1.getOpcode() == ISD::SETCC) {
1146 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1147 Tmp1.getOperand(0), Tmp1.getOperand(1),
1148 Tmp3, Tmp4, Tmp1.getOperand(2));
1149 } else {
1150 Result = DAG.getSelectCC(Tmp1,
1151 DAG.getConstant(0, Tmp1.getValueType()),
1152 Tmp3, Tmp4, ISD::SETNE);
1153 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001154 }
1155 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001156 case ISD::SETCC:
1157 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1158 case Legal:
1159 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1160 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001161 break;
1162 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001163 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1164 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1165
1166 // If this is an FP compare, the operands have already been extended.
1167 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1168 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001169 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001170
1171 // Otherwise, we have to insert explicit sign or zero extends. Note
1172 // that we could insert sign extends for ALL conditions, but zero extend
1173 // is cheaper on many machines (an AND instead of two shifts), so prefer
1174 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001175 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001176 default: assert(0 && "Unknown integer comparison!");
1177 case ISD::SETEQ:
1178 case ISD::SETNE:
1179 case ISD::SETUGE:
1180 case ISD::SETUGT:
1181 case ISD::SETULE:
1182 case ISD::SETULT:
1183 // ALL of these operations will work if we either sign or zero extend
1184 // the operands (including the unsigned comparisons!). Zero extend is
1185 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001186 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1187 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001188 break;
1189 case ISD::SETGE:
1190 case ISD::SETGT:
1191 case ISD::SETLT:
1192 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001193 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1194 DAG.getValueType(VT));
1195 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1196 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001197 break;
1198 }
Chris Lattner4d978642005-01-15 22:16:26 +00001199 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001200 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001201 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001202 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1203 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1204 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001205 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001206 case ISD::SETEQ:
1207 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001208 if (RHSLo == RHSHi)
1209 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1210 if (RHSCST->isAllOnesValue()) {
1211 // Comparison to -1.
1212 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001213 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001214 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001215 }
1216
Chris Lattnerdc750592005-01-07 07:47:09 +00001217 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1218 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1219 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001220 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001221 break;
1222 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001223 // If this is a comparison of the sign bit, just look at the top part.
1224 // X > -1, x < 0
1225 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001226 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001227 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001228 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001229 (CST->isAllOnesValue()))) { // X > -1
1230 Tmp1 = LHSHi;
1231 Tmp2 = RHSHi;
1232 break;
1233 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001234
Chris Lattnerdc750592005-01-07 07:47:09 +00001235 // FIXME: This generated code sucks.
1236 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001237 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001238 default: assert(0 && "Unknown integer setcc!");
1239 case ISD::SETLT:
1240 case ISD::SETULT: LowCC = ISD::SETULT; break;
1241 case ISD::SETGT:
1242 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1243 case ISD::SETLE:
1244 case ISD::SETULE: LowCC = ISD::SETULE; break;
1245 case ISD::SETGE:
1246 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1247 }
Misha Brukman835702a2005-04-21 22:36:52 +00001248
Chris Lattnerdc750592005-01-07 07:47:09 +00001249 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1250 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1251 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1252
1253 // NOTE: on targets without efficient SELECT of bools, we can always use
1254 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001255 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1256 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1257 Node->getOperand(2));
1258 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001259 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1260 Result, Tmp1, Tmp2));
1261 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001262 }
1263 }
Nate Begeman987121a2005-08-23 04:29:48 +00001264
1265 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1266 default:
1267 assert(0 && "Cannot handle this action for SETCC yet!");
1268 break;
1269 case TargetLowering::Legal:
1270 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1271 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1272 Node->getOperand(2));
1273 break;
1274 case TargetLowering::Expand:
1275 // Expand a setcc node into a select_cc of the same condition, lhs, and
1276 // rhs that selects between const 1 (true) and const 0 (false).
1277 MVT::ValueType VT = Node->getValueType(0);
1278 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1279 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1280 Node->getOperand(2));
1281 Result = LegalizeOp(Result);
1282 break;
1283 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001284 break;
1285
Chris Lattner85d70c62005-01-11 05:57:22 +00001286 case ISD::MEMSET:
1287 case ISD::MEMCPY:
1288 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001289 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001290 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1291
1292 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1293 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1294 case Expand: assert(0 && "Cannot expand a byte!");
1295 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001296 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001297 break;
1298 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001299 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001300 break;
1301 }
1302 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001303 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001304 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001305
1306 SDOperand Tmp4;
1307 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001308 case Expand: {
1309 // Length is too big, just take the lo-part of the length.
1310 SDOperand HiPart;
1311 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1312 break;
1313 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001314 case Legal:
1315 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001316 break;
1317 case Promote:
1318 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001319 break;
1320 }
1321
1322 SDOperand Tmp5;
1323 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1324 case Expand: assert(0 && "Cannot expand this yet!");
1325 case Legal:
1326 Tmp5 = LegalizeOp(Node->getOperand(4));
1327 break;
1328 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001329 Tmp5 = PromoteOp(Node->getOperand(4));
1330 break;
1331 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001332
1333 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1334 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001335 case TargetLowering::Custom: {
1336 SDOperand Tmp =
1337 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1338 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1339 if (Tmp.Val) {
1340 Result = LegalizeOp(Tmp);
1341 break;
1342 }
1343 // FALLTHROUGH if the target thinks it is legal.
1344 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001345 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001346 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1347 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1348 Tmp5 != Node->getOperand(4)) {
1349 std::vector<SDOperand> Ops;
1350 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1351 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1352 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1353 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001354 break;
1355 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001356 // Otherwise, the target does not support this operation. Lower the
1357 // operation to an explicit libcall as appropriate.
1358 MVT::ValueType IntPtr = TLI.getPointerTy();
1359 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1360 std::vector<std::pair<SDOperand, const Type*> > Args;
1361
Reid Spencer6dced922005-01-12 14:53:45 +00001362 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001363 if (Node->getOpcode() == ISD::MEMSET) {
1364 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1365 // Extend the ubyte argument to be an int value for the call.
1366 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1367 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1368 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1369
1370 FnName = "memset";
1371 } else if (Node->getOpcode() == ISD::MEMCPY ||
1372 Node->getOpcode() == ISD::MEMMOVE) {
1373 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1374 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1375 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1376 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1377 } else {
1378 assert(0 && "Unknown op!");
1379 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001380
Chris Lattner85d70c62005-01-11 05:57:22 +00001381 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001382 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001383 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001384 Result = CallResult.second;
1385 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001386 break;
1387 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001388 }
1389 break;
1390 }
Chris Lattner5385db52005-05-09 20:23:03 +00001391
1392 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001393 Tmp1 = LegalizeOp(Node->getOperand(0));
1394 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001395
Chris Lattner86535992005-05-14 07:45:46 +00001396 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1397 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1398 std::vector<SDOperand> Ops;
1399 Ops.push_back(Tmp1);
1400 Ops.push_back(Tmp2);
1401 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1402 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001403 Result = SDOperand(Node, 0);
1404 // Since these produce two values, make sure to remember that we legalized
1405 // both of them.
1406 AddLegalizedOperand(SDOperand(Node, 0), Result);
1407 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1408 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001409 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001410 Tmp1 = LegalizeOp(Node->getOperand(0));
1411 Tmp2 = LegalizeOp(Node->getOperand(1));
1412 Tmp3 = LegalizeOp(Node->getOperand(2));
1413 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1414 Tmp3 != Node->getOperand(2))
1415 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1416 break;
1417
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001418 case ISD::READIO:
1419 Tmp1 = LegalizeOp(Node->getOperand(0));
1420 Tmp2 = LegalizeOp(Node->getOperand(1));
1421
1422 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1423 case TargetLowering::Custom:
1424 default: assert(0 && "This action not implemented for this operation!");
1425 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001426 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1427 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1428 std::vector<SDOperand> Ops;
1429 Ops.push_back(Tmp1);
1430 Ops.push_back(Tmp2);
1431 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1432 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001433 Result = SDOperand(Node, 0);
1434 break;
1435 case TargetLowering::Expand:
1436 // Replace this with a load from memory.
1437 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1438 Node->getOperand(1), DAG.getSrcValue(NULL));
1439 Result = LegalizeOp(Result);
1440 break;
1441 }
1442
1443 // Since these produce two values, make sure to remember that we legalized
1444 // both of them.
1445 AddLegalizedOperand(SDOperand(Node, 0), Result);
1446 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1447 return Result.getValue(Op.ResNo);
1448
1449 case ISD::WRITEIO:
1450 Tmp1 = LegalizeOp(Node->getOperand(0));
1451 Tmp2 = LegalizeOp(Node->getOperand(1));
1452 Tmp3 = LegalizeOp(Node->getOperand(2));
1453
1454 switch (TLI.getOperationAction(Node->getOpcode(),
1455 Node->getOperand(1).getValueType())) {
1456 case TargetLowering::Custom:
1457 default: assert(0 && "This action not implemented for this operation!");
1458 case TargetLowering::Legal:
1459 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1460 Tmp3 != Node->getOperand(2))
1461 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1462 break;
1463 case TargetLowering::Expand:
1464 // Replace this with a store to memory.
1465 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1466 Node->getOperand(1), Node->getOperand(2),
1467 DAG.getSrcValue(NULL));
1468 Result = LegalizeOp(Result);
1469 break;
1470 }
1471 break;
1472
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001473 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001474 case ISD::SUB_PARTS:
1475 case ISD::SHL_PARTS:
1476 case ISD::SRA_PARTS:
1477 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001478 std::vector<SDOperand> Ops;
1479 bool Changed = false;
1480 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1481 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1482 Changed |= Ops.back() != Node->getOperand(i);
1483 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001484 if (Changed) {
1485 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1486 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1487 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001488
1489 // Since these produce multiple values, make sure to remember that we
1490 // legalized all of them.
1491 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1492 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1493 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001494 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001495
1496 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001497 case ISD::ADD:
1498 case ISD::SUB:
1499 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001500 case ISD::MULHS:
1501 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001502 case ISD::UDIV:
1503 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001504 case ISD::AND:
1505 case ISD::OR:
1506 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001507 case ISD::SHL:
1508 case ISD::SRL:
1509 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +00001510 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001511 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1512 case Expand: assert(0 && "Not possible");
1513 case Legal:
1514 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1515 break;
1516 case Promote:
1517 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1518 break;
1519 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001520 if (Tmp1 != Node->getOperand(0) ||
1521 Tmp2 != Node->getOperand(1))
1522 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1523 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001524
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001525 case ISD::UREM:
1526 case ISD::SREM:
1527 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1528 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1529 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1530 case TargetLowering::Legal:
1531 if (Tmp1 != Node->getOperand(0) ||
1532 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001533 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001534 Tmp2);
1535 break;
1536 case TargetLowering::Promote:
1537 case TargetLowering::Custom:
1538 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001539 case TargetLowering::Expand:
1540 if (MVT::isInteger(Node->getValueType(0))) {
1541 MVT::ValueType VT = Node->getValueType(0);
1542 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1543 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1544 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1545 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1546 } else {
1547 // Floating point mod -> fmod libcall.
1548 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1549 SDOperand Dummy;
1550 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001551 }
1552 break;
1553 }
1554 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001555
Andrew Lenharth5e177822005-05-03 17:19:30 +00001556 case ISD::CTPOP:
1557 case ISD::CTTZ:
1558 case ISD::CTLZ:
1559 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1560 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1561 case TargetLowering::Legal:
1562 if (Tmp1 != Node->getOperand(0))
1563 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1564 break;
1565 case TargetLowering::Promote: {
1566 MVT::ValueType OVT = Tmp1.getValueType();
1567 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001568
1569 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001570 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1571 // Perform the larger operation, then subtract if needed.
1572 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1573 switch(Node->getOpcode())
1574 {
1575 case ISD::CTPOP:
1576 Result = Tmp1;
1577 break;
1578 case ISD::CTTZ:
1579 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001580 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1581 DAG.getConstant(getSizeInBits(NVT), NVT),
1582 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001583 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001584 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1585 break;
1586 case ISD::CTLZ:
1587 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001588 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1589 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001590 getSizeInBits(OVT), NVT));
1591 break;
1592 }
1593 break;
1594 }
1595 case TargetLowering::Custom:
1596 assert(0 && "Cannot custom handle this yet!");
1597 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001598 switch(Node->getOpcode())
1599 {
1600 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001601 static const uint64_t mask[6] = {
1602 0x5555555555555555ULL, 0x3333333333333333ULL,
1603 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1604 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1605 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001606 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001607 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1608 unsigned len = getSizeInBits(VT);
1609 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001610 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001611 Tmp2 = DAG.getConstant(mask[i], VT);
1612 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001613 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001614 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1615 DAG.getNode(ISD::AND, VT,
1616 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1617 Tmp2));
1618 }
1619 Result = Tmp1;
1620 break;
1621 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001622 case ISD::CTLZ: {
1623 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001624 x = x | (x >> 1);
1625 x = x | (x >> 2);
1626 ...
1627 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001628 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001629 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001630
Chris Lattner56add052005-05-11 18:35:21 +00001631 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1632 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001633 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1634 unsigned len = getSizeInBits(VT);
1635 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1636 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001637 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001638 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1639 }
1640 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001641 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001642 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001643 }
1644 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001645 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001646 // unless the target has ctlz but not ctpop, in which case we use:
1647 // { return 32 - nlz(~x & (x-1)); }
1648 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001649 MVT::ValueType VT = Tmp1.getValueType();
1650 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001651 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001652 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1653 DAG.getNode(ISD::SUB, VT, Tmp1,
1654 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001655 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001656 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1657 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001658 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001659 DAG.getConstant(getSizeInBits(VT), VT),
1660 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1661 } else {
1662 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1663 }
Chris Lattner72473242005-05-11 05:27:09 +00001664 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001665 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001666 default:
1667 assert(0 && "Cannot expand this yet!");
1668 break;
1669 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001670 break;
1671 }
1672 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001673
Chris Lattner13fe99c2005-04-02 05:00:07 +00001674 // Unary operators
1675 case ISD::FABS:
1676 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001677 case ISD::FSQRT:
1678 case ISD::FSIN:
1679 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001680 Tmp1 = LegalizeOp(Node->getOperand(0));
1681 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1682 case TargetLowering::Legal:
1683 if (Tmp1 != Node->getOperand(0))
1684 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1685 break;
1686 case TargetLowering::Promote:
1687 case TargetLowering::Custom:
1688 assert(0 && "Cannot promote/custom handle this yet!");
1689 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001690 switch(Node->getOpcode()) {
1691 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001692 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1693 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1694 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1695 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001696 break;
1697 }
1698 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001699 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1700 MVT::ValueType VT = Node->getValueType(0);
1701 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001702 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001703 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1704 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1705 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001706 break;
1707 }
1708 case ISD::FSQRT:
1709 case ISD::FSIN:
1710 case ISD::FCOS: {
1711 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00001712 const char *FnName = 0;
1713 switch(Node->getOpcode()) {
1714 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1715 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1716 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1717 default: assert(0 && "Unreachable!");
1718 }
Nate Begeman77558da2005-08-04 21:43:28 +00001719 SDOperand Dummy;
1720 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00001721 break;
1722 }
1723 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001724 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001725 }
1726 break;
1727 }
1728 break;
1729
1730 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001731 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001732 case ISD::UINT_TO_FP: {
1733 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001734 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1735 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00001736 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001737 Node->getOperand(0).getValueType())) {
1738 default: assert(0 && "Unknown operation action!");
1739 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00001740 Result = ExpandLegalINT_TO_FP(isSigned,
1741 LegalizeOp(Node->getOperand(0)),
1742 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001743 AddLegalizedOperand(Op, Result);
1744 return Result;
1745 case TargetLowering::Promote:
1746 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1747 Node->getValueType(0),
1748 isSigned);
1749 AddLegalizedOperand(Op, Result);
1750 return Result;
1751 case TargetLowering::Legal:
1752 break;
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001753 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001754
Chris Lattnerdc750592005-01-07 07:47:09 +00001755 Tmp1 = LegalizeOp(Node->getOperand(0));
1756 if (Tmp1 != Node->getOperand(0))
1757 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1758 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001759 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001760 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1761 Node->getValueType(0), Node->getOperand(0));
1762 break;
1763 case Promote:
1764 if (isSigned) {
1765 Result = PromoteOp(Node->getOperand(0));
1766 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1767 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1768 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1769 } else {
1770 Result = PromoteOp(Node->getOperand(0));
1771 Result = DAG.getZeroExtendInReg(Result,
1772 Node->getOperand(0).getValueType());
1773 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00001774 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001775 break;
1776 }
1777 break;
1778 }
1779 case ISD::TRUNCATE:
1780 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1781 case Legal:
1782 Tmp1 = LegalizeOp(Node->getOperand(0));
1783 if (Tmp1 != Node->getOperand(0))
1784 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1785 break;
1786 case Expand:
1787 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1788
1789 // Since the result is legal, we should just be able to truncate the low
1790 // part of the source.
1791 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1792 break;
1793 case Promote:
1794 Result = PromoteOp(Node->getOperand(0));
1795 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1796 break;
1797 }
1798 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001799
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001800 case ISD::FP_TO_SINT:
1801 case ISD::FP_TO_UINT:
1802 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1803 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001804 Tmp1 = LegalizeOp(Node->getOperand(0));
1805
Chris Lattner44fe26f2005-07-29 00:11:56 +00001806 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1807 default: assert(0 && "Unknown operation action!");
1808 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00001809 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1810 SDOperand True, False;
1811 MVT::ValueType VT = Node->getOperand(0).getValueType();
1812 MVT::ValueType NVT = Node->getValueType(0);
1813 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1814 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1815 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1816 Node->getOperand(0), Tmp2, ISD::SETLT);
1817 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1818 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
1819 DAG.getNode(ISD::SUB, VT, Node->getOperand(0),
1820 Tmp2));
1821 False = DAG.getNode(ISD::XOR, NVT, False,
1822 DAG.getConstant(1ULL << ShiftAmt, NVT));
1823 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00001824 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00001825 } else {
1826 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1827 }
1828 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001829 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001830 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00001831 Node->getOpcode() == ISD::FP_TO_SINT);
1832 AddLegalizedOperand(Op, Result);
1833 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001834 case TargetLowering::Custom: {
1835 SDOperand Tmp =
1836 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1837 Tmp = TLI.LowerOperation(Tmp, DAG);
1838 if (Tmp.Val) {
1839 AddLegalizedOperand(Op, Tmp);
1840 NeedsAnotherIteration = true;
Chris Lattnerdcde1b22005-08-29 17:30:00 +00001841 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001842 } else {
1843 // The target thinks this is legal afterall.
1844 break;
1845 }
1846 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00001847 case TargetLowering::Legal:
1848 break;
1849 }
Jeff Cohen546fd592005-07-30 18:33:25 +00001850
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001851 if (Tmp1 != Node->getOperand(0))
1852 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1853 break;
1854 case Expand:
1855 assert(0 && "Shouldn't need to expand other operators here!");
1856 case Promote:
1857 Result = PromoteOp(Node->getOperand(0));
1858 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1859 break;
1860 }
1861 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001862
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001863 case ISD::ZERO_EXTEND:
1864 case ISD::SIGN_EXTEND:
1865 case ISD::FP_EXTEND:
1866 case ISD::FP_ROUND:
1867 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1868 case Legal:
1869 Tmp1 = LegalizeOp(Node->getOperand(0));
1870 if (Tmp1 != Node->getOperand(0))
1871 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1872 break;
1873 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001874 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001875
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001876 case Promote:
1877 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001878 case ISD::ZERO_EXTEND:
1879 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001880 // NOTE: Any extend would work here...
1881 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001882 Result = DAG.getZeroExtendInReg(Result,
1883 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001884 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001885 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001886 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001887 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001888 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001889 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001890 Result,
1891 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001892 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001893 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001894 Result = PromoteOp(Node->getOperand(0));
1895 if (Result.getValueType() != Op.getValueType())
1896 // Dynamically dead while we have only 2 FP types.
1897 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1898 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001899 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001900 Result = PromoteOp(Node->getOperand(0));
1901 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1902 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001903 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001904 }
1905 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001906 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001907 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001908 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001909 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00001910
1911 // If this operation is not supported, convert it to a shl/shr or load/store
1912 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001913 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1914 default: assert(0 && "This action not supported for this op yet!");
1915 case TargetLowering::Legal:
1916 if (Tmp1 != Node->getOperand(0))
1917 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001918 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00001919 break;
1920 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001921 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001922 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001923 // NOTE: we could fall back on load/store here too for targets without
1924 // SAR. However, it is doubtful that any exist.
1925 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1926 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001927 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001928 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1929 Node->getOperand(0), ShiftCst);
1930 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1931 Result, ShiftCst);
1932 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1933 // The only way we can lower this is to turn it into a STORETRUNC,
1934 // EXTLOAD pair, targetting a temporary location (a stack slot).
1935
1936 // NOTE: there is a choice here between constantly creating new stack
1937 // slots and always reusing the same one. We currently always create
1938 // new ones, as reuse may inhibit scheduling.
1939 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1940 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1941 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1942 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001943 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001944 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1945 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1946 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001947 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001948 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001949 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1950 Result, StackSlot, DAG.getSrcValue(NULL),
1951 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001952 } else {
1953 assert(0 && "Unknown op");
1954 }
1955 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001956 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001957 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001958 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001959 }
Chris Lattner99222f72005-01-15 07:15:18 +00001960 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001961
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001962 // Note that LegalizeOp may be reentered even from single-use nodes, which
1963 // means that we always must cache transformed nodes.
1964 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001965 return Result;
1966}
1967
Chris Lattner4d978642005-01-15 22:16:26 +00001968/// PromoteOp - Given an operation that produces a value in an invalid type,
1969/// promote it to compute the value into a larger type. The produced value will
1970/// have the correct bits for the low portion of the register, but no guarantee
1971/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001972SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1973 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001974 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001975 assert(getTypeAction(VT) == Promote &&
1976 "Caller should expand or legalize operands that are not promotable!");
1977 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1978 "Cannot promote to smaller type!");
1979
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001980 SDOperand Tmp1, Tmp2, Tmp3;
1981
1982 SDOperand Result;
1983 SDNode *Node = Op.Val;
1984
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001985 if (!Node->hasOneUse()) {
1986 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1987 if (I != PromotedNodes.end()) return I->second;
1988 } else {
1989 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1990 }
1991
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001992 // Promotion needs an optimization step to clean up after it, and is not
1993 // careful to avoid operations the target does not support. Make sure that
1994 // all generated operations are legalized in the next iteration.
1995 NeedsAnotherIteration = true;
1996
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001997 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00001998 case ISD::CopyFromReg:
1999 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002000 default:
2001 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2002 assert(0 && "Do not know how to promote this operator!");
2003 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002004 case ISD::UNDEF:
2005 Result = DAG.getNode(ISD::UNDEF, NVT);
2006 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002007 case ISD::Constant:
Chris Lattner56ca46e2005-08-26 22:50:40 +00002008 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002009 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2010 break;
2011 case ISD::ConstantFP:
2012 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2013 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2014 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002015
Chris Lattner2cb338d2005-01-18 02:59:52 +00002016 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002017 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002018 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2019 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002020 Result = LegalizeOp(Result);
2021 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002022
2023 case ISD::TRUNCATE:
2024 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2025 case Legal:
2026 Result = LegalizeOp(Node->getOperand(0));
2027 assert(Result.getValueType() >= NVT &&
2028 "This truncation doesn't make sense!");
2029 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2030 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2031 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002032 case Promote:
2033 // The truncation is not required, because we don't guarantee anything
2034 // about high bits anyway.
2035 Result = PromoteOp(Node->getOperand(0));
2036 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002037 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002038 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2039 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002040 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002041 }
2042 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002043 case ISD::SIGN_EXTEND:
2044 case ISD::ZERO_EXTEND:
2045 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2046 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2047 case Legal:
2048 // Input is legal? Just do extend all the way to the larger type.
2049 Result = LegalizeOp(Node->getOperand(0));
2050 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2051 break;
2052 case Promote:
2053 // Promote the reg if it's smaller.
2054 Result = PromoteOp(Node->getOperand(0));
2055 // The high bits are not guaranteed to be anything. Insert an extend.
2056 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002057 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002058 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002059 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002060 Result = DAG.getZeroExtendInReg(Result,
2061 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002062 break;
2063 }
2064 break;
2065
2066 case ISD::FP_EXTEND:
2067 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2068 case ISD::FP_ROUND:
2069 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2070 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2071 case Promote: assert(0 && "Unreachable with 2 FP types!");
2072 case Legal:
2073 // Input is legal? Do an FP_ROUND_INREG.
2074 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002075 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2076 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002077 break;
2078 }
2079 break;
2080
2081 case ISD::SINT_TO_FP:
2082 case ISD::UINT_TO_FP:
2083 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2084 case Legal:
2085 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002086 // No extra round required here.
2087 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002088 break;
2089
2090 case Promote:
2091 Result = PromoteOp(Node->getOperand(0));
2092 if (Node->getOpcode() == ISD::SINT_TO_FP)
2093 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002094 Result,
2095 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002096 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002097 Result = DAG.getZeroExtendInReg(Result,
2098 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002099 // No extra round required here.
2100 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002101 break;
2102 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002103 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2104 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002105 // Round if we cannot tolerate excess precision.
2106 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002107 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2108 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002109 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002110 }
Chris Lattner4d978642005-01-15 22:16:26 +00002111 break;
2112
2113 case ISD::FP_TO_SINT:
2114 case ISD::FP_TO_UINT:
2115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2116 case Legal:
2117 Tmp1 = LegalizeOp(Node->getOperand(0));
2118 break;
2119 case Promote:
2120 // The input result is prerounded, so we don't have to do anything
2121 // special.
2122 Tmp1 = PromoteOp(Node->getOperand(0));
2123 break;
2124 case Expand:
2125 assert(0 && "not implemented");
2126 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002127 // If we're promoting a UINT to a larger size, check to see if the new node
2128 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2129 // we can use that instead. This allows us to generate better code for
2130 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2131 // legal, such as PowerPC.
2132 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002133 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
2134 TLI.isOperationLegal(ISD::FP_TO_SINT, NVT)) {
Nate Begeman36853ee2005-08-14 01:20:53 +00002135 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2136 } else {
2137 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2138 }
Chris Lattner4d978642005-01-15 22:16:26 +00002139 break;
2140
Chris Lattner13fe99c2005-04-02 05:00:07 +00002141 case ISD::FABS:
2142 case ISD::FNEG:
2143 Tmp1 = PromoteOp(Node->getOperand(0));
2144 assert(Tmp1.getValueType() == NVT);
2145 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2146 // NOTE: we do not have to do any extra rounding here for
2147 // NoExcessFPPrecision, because we know the input will have the appropriate
2148 // precision, and these operations don't modify precision at all.
2149 break;
2150
Chris Lattner9d6fa982005-04-28 21:44:33 +00002151 case ISD::FSQRT:
2152 case ISD::FSIN:
2153 case ISD::FCOS:
2154 Tmp1 = PromoteOp(Node->getOperand(0));
2155 assert(Tmp1.getValueType() == NVT);
2156 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2157 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002158 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2159 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002160 break;
2161
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002162 case ISD::AND:
2163 case ISD::OR:
2164 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002165 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002166 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002167 case ISD::MUL:
2168 // The input may have strange things in the top bits of the registers, but
2169 // these operations don't care. They may have wierd bits going out, but
2170 // that too is okay if they are integer operations.
2171 Tmp1 = PromoteOp(Node->getOperand(0));
2172 Tmp2 = PromoteOp(Node->getOperand(1));
2173 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2174 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2175
2176 // However, if this is a floating point operation, they will give excess
2177 // precision that we may not be able to tolerate. If we DO allow excess
2178 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002179 // FIXME: Why would we need to round FP ops more than integer ones?
2180 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002181 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002182 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2183 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002184 break;
2185
Chris Lattner4d978642005-01-15 22:16:26 +00002186 case ISD::SDIV:
2187 case ISD::SREM:
2188 // These operators require that their input be sign extended.
2189 Tmp1 = PromoteOp(Node->getOperand(0));
2190 Tmp2 = PromoteOp(Node->getOperand(1));
2191 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002192 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2193 DAG.getValueType(VT));
2194 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2195 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002196 }
2197 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2198
2199 // Perform FP_ROUND: this is probably overly pessimistic.
2200 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002201 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2202 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002203 break;
2204
2205 case ISD::UDIV:
2206 case ISD::UREM:
2207 // These operators require that their input be zero extended.
2208 Tmp1 = PromoteOp(Node->getOperand(0));
2209 Tmp2 = PromoteOp(Node->getOperand(1));
2210 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002211 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2212 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002213 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2214 break;
2215
2216 case ISD::SHL:
2217 Tmp1 = PromoteOp(Node->getOperand(0));
2218 Tmp2 = LegalizeOp(Node->getOperand(1));
2219 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2220 break;
2221 case ISD::SRA:
2222 // The input value must be properly sign extended.
2223 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002224 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2225 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002226 Tmp2 = LegalizeOp(Node->getOperand(1));
2227 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2228 break;
2229 case ISD::SRL:
2230 // The input value must be properly zero extended.
2231 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002232 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002233 Tmp2 = LegalizeOp(Node->getOperand(1));
2234 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2235 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002236 case ISD::LOAD:
2237 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2238 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00002239 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00002240 if (MVT::isInteger(NVT))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002241 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2242 Node->getOperand(2), VT);
Chris Lattner391a3512005-04-10 17:40:35 +00002243 else
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002244 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2245 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002246
2247 // Remember that we legalized the chain.
2248 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2249 break;
2250 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002251 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2252 case Expand: assert(0 && "It's impossible to expand bools");
2253 case Legal:
2254 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2255 break;
2256 case Promote:
2257 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2258 break;
2259 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002260 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2261 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2262 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2263 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002264 case ISD::SELECT_CC:
2265 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2266 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2267 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2268 Node->getOperand(1), Tmp2, Tmp3,
2269 Node->getOperand(4));
2270 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002271 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002272 case ISD::CALL: {
2273 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2274 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2275
Chris Lattner3d95c142005-01-19 20:24:35 +00002276 std::vector<SDOperand> Ops;
2277 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2278 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2279
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002280 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2281 "Can only promote single result calls");
2282 std::vector<MVT::ValueType> RetTyVTs;
2283 RetTyVTs.reserve(2);
2284 RetTyVTs.push_back(NVT);
2285 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002286 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2287 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002288 Result = SDOperand(NC, 0);
2289
2290 // Insert the new chain mapping.
2291 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2292 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002293 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002294 case ISD::CTPOP:
2295 case ISD::CTTZ:
2296 case ISD::CTLZ:
2297 Tmp1 = Node->getOperand(0);
2298 //Zero extend the argument
2299 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2300 // Perform the larger operation, then subtract if needed.
2301 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2302 switch(Node->getOpcode())
2303 {
2304 case ISD::CTPOP:
2305 Result = Tmp1;
2306 break;
2307 case ISD::CTTZ:
2308 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002309 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002310 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002311 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002312 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2313 break;
2314 case ISD::CTLZ:
2315 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002316 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2317 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002318 getSizeInBits(VT), NVT));
2319 break;
2320 }
2321 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002322 }
2323
2324 assert(Result.Val && "Didn't set a result!");
2325 AddPromotedOperand(Op, Result);
2326 return Result;
2327}
Chris Lattnerdc750592005-01-07 07:47:09 +00002328
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002329/// ExpandAddSub - Find a clever way to expand this add operation into
2330/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002331void SelectionDAGLegalize::
2332ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2333 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002334 // Expand the subcomponents.
2335 SDOperand LHSL, LHSH, RHSL, RHSH;
2336 ExpandOp(LHS, LHSL, LHSH);
2337 ExpandOp(RHS, RHSL, RHSH);
2338
Chris Lattner8ffd0042005-04-11 20:29:59 +00002339 // FIXME: this should be moved to the dag combiner someday.
Chris Lattner669e8c22005-05-14 07:25:05 +00002340 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2341 if (LHSL.getValueType() == MVT::i32) {
2342 SDOperand LowEl;
2343 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2344 if (C->getValue() == 0)
2345 LowEl = RHSL;
2346 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2347 if (C->getValue() == 0)
2348 LowEl = LHSL;
2349 if (LowEl.Val) {
2350 // Turn this into an add/sub of the high part only.
2351 SDOperand HiEl =
2352 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2353 LowEl.getValueType(), LHSH, RHSH);
2354 Lo = LowEl;
2355 Hi = HiEl;
2356 return;
Chris Lattner8ffd0042005-04-11 20:29:59 +00002357 }
Chris Lattner669e8c22005-05-14 07:25:05 +00002358 }
Chris Lattner8ffd0042005-04-11 20:29:59 +00002359
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002360 std::vector<SDOperand> Ops;
2361 Ops.push_back(LHSL);
2362 Ops.push_back(LHSH);
2363 Ops.push_back(RHSL);
2364 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002365
2366 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2367 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002368 Hi = Lo.getValue(1);
2369}
2370
Chris Lattner4157c412005-04-02 04:00:59 +00002371void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2372 SDOperand Op, SDOperand Amt,
2373 SDOperand &Lo, SDOperand &Hi) {
2374 // Expand the subcomponents.
2375 SDOperand LHSL, LHSH;
2376 ExpandOp(Op, LHSL, LHSH);
2377
2378 std::vector<SDOperand> Ops;
2379 Ops.push_back(LHSL);
2380 Ops.push_back(LHSH);
2381 Ops.push_back(Amt);
Chris Lattner669e8c22005-05-14 07:25:05 +00002382 std::vector<MVT::ValueType> VTs;
2383 VTs.push_back(LHSL.getValueType());
2384 VTs.push_back(LHSH.getValueType());
2385 VTs.push_back(Amt.getValueType());
2386 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002387 Hi = Lo.getValue(1);
2388}
2389
2390
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002391/// ExpandShift - Try to find a clever way to expand this shift operation out to
2392/// smaller elements. If we can't find a way that is more efficient than a
2393/// libcall on this target, return false. Otherwise, return true with the
2394/// low-parts expanded into Lo and Hi.
2395bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2396 SDOperand &Lo, SDOperand &Hi) {
2397 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2398 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002399
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002400 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002401 SDOperand ShAmt = LegalizeOp(Amt);
2402 MVT::ValueType ShTy = ShAmt.getValueType();
2403 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2404 unsigned NVTBits = MVT::getSizeInBits(NVT);
2405
2406 // Handle the case when Amt is an immediate. Other cases are currently broken
2407 // and are disabled.
2408 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2409 unsigned Cst = CN->getValue();
2410 // Expand the incoming operand to be shifted, so that we have its parts
2411 SDOperand InL, InH;
2412 ExpandOp(Op, InL, InH);
2413 switch(Opc) {
2414 case ISD::SHL:
2415 if (Cst > VTBits) {
2416 Lo = DAG.getConstant(0, NVT);
2417 Hi = DAG.getConstant(0, NVT);
2418 } else if (Cst > NVTBits) {
2419 Lo = DAG.getConstant(0, NVT);
2420 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002421 } else if (Cst == NVTBits) {
2422 Lo = DAG.getConstant(0, NVT);
2423 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002424 } else {
2425 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2426 Hi = DAG.getNode(ISD::OR, NVT,
2427 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2428 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2429 }
2430 return true;
2431 case ISD::SRL:
2432 if (Cst > VTBits) {
2433 Lo = DAG.getConstant(0, NVT);
2434 Hi = DAG.getConstant(0, NVT);
2435 } else if (Cst > NVTBits) {
2436 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2437 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002438 } else if (Cst == NVTBits) {
2439 Lo = InH;
2440 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002441 } else {
2442 Lo = DAG.getNode(ISD::OR, NVT,
2443 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2444 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2445 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2446 }
2447 return true;
2448 case ISD::SRA:
2449 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002450 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002451 DAG.getConstant(NVTBits-1, ShTy));
2452 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002453 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002454 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002455 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002456 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002457 } else if (Cst == NVTBits) {
2458 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002459 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002460 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002461 } else {
2462 Lo = DAG.getNode(ISD::OR, NVT,
2463 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2464 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2465 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2466 }
2467 return true;
2468 }
2469 }
2470 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2471 // so disable it for now. Currently targets are handling this via SHL_PARTS
2472 // and friends.
2473 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002474
2475 // If we have an efficient select operation (or if the selects will all fold
2476 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002477 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002478 return false;
2479
2480 SDOperand InL, InH;
2481 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002482 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2483 DAG.getConstant(NVTBits, ShTy), ShAmt);
2484
Chris Lattner4d25c042005-01-20 20:29:23 +00002485 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002486 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2487 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002488
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002489 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2490 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2491 DAG.getConstant(NVTBits-1, ShTy));
2492 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2493 DAG.getConstant(NVTBits-1, ShTy));
2494 }
2495
2496 if (Opc == ISD::SHL) {
2497 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2498 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2499 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002500 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002501
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002502 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2503 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2504 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002505 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002506 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2507 DAG.getConstant(32, ShTy),
2508 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002509 DAG.getConstant(0, NVT),
2510 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002511 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002512 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002513 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002514 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002515
2516 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002517 if (Opc == ISD::SRA)
2518 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2519 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002520 else
2521 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002522 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002523 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002524 }
2525 return true;
2526}
Chris Lattneraac464e2005-01-21 06:05:23 +00002527
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002528/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2529/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002530/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002531static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002532 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002533
Chris Lattner2dce7032005-05-12 23:24:06 +00002534 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002535 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002536 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002537 Found = Node;
2538 return;
2539 }
2540
2541 // Otherwise, scan the operands of Node to see if any of them is a call.
2542 assert(Node->getNumOperands() != 0 &&
2543 "All leaves should have depth equal to the entry node!");
2544 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002545 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002546
2547 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002548 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002549 Found);
2550}
2551
2552
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002553/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2554/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002555/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002556static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2557 std::set<SDNode*> &Visited) {
2558 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2559 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002560
Chris Lattner2dce7032005-05-12 23:24:06 +00002561 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002562 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002563 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002564 Found = Node;
2565 return;
2566 }
2567
2568 // Otherwise, scan the operands of Node to see if any of them is a call.
2569 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2570 if (UI == E) return;
2571 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002572 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002573
2574 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002575 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002576}
2577
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002578/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002579/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002580static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002581 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002582 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002583 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002584 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002585
2586 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002587 return FindCallSeqEnd(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00002588
Chris Lattner4add7e32005-01-23 04:42:50 +00002589 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002590 if (TheChain.getValueType() != MVT::Other)
2591 TheChain = SDOperand(Node, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002592 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00002593
2594 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002595 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002596
Chris Lattner4add7e32005-01-23 04:42:50 +00002597 // Make sure to only follow users of our token chain.
2598 SDNode *User = *UI;
2599 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2600 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002601 if (SDNode *Result = FindCallSeqEnd(User))
2602 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002603 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002604 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002605}
2606
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002607/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002608/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002609static SDNode *FindCallSeqStart(SDNode *Node) {
2610 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002611 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002612
2613 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2614 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002615 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002616}
2617
2618
Chris Lattner4add7e32005-01-23 04:42:50 +00002619/// FindInputOutputChains - If we are replacing an operation with a call we need
2620/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002621/// properly serialize the calls in the block. The returned operand is the
2622/// input chain value for the new call (e.g. the entry node or the previous
2623/// call), and OutChain is set to be the chain node to update to point to the
2624/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002625static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2626 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002627 SDNode *LatestCallSeqStart = Entry.Val;
2628 SDNode *LatestCallSeqEnd = 0;
2629 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2630 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002631
Chris Lattner2dce7032005-05-12 23:24:06 +00002632 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002633 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002634 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2635 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002636 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2637 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanadd0c632005-04-11 03:01:51 +00002638 else
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002639 LatestCallSeqEnd = Entry.Val;
2640 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002641
Chris Lattner06bbeb62005-05-11 19:02:11 +00002642 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002643 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002644 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002645 std::set<SDNode*> Visited;
2646 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002647
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002648 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002649 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002650 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002651
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002652 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002653}
2654
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002655/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002656void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2657 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002658 // Nothing to splice it into?
2659 if (OutChain == 0) return;
2660
2661 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2662 //OutChain->dump();
2663
2664 // Form a token factor node merging the old inval and the new inval.
2665 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2666 OutChain->getOperand(0));
2667 // Change the node to refer to the new token.
2668 OutChain->setAdjCallChain(InToken);
2669}
Chris Lattner4add7e32005-01-23 04:42:50 +00002670
2671
Chris Lattneraac464e2005-01-21 06:05:23 +00002672// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2673// does not fit into a register, return the lo part and set the hi part to the
2674// by-reg argument. If it does fit into a single register, return the result
2675// and leave the Hi part unset.
2676SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2677 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002678 SDNode *OutChain;
2679 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2680 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002681 if (InChain.Val == 0)
2682 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002683
Chris Lattneraac464e2005-01-21 06:05:23 +00002684 TargetLowering::ArgListTy Args;
2685 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2686 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2687 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2688 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2689 }
2690 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002691
Chris Lattner06bbeb62005-05-11 19:02:11 +00002692 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002693 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002694 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002695 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2696 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002697 SpliceCallInto(CallInfo.second, OutChain);
2698
2699 NeedsAnotherIteration = true;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002700
2701 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002702 default: assert(0 && "Unknown thing");
2703 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002704 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002705 case Promote:
2706 assert(0 && "Cannot promote this yet!");
2707 case Expand:
2708 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002709 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002710 return Lo;
2711 }
2712}
2713
Chris Lattner4add7e32005-01-23 04:42:50 +00002714
Chris Lattneraac464e2005-01-21 06:05:23 +00002715/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2716/// destination type is legal.
2717SDOperand SelectionDAGLegalize::
2718ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002719 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00002720 assert(getTypeAction(Source.getValueType()) == Expand &&
2721 "This is not an expansion!");
2722 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2723
Chris Lattner06bbeb62005-05-11 19:02:11 +00002724 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002725 assert(Source.getValueType() == MVT::i64 &&
2726 "This only works for 64-bit -> FP");
2727 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2728 // incoming integer is set. To handle this, we dynamically test to see if
2729 // it is set, and, if so, add a fudge factor.
2730 SDOperand Lo, Hi;
2731 ExpandOp(Source, Lo, Hi);
2732
Chris Lattner2a4f7312005-05-13 04:45:13 +00002733 // If this is unsigned, and not supported, first perform the conversion to
2734 // signed, then adjust the result if the sign bit is set.
2735 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2736 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2737
Chris Lattnerd47675e2005-08-09 20:20:18 +00002738 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2739 DAG.getConstant(0, Hi.getValueType()),
2740 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002741 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2742 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2743 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002744 uint64_t FF = 0x5f800000ULL;
2745 if (TLI.isLittleEndian()) FF <<= 32;
2746 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002747
Chris Lattnerc30405e2005-08-26 17:15:30 +00002748 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002749 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2750 SDOperand FudgeInReg;
2751 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002752 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2753 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002754 else {
2755 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002756 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2757 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002758 }
2759 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002760 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002761
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002762 // Check to see if the target has a custom way to lower this. If so, use it.
2763 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2764 default: assert(0 && "This action not implemented for this operation!");
2765 case TargetLowering::Legal:
2766 case TargetLowering::Expand:
2767 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002768 case TargetLowering::Custom: {
2769 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
2770 Source), DAG);
2771 if (NV.Val)
2772 return LegalizeOp(NV);
2773 break; // The target decided this was legal after all
2774 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002775 }
2776
Chris Lattner153587e2005-05-12 07:00:44 +00002777 // Expand the source, then glue it back together for the call. We must expand
2778 // the source in case it is shared (this pass of legalize must traverse it).
2779 SDOperand SrcLo, SrcHi;
2780 ExpandOp(Source, SrcLo, SrcHi);
2781 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2782
Chris Lattner06bbeb62005-05-11 19:02:11 +00002783 SDNode *OutChain = 0;
2784 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2785 DAG.getEntryNode());
2786 const char *FnName = 0;
2787 if (DestTy == MVT::f32)
2788 FnName = "__floatdisf";
2789 else {
2790 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2791 FnName = "__floatdidf";
2792 }
2793
Chris Lattneraac464e2005-01-21 06:05:23 +00002794 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2795
2796 TargetLowering::ArgListTy Args;
2797 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00002798
Chris Lattneraac464e2005-01-21 06:05:23 +00002799 Args.push_back(std::make_pair(Source, ArgTy));
2800
2801 // We don't care about token chains for libcalls. We just use the entry
2802 // node as our input and ignore the output chain. This allows us to place
2803 // calls wherever we need them to satisfy data dependences.
2804 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002805
2806 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00002807 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2808 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002809
Chris Lattnera5bf1032005-05-12 04:49:08 +00002810 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002811 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002812}
Misha Brukman835702a2005-04-21 22:36:52 +00002813
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002814
2815
Chris Lattnerdc750592005-01-07 07:47:09 +00002816/// ExpandOp - Expand the specified SDOperand into its two component pieces
2817/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2818/// LegalizeNodes map is filled in for any results that are not expanded, the
2819/// ExpandedNodes map is filled in for any results that are expanded, and the
2820/// Lo/Hi values are returned.
2821void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2822 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002823 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002824 SDNode *Node = Op.Val;
2825 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2826 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2827 assert(MVT::isInteger(NVT) && NVT < VT &&
2828 "Cannot expand to FP value or to larger int value!");
2829
2830 // If there is more than one use of this, see if we already expanded it.
2831 // There is no use remembering values that only have a single use, as the map
2832 // entries will never be reused.
2833 if (!Node->hasOneUse()) {
2834 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2835 = ExpandedNodes.find(Op);
2836 if (I != ExpandedNodes.end()) {
2837 Lo = I->second.first;
2838 Hi = I->second.second;
2839 return;
2840 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002841 } else {
2842 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002843 }
2844
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002845 // Expanding to multiple registers needs to perform an optimization step, and
2846 // is not careful to avoid operations the target does not support. Make sure
2847 // that all generated operations are legalized in the next iteration.
2848 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002849
Chris Lattnerdc750592005-01-07 07:47:09 +00002850 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002851 case ISD::CopyFromReg:
2852 assert(0 && "CopyFromReg must be legal!");
2853 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00002854 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2855 assert(0 && "Do not know how to expand this operator!");
2856 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002857 case ISD::UNDEF:
2858 Lo = DAG.getNode(ISD::UNDEF, NVT);
2859 Hi = DAG.getNode(ISD::UNDEF, NVT);
2860 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002861 case ISD::Constant: {
2862 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2863 Lo = DAG.getConstant(Cst, NVT);
2864 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2865 break;
2866 }
2867
Chris Lattner32e08b72005-03-28 22:03:13 +00002868 case ISD::BUILD_PAIR:
2869 // Legalize both operands. FIXME: in the future we should handle the case
2870 // where the two elements are not legal.
2871 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2872 Lo = LegalizeOp(Node->getOperand(0));
2873 Hi = LegalizeOp(Node->getOperand(1));
2874 break;
2875
Chris Lattner55e9cde2005-05-11 04:51:16 +00002876 case ISD::CTPOP:
2877 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002878 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2879 DAG.getNode(ISD::CTPOP, NVT, Lo),
2880 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002881 Hi = DAG.getConstant(0, NVT);
2882 break;
2883
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002884 case ISD::CTLZ: {
2885 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002886 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002887 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2888 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002889 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2890 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002891 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2892 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2893
2894 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2895 Hi = DAG.getConstant(0, NVT);
2896 break;
2897 }
2898
2899 case ISD::CTTZ: {
2900 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002901 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002902 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2903 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002904 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2905 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002906 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2907 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2908
2909 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2910 Hi = DAG.getConstant(0, NVT);
2911 break;
2912 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00002913
Chris Lattnerdc750592005-01-07 07:47:09 +00002914 case ISD::LOAD: {
2915 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2916 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002917 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002918
2919 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002920 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002921 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2922 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002923 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002924 //are from the same instruction?
2925 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002926
2927 // Build a factor node to remember that this load is independent of the
2928 // other one.
2929 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2930 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002931
Chris Lattnerdc750592005-01-07 07:47:09 +00002932 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002933 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002934 if (!TLI.isLittleEndian())
2935 std::swap(Lo, Hi);
2936 break;
2937 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00002938 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00002939 case ISD::CALL: {
2940 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2941 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2942
Chris Lattner3d95c142005-01-19 20:24:35 +00002943 bool Changed = false;
2944 std::vector<SDOperand> Ops;
2945 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2946 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2947 Changed |= Ops.back() != Node->getOperand(i);
2948 }
2949
Chris Lattnerdc750592005-01-07 07:47:09 +00002950 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2951 "Can only expand a call once so far, not i64 -> i16!");
2952
2953 std::vector<MVT::ValueType> RetTyVTs;
2954 RetTyVTs.reserve(3);
2955 RetTyVTs.push_back(NVT);
2956 RetTyVTs.push_back(NVT);
2957 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002958 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2959 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00002960 Lo = SDOperand(NC, 0);
2961 Hi = SDOperand(NC, 1);
2962
2963 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002964 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002965 break;
2966 }
2967 case ISD::AND:
2968 case ISD::OR:
2969 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2970 SDOperand LL, LH, RL, RH;
2971 ExpandOp(Node->getOperand(0), LL, LH);
2972 ExpandOp(Node->getOperand(1), RL, RH);
2973 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2974 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2975 break;
2976 }
2977 case ISD::SELECT: {
2978 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002979
2980 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2981 case Expand: assert(0 && "It's impossible to expand bools");
2982 case Legal:
2983 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2984 break;
2985 case Promote:
2986 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2987 break;
2988 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002989 ExpandOp(Node->getOperand(1), LL, LH);
2990 ExpandOp(Node->getOperand(2), RL, RH);
2991 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2992 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2993 break;
2994 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002995 case ISD::SELECT_CC: {
2996 SDOperand TL, TH, FL, FH;
2997 ExpandOp(Node->getOperand(2), TL, TH);
2998 ExpandOp(Node->getOperand(3), FL, FH);
2999 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3000 Node->getOperand(1), TL, FL, Node->getOperand(4));
3001 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3002 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003003 Lo = LegalizeOp(Lo);
3004 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003005 break;
3006 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003007 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003008 SDOperand In;
3009 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3010 case Expand: assert(0 && "expand-expand not implemented yet!");
3011 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3012 case Promote:
3013 In = PromoteOp(Node->getOperand(0));
3014 // Emit the appropriate sign_extend_inreg to get the value we want.
3015 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003016 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003017 break;
3018 }
3019
Chris Lattnerdc750592005-01-07 07:47:09 +00003020 // The low part is just a sign extension of the input (which degenerates to
3021 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003022 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003023
Chris Lattnerdc750592005-01-07 07:47:09 +00003024 // The high part is obtained by SRA'ing all but one of the bits of the lo
3025 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003026 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003027 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3028 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003029 break;
3030 }
Chris Lattner47844892005-04-03 23:41:52 +00003031 case ISD::ZERO_EXTEND: {
3032 SDOperand In;
3033 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3034 case Expand: assert(0 && "expand-expand not implemented yet!");
3035 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3036 case Promote:
3037 In = PromoteOp(Node->getOperand(0));
3038 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003039 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003040 break;
3041 }
3042
Chris Lattnerdc750592005-01-07 07:47:09 +00003043 // The low part is just a zero extension of the input (which degenerates to
3044 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003045 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003046
Chris Lattnerdc750592005-01-07 07:47:09 +00003047 // The high part is just a zero.
3048 Hi = DAG.getConstant(0, NVT);
3049 break;
Chris Lattner47844892005-04-03 23:41:52 +00003050 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003051 // These operators cannot be expanded directly, emit them as calls to
3052 // library functions.
3053 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003054 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003055 SDOperand Op;
3056 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3057 case Expand: assert(0 && "cannot expand FP!");
3058 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3059 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3060 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003061
Chris Lattner941d84a2005-07-30 01:40:57 +00003062 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3063
Chris Lattnerfe68d752005-07-29 00:33:32 +00003064 // Now that the custom expander is done, expand the result, which is still
3065 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003066 if (Op.Val) {
3067 ExpandOp(Op, Lo, Hi);
3068 break;
3069 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003070 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003071
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003072 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003073 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003074 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003075 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003076 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003077
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003078 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003079 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3080 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3081 LegalizeOp(Node->getOperand(0)));
3082 // Now that the custom expander is done, expand the result, which is still
3083 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003084 Op = TLI.LowerOperation(Op, DAG);
3085 if (Op.Val) {
3086 ExpandOp(Op, Lo, Hi);
3087 break;
3088 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003089 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003090
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003091 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003092 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003093 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003094 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003095 break;
3096
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003097 case ISD::SHL:
3098 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003099 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003100 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003101
3102 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003103 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003104 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3105 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003106 break;
3107 }
3108
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003109 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003110 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003111 break;
3112
3113 case ISD::SRA:
3114 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003115 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003116 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003117
3118 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003119 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003120 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3121 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003122 break;
3123 }
3124
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003125 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003126 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003127 break;
3128 case ISD::SRL:
3129 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003130 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003131 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003132
3133 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003134 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003135 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3136 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003137 break;
3138 }
3139
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003140 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003141 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003142 break;
3143
Misha Brukman835702a2005-04-21 22:36:52 +00003144 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003145 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3146 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003147 break;
3148 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003149 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3150 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003151 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003152 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003153 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003154 SDOperand LL, LH, RL, RH;
3155 ExpandOp(Node->getOperand(0), LL, LH);
3156 ExpandOp(Node->getOperand(1), RL, RH);
3157 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3158 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3159 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3160 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3161 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3162 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3163 } else {
3164 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3165 }
3166 break;
3167 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003168 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3169 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3170 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3171 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003172 }
3173
3174 // Remember in a map if the values will be reused later.
3175 if (!Node->hasOneUse()) {
3176 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3177 std::make_pair(Lo, Hi))).second;
3178 assert(isNew && "Value already expanded?!?");
3179 }
3180}
3181
3182
3183// SelectionDAG::Legalize - This is the entry point for the file.
3184//
Chris Lattner4add7e32005-01-23 04:42:50 +00003185void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003186 /// run - This is the main entry point to this class.
3187 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003188 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003189}
3190