blob: 04fef370171effbb31f9b4ff04953125c6234550 [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
Chris Lattnerdc750592005-01-07 07:47:09 +000044 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000045 Legal, // The target natively supports this operation.
46 Promote, // This operation should be executed in a larger type.
47 Expand, // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000048 };
49
Chris Lattnerdc750592005-01-07 07:47:09 +000050 /// ValueTypeActions - This is a bitvector that contains two bits for each
51 /// value type, where the two bits correspond to the LegalizeAction enum.
52 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000053 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000054
Chris Lattnerdc750592005-01-07 07:47:09 +000055 /// LegalizedNodes - For nodes that are of legal width, and that have more
56 /// than one use, this map indicates what regularized operand to use. This
57 /// allows us to avoid legalizing the same thing more than once.
58 std::map<SDOperand, SDOperand> LegalizedNodes;
59
Chris Lattner1f2c9d82005-01-15 05:21:40 +000060 /// PromotedNodes - For nodes that are below legal width, and that have more
61 /// than one use, this map indicates what promoted value to use. This allows
62 /// us to avoid promoting the same thing more than once.
63 std::map<SDOperand, SDOperand> PromotedNodes;
64
Chris Lattnerdc750592005-01-07 07:47:09 +000065 /// ExpandedNodes - For nodes that need to be expanded, and which have more
66 /// than one use, this map indicates which which operands are the expanded
67 /// version of the input. This allows us to avoid expanding the same node
68 /// more than once.
69 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
70
Chris Lattnerea4ca942005-01-07 22:28:47 +000071 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +000072 LegalizedNodes.insert(std::make_pair(From, To));
73 // If someone requests legalization of the new node, return itself.
74 if (From != To)
75 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +000076 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000077 void AddPromotedOperand(SDOperand From, SDOperand To) {
78 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
79 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +000080 // If someone requests legalization of the new node, return itself.
81 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +000082 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000083
Chris Lattnerdc750592005-01-07 07:47:09 +000084public:
85
Chris Lattner4add7e32005-01-23 04:42:50 +000086 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000087
Chris Lattnerdc750592005-01-07 07:47:09 +000088 /// getTypeAction - Return how we should legalize values of this type, either
89 /// it is already legal or we need to expand it into multiple registers of
90 /// smaller integer type, or we need to promote it to a larger type.
91 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +000092 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +000093 }
94
95 /// isTypeLegal - Return true if this type is legal on this target.
96 ///
97 bool isTypeLegal(MVT::ValueType VT) const {
98 return getTypeAction(VT) == Legal;
99 }
100
Chris Lattnerdc750592005-01-07 07:47:09 +0000101 void LegalizeDAG();
102
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000103private:
104
Chris Lattnerdc750592005-01-07 07:47:09 +0000105 SDOperand LegalizeOp(SDOperand O);
106 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000107 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000108
Chris Lattneraac464e2005-01-21 06:05:23 +0000109 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
110 SDOperand &Hi);
111 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
112 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000113
Chris Lattner36e663d2005-12-23 00:16:34 +0000114 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000115 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
116 SDOperand LegalOp,
117 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000118 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
119 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000120 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
121 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000122
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000123 SDOperand ExpandBSWAP(SDOperand Op);
124 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000125 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
126 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000127 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
128 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000129 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
130
Chris Lattnerdc750592005-01-07 07:47:09 +0000131 SDOperand getIntPtrConstant(uint64_t Val) {
132 return DAG.getConstant(Val, TLI.getPointerTy());
133 }
134};
135}
136
Chris Lattner301015a2005-11-19 05:51:46 +0000137static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000138 switch (VecOp) {
139 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-11-19 00:36:38 +0000140 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
141 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
142 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
143 }
144}
Chris Lattnerdc750592005-01-07 07:47:09 +0000145
Chris Lattner4add7e32005-01-23 04:42:50 +0000146SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
147 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
148 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000149 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000150 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000151}
152
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000153/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
154/// not been visited yet and if all of its operands have already been visited.
155static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
156 std::map<SDNode*, unsigned> &Visited) {
157 if (++Visited[N] != N->getNumOperands())
158 return; // Haven't visited all operands yet
159
160 Order.push_back(N);
161
162 if (N->hasOneUse()) { // Tail recurse in common case.
163 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
164 return;
165 }
166
167 // Now that we have N in, add anything that uses it if all of their operands
168 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000169 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
170 ComputeTopDownOrdering(*UI, Order, Visited);
171}
172
Chris Lattner44fe26f2005-07-29 00:11:56 +0000173
Chris Lattnerdc750592005-01-07 07:47:09 +0000174void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000175 // The legalize process is inherently a bottom-up recursive process (users
176 // legalize their uses before themselves). Given infinite stack space, we
177 // could just start legalizing on the root and traverse the whole graph. In
178 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000179 // blocks. To avoid this problem, compute an ordering of the nodes where each
180 // node is only legalized after all of its operands are legalized.
181 std::map<SDNode*, unsigned> Visited;
182 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000183
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000184 // Compute ordering from all of the leaves in the graphs, those (like the
185 // entry node) that have no operands.
186 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
187 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000188 if (I->getNumOperands() == 0) {
189 Visited[I] = 0 - 1U;
190 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000191 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000192 }
193
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000194 assert(Order.size() == Visited.size() &&
195 Order.size() ==
196 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000197 "Error: DAG is cyclic!");
198 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000199
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000200 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
201 SDNode *N = Order[i];
202 switch (getTypeAction(N->getValueType(0))) {
203 default: assert(0 && "Bad type action!");
204 case Legal:
205 LegalizeOp(SDOperand(N, 0));
206 break;
207 case Promote:
208 PromoteOp(SDOperand(N, 0));
209 break;
210 case Expand: {
211 SDOperand X, Y;
212 ExpandOp(SDOperand(N, 0), X, Y);
213 break;
214 }
215 }
216 }
217
218 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000219 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000220 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
221 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000222
223 ExpandedNodes.clear();
224 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000225 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000226
227 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000228 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000229}
230
231SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000232 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000233 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000234 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000235
Chris Lattnerdc750592005-01-07 07:47:09 +0000236 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000237 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000238 if (Node->getNumValues() > 1) {
239 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
240 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000241 case Legal: break; // Nothing to do.
242 case Expand: {
243 SDOperand T1, T2;
244 ExpandOp(Op.getValue(i), T1, T2);
245 assert(LegalizedNodes.count(Op) &&
246 "Expansion didn't add legal operands!");
247 return LegalizedNodes[Op];
248 }
249 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000250 PromoteOp(Op.getValue(i));
251 assert(LegalizedNodes.count(Op) &&
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000252 "Promotion didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000253 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000254 }
255 }
256
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000257 // Note that LegalizeOp may be reentered even from single-use nodes, which
258 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000259 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
260 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000261
Nate Begemane5b86d72005-08-10 20:51:12 +0000262 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000263 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000264 bool isCustom = false;
265
Chris Lattnerdc750592005-01-07 07:47:09 +0000266 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000267 case ISD::FrameIndex:
268 case ISD::EntryToken:
269 case ISD::Register:
270 case ISD::BasicBlock:
271 case ISD::TargetFrameIndex:
272 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000273 case ISD::TargetConstantFP:
274 case ISD::TargetConstantVec:
Chris Lattnereb637512006-01-28 08:31:04 +0000275 case ISD::TargetConstantPool:
276 case ISD::TargetGlobalAddress:
277 case ISD::TargetExternalSymbol:
278 case ISD::VALUETYPE:
279 case ISD::SRCVALUE:
280 case ISD::STRING:
281 case ISD::CONDCODE:
282 // Primitives must all be legal.
283 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
284 "This must be legal!");
285 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000286 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000287 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
288 // If this is a target node, legalize it by legalizing the operands then
289 // passing it through.
290 std::vector<SDOperand> Ops;
291 bool Changed = false;
292 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
293 Ops.push_back(LegalizeOp(Node->getOperand(i)));
294 Changed = Changed || Node->getOperand(i) != Ops.back();
295 }
296 if (Changed)
297 if (Node->getNumValues() == 1)
298 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
299 else {
300 std::vector<MVT::ValueType> VTs(Node->value_begin(),
301 Node->value_end());
302 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
303 }
304
305 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
306 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
307 return Result.getValue(Op.ResNo);
308 }
309 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000310 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
311 assert(0 && "Do not know how to legalize this operator!");
312 abort();
Chris Lattnerdc750592005-01-07 07:47:09 +0000313 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000314 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000315 case ISD::ConstantPool: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000316 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
317 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000318 case TargetLowering::Custom:
319 Tmp1 = TLI.LowerOperation(Op, DAG);
320 if (Tmp1.Val) Result = Tmp1;
321 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000322 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000323 break;
324 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000325 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000326 case ISD::AssertSext:
327 case ISD::AssertZext:
328 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000329 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000330 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000331 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000332 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000333 Result = Node->getOperand(Op.ResNo);
334 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000335 case ISD::CopyFromReg:
336 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000337 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000338 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000339 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000340 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000341 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000342 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000343 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000344 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
345 } else {
346 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
347 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000348 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
349 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000350 // Since CopyFromReg produces two values, make sure to remember that we
351 // legalized both of them.
352 AddLegalizedOperand(Op.getValue(0), Result);
353 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
354 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000355 case ISD::UNDEF: {
356 MVT::ValueType VT = Op.getValueType();
357 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000358 default: assert(0 && "This action is not supported yet!");
359 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000360 if (MVT::isInteger(VT))
361 Result = DAG.getConstant(0, VT);
362 else if (MVT::isFloatingPoint(VT))
363 Result = DAG.getConstantFP(0, VT);
364 else
365 assert(0 && "Unknown value type!");
366 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000367 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000368 break;
369 }
370 break;
371 }
Chris Lattner435b4022005-11-29 06:21:05 +0000372
373 case ISD::LOCATION:
374 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
375 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
376
377 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
378 case TargetLowering::Promote:
379 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000380 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000381 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000382 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
383 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
384
385 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
386 const std::string &FName =
387 cast<StringSDNode>(Node->getOperand(3))->getValue();
388 const std::string &DirName =
389 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyb9966022006-01-17 17:31:53 +0000390 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000391
Jim Laskey9e296be2005-12-21 20:51:37 +0000392 std::vector<SDOperand> Ops;
393 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000394 SDOperand LineOp = Node->getOperand(1);
395 SDOperand ColOp = Node->getOperand(2);
396
397 if (useDEBUG_LOC) {
398 Ops.push_back(LineOp); // line #
399 Ops.push_back(ColOp); // col #
400 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
401 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
402 } else {
403 unsigned Line = dyn_cast<ConstantSDNode>(LineOp)->getValue();
404 unsigned Col = dyn_cast<ConstantSDNode>(ColOp)->getValue();
405 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
406 Ops.push_back(DAG.getConstant(ID, MVT::i32));
407 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
408 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000409 } else {
410 Result = Tmp1; // chain
411 }
Chris Lattner435b4022005-11-29 06:21:05 +0000412 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000413 }
Chris Lattner435b4022005-11-29 06:21:05 +0000414 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000415 if (Tmp1 != Node->getOperand(0) ||
416 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000417 std::vector<SDOperand> Ops;
418 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000419 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
420 Ops.push_back(Node->getOperand(1)); // line # must be legal.
421 Ops.push_back(Node->getOperand(2)); // col # must be legal.
422 } else {
423 // Otherwise promote them.
424 Ops.push_back(PromoteOp(Node->getOperand(1)));
425 Ops.push_back(PromoteOp(Node->getOperand(2)));
426 }
Chris Lattner435b4022005-11-29 06:21:05 +0000427 Ops.push_back(Node->getOperand(3)); // filename must be legal.
428 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000429 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner435b4022005-11-29 06:21:05 +0000430 }
431 break;
432 }
433 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000434
435 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000436 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000437 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000438 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000439 case TargetLowering::Legal:
440 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
441 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
442 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
443 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000444 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000445 break;
446 }
447 break;
448
449 case ISD::DEBUG_LABEL:
450 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
451 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000452 default: assert(0 && "This action is not supported yet!");
453 case TargetLowering::Legal:
454 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
455 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000456 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000457 break;
458 }
459 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000460
Chris Lattnerdc750592005-01-07 07:47:09 +0000461 case ISD::Constant:
462 // We know we don't need to expand constants here, constants only have one
463 // value and we check that it is fine above.
464
465 // FIXME: Maybe we should handle things like targets that don't support full
466 // 32-bit immediates?
467 break;
468 case ISD::ConstantFP: {
469 // Spill FP immediates to the constant pool if the target cannot directly
470 // codegen them. Targets often have some immediate values that can be
471 // efficiently generated into an FP register without a load. We explicitly
472 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000473 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
474
475 // Check to see if this FP immediate is already legal.
476 bool isLegal = false;
477 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
478 E = TLI.legal_fpimm_end(); I != E; ++I)
479 if (CFP->isExactlyValue(*I)) {
480 isLegal = true;
481 break;
482 }
483
Chris Lattner758b0ac2006-01-29 06:26:56 +0000484 // If this is a legal constant, turn it into a TargetConstantFP node.
485 if (isLegal) {
486 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
487 break;
488 }
489
490 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
491 default: assert(0 && "This action is not supported yet!");
492 case TargetLowering::Custom:
493 Tmp3 = TLI.LowerOperation(Result, DAG);
494 if (Tmp3.Val) {
495 Result = Tmp3;
496 break;
497 }
498 // FALLTHROUGH
499 case TargetLowering::Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000500 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000501 bool Extend = false;
502
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000503 // If a FP immediate is precise when represented as a float and if the
504 // target can do an extending load from float to double, we put it into
505 // the constant pool as a float, even if it's is statically typed as a
506 // double.
Chris Lattnerdc750592005-01-07 07:47:09 +0000507 MVT::ValueType VT = CFP->getValueType(0);
508 bool isDouble = VT == MVT::f64;
509 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
510 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000511 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
512 // Only do this if the target has a native EXTLOAD instruction from
513 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000514 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000515 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
516 VT = MVT::f32;
517 Extend = true;
518 }
Misha Brukman835702a2005-04-21 22:36:52 +0000519
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000520 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000521 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000522 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
523 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000524 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000525 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
526 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000527 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000528 }
529 break;
530 }
Chris Lattner2f2927892006-01-29 06:34:16 +0000531 case ISD::ConstantVec:
532 switch (TLI.getOperationAction(ISD::ConstantVec, Node->getValueType(0))) {
533 default: assert(0 && "This action is not supported yet!");
534 case TargetLowering::Custom:
535 Tmp3 = TLI.LowerOperation(Result, DAG);
536 if (Tmp3.Val) {
537 Result = Tmp3;
538 break;
539 }
540 // FALLTHROUGH
541 case TargetLowering::Expand:
542 // We assume that vector constants are not legal, and will be immediately
543 // spilled to the constant pool.
544 //
545 // Create a ConstantPacked, and put it in the constant pool.
546 MVT::ValueType VT = Node->getValueType(0);
547 const Type *OpNTy =
548 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
549 std::vector<Constant*> CV;
550 if (MVT::isFloatingPoint(VT)) {
551 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
552 double V = cast<ConstantFPSDNode>(Node->getOperand(i))->getValue();
553 CV.push_back(ConstantFP::get(OpNTy, V));
554 }
555 } else {
556 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
557 uint64_t V = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
558 CV.push_back(ConstantUInt::get(OpNTy, V));
559 }
560 }
561 Constant *CP = ConstantPacked::get(CV);
562 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
563 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
564 DAG.getSrcValue(NULL));
565 break;
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000566 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000567 break;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000568 case ISD::TokenFactor:
569 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000570 Tmp1 = LegalizeOp(Node->getOperand(0));
571 Tmp2 = LegalizeOp(Node->getOperand(1));
572 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
573 } else if (Node->getNumOperands() == 3) {
574 Tmp1 = LegalizeOp(Node->getOperand(0));
575 Tmp2 = LegalizeOp(Node->getOperand(1));
576 Tmp3 = LegalizeOp(Node->getOperand(2));
577 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000578 } else {
579 std::vector<SDOperand> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000580 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000581 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
582 Ops.push_back(LegalizeOp(Node->getOperand(i)));
583 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000584 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000585 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000586
Chris Lattner2dce7032005-05-12 23:24:06 +0000587 case ISD::CALLSEQ_START:
588 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000589 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +0000590 // Do not try to legalize the target-specific arguments (#1+), except for
591 // an optional flag input.
592 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
593 if (Tmp1 != Node->getOperand(0)) {
594 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
595 Ops[0] = Tmp1;
596 Result = DAG.UpdateNodeOperands(Result, Ops);
597 }
598 } else {
599 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
600 if (Tmp1 != Node->getOperand(0) ||
601 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
602 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
603 Ops[0] = Tmp1;
604 Ops.back() = Tmp2;
605 Result = DAG.UpdateNodeOperands(Result, Ops);
606 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +0000607 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000608 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000609 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +0000610 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
611 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
612 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000613 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +0000614
Chris Lattnerd02b0542006-01-28 10:58:55 +0000615 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +0000616 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +0000617 switch (TLI.getOperationAction(Node->getOpcode(),
618 Node->getValueType(0))) {
619 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +0000620 case TargetLowering::Expand: {
621 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
622 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
623 " not tell us which reg is the stack pointer!");
624 SDOperand Chain = Tmp1.getOperand(0);
625 SDOperand Size = Tmp2.getOperand(1);
626 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
627 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
628 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +0000629 Tmp1 = LegalizeOp(Tmp1);
630 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +0000631 break;
632 }
633 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +0000634 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
635 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000636 Tmp1 = LegalizeOp(Tmp3);
637 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +0000638 }
Chris Lattner59b82f92006-01-15 08:54:32 +0000639 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000640 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +0000641 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000642 }
Chris Lattner59b82f92006-01-15 08:54:32 +0000643 // Since this op produce two values, make sure to remember that we
644 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000645 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
646 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +0000647 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000648 }
Chris Lattner476e67b2006-01-26 22:24:51 +0000649 case ISD::INLINEASM:
650 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
651 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000652 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +0000653 Tmp2 = Tmp3 = SDOperand(0, 0);
654 else
655 Tmp3 = LegalizeOp(Tmp2);
656
657 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
658 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
659 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +0000660 if (Tmp3.Val) Ops.back() = Tmp3;
661 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +0000662 }
663
664 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000665 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +0000666 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
667 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +0000668 case ISD::BR:
669 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000670 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +0000671 break;
672
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000673 case ISD::BRCOND:
674 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000675
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000676 switch (getTypeAction(Node->getOperand(1).getValueType())) {
677 case Expand: assert(0 && "It's impossible to expand bools");
678 case Legal:
679 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
680 break;
681 case Promote:
682 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
683 break;
684 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000685
686 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000687 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000688
689 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
690 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000691 case TargetLowering::Legal: break;
692 case TargetLowering::Custom:
693 Tmp1 = TLI.LowerOperation(Result, DAG);
694 if (Tmp1.Val) Result = Tmp1;
695 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000696 case TargetLowering::Expand:
697 // Expand brcond's setcc into its constituent parts and create a BR_CC
698 // Node.
699 if (Tmp2.getOpcode() == ISD::SETCC) {
700 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
701 Tmp2.getOperand(0), Tmp2.getOperand(1),
702 Node->getOperand(2));
703 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000704 // Make sure the condition is either zero or one. It may have been
705 // promoted from something else.
706 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
707
Nate Begeman371e4952005-08-16 19:49:35 +0000708 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
709 DAG.getCondCode(ISD::SETNE), Tmp2,
710 DAG.getConstant(0, Tmp2.getValueType()),
711 Node->getOperand(2));
712 }
713 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000714 }
715 break;
716 case ISD::BR_CC:
717 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000718 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000719 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
720 Node->getOperand(2), // LHS
721 Node->getOperand(3), // RHS
722 Node->getOperand(1)));
723 // If we get a SETCC back from legalizing the SETCC node we just
724 // created, then use its LHS, RHS, and CC directly in creating a new
725 // node. Otherwise, select between the true and false value based on
726 // comparing the result of the legalized with zero.
727 if (Tmp2.getOpcode() == ISD::SETCC) {
728 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
729 Tmp2.getOperand(0), Tmp2.getOperand(1),
730 Node->getOperand(4));
731 } else {
732 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
733 DAG.getCondCode(ISD::SETNE),
734 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
735 Node->getOperand(4));
736 }
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000737 break;
738 }
739
740 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
741 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
742
Chris Lattnerd02b0542006-01-28 10:58:55 +0000743 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
744 Tmp3, Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000745
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000746 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
747 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000748 case TargetLowering::Legal: break;
749 case TargetLowering::Custom:
750 Tmp4 = TLI.LowerOperation(Result, DAG);
751 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000752 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000753 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000754 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000755 case ISD::BRCONDTWOWAY:
756 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
757 switch (getTypeAction(Node->getOperand(1).getValueType())) {
758 case Expand: assert(0 && "It's impossible to expand bools");
759 case Legal:
760 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
761 break;
762 case Promote:
763 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
764 break;
765 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000766
Chris Lattnerfd986782005-04-09 03:30:19 +0000767 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
768 // pair.
769 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
770 case TargetLowering::Promote:
771 default: assert(0 && "This action is not supported yet!");
772 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000773 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
774 Node->getOperand(3));
Chris Lattnerfd986782005-04-09 03:30:19 +0000775 break;
776 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000777 // If BRTWOWAY_CC is legal for this target, then simply expand this node
778 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
779 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000780 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000781 if (Tmp2.getOpcode() == ISD::SETCC) {
Chris Lattner678da982006-01-29 06:00:45 +0000782 Tmp3 = Tmp2.getOperand(0);
783 Tmp4 = Tmp2.getOperand(1);
784 Tmp2 = Tmp2.getOperand(2);
Nate Begeman371e4952005-08-16 19:49:35 +0000785 } else {
Chris Lattner678da982006-01-29 06:00:45 +0000786 Tmp3 = Tmp2;
787 Tmp4 = DAG.getConstant(0, Tmp2.getValueType());
788 Tmp2 = DAG.getCondCode(ISD::SETNE);
Nate Begeman371e4952005-08-16 19:49:35 +0000789 }
Chris Lattner678da982006-01-29 06:00:45 +0000790 std::vector<SDOperand> Ops;
791 Ops.push_back(Tmp1);
792 Ops.push_back(Tmp2);
793 Ops.push_back(Tmp3);
794 Ops.push_back(Tmp4);
795 Ops.push_back(Node->getOperand(2));
796 Ops.push_back(Node->getOperand(3));
797 Result = DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
Nate Begeman371e4952005-08-16 19:49:35 +0000798 } else {
799 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000800 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000801 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
802 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000803 break;
804 }
805 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000806 case ISD::BRTWOWAY_CC:
807 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000808 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000809 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
810 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
811 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
812 Tmp3 != Node->getOperand(3)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000813 std::vector<SDOperand> Ops;
814 Ops.push_back(Tmp1);
815 Ops.push_back(Node->getOperand(1));
816 Ops.push_back(Tmp2);
817 Ops.push_back(Tmp3);
818 Ops.push_back(Node->getOperand(4));
819 Ops.push_back(Node->getOperand(5));
820 Result = DAG.UpdateNodeOperands(Result, Ops);
Nate Begeman371e4952005-08-16 19:49:35 +0000821 }
822 break;
823 } else {
824 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
825 Node->getOperand(2), // LHS
826 Node->getOperand(3), // RHS
827 Node->getOperand(1)));
828 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
829 // pair.
830 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
831 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000832 case TargetLowering::Legal: {
Nate Begeman371e4952005-08-16 19:49:35 +0000833 // If we get a SETCC back from legalizing the SETCC node we just
834 // created, then use its LHS, RHS, and CC directly in creating a new
835 // node. Otherwise, select between the true and false value based on
836 // comparing the result of the legalized with zero.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000837 std::vector<SDOperand> Ops;
838 Ops.push_back(Tmp1);
Nate Begeman371e4952005-08-16 19:49:35 +0000839 if (Tmp2.getOpcode() == ISD::SETCC) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000840 Ops.push_back(Tmp2.getOperand(2));
841 Ops.push_back(Tmp2.getOperand(0));
842 Ops.push_back(Tmp2.getOperand(1));
Nate Begeman371e4952005-08-16 19:49:35 +0000843 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000844 Ops.push_back(DAG.getCondCode(ISD::SETNE));
845 Ops.push_back(Tmp2);
846 Ops.push_back(DAG.getConstant(0, Tmp2.getValueType()));
Nate Begeman371e4952005-08-16 19:49:35 +0000847 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000848 Ops.push_back(Node->getOperand(4));
849 Ops.push_back(Node->getOperand(5));
850 Result = DAG.UpdateNodeOperands(Result, Ops);
Nate Begeman371e4952005-08-16 19:49:35 +0000851 break;
Chris Lattnerd02b0542006-01-28 10:58:55 +0000852 }
Nate Begeman371e4952005-08-16 19:49:35 +0000853 case TargetLowering::Expand:
854 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
855 Node->getOperand(4));
856 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
857 break;
858 }
859 }
860 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +0000861 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000862 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
863 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000864
Evan Cheng31d15fa2005-12-23 07:29:34 +0000865 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000866 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
867 Tmp2 = Result.getValue(0);
868 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000869
Evan Cheng31d15fa2005-12-23 07:29:34 +0000870 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
871 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000872 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000873 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000874 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000875 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000876 Tmp2 = LegalizeOp(Tmp1);
877 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +0000878 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000879 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +0000880 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000881 // Since loads produce two values, make sure to remember that we
882 // legalized both of them.
883 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
884 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
885 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +0000886 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000887 case ISD::EXTLOAD:
888 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000889 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000890 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
891 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000892
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000893 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000894 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000895 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000896 case TargetLowering::Promote:
897 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000898 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
899 DAG.getValueType(MVT::i8));
900 Tmp1 = Result.getValue(0);
901 Tmp2 = Result.getValue(1);
902 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000903 case TargetLowering::Custom:
904 isCustom = true;
905 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000906 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000907 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
908 Node->getOperand(3));
909 Tmp1 = Result.getValue(0);
910 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000911
912 if (isCustom) {
913 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
914 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000915 Tmp1 = LegalizeOp(Tmp3);
916 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000917 }
918 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000919 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000920 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +0000921 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000922 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
923 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000924 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000925 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
926 Tmp2 = LegalizeOp(Load.getValue(1));
927 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000928 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000929 assert(Node->getOpcode() != ISD::EXTLOAD &&
930 "EXTLOAD should always be supported!");
931 // Turn the unsupported load into an EXTLOAD followed by an explicit
932 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000933 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
934 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000935 SDOperand ValRes;
936 if (Node->getOpcode() == ISD::SEXTLOAD)
937 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000938 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000939 else
940 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000941 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
942 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
943 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000944 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000945 // Since loads produce two values, make sure to remember that we legalized
946 // both of them.
947 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
948 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
949 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000950 }
Nate Begeman5172ce62005-10-19 00:06:56 +0000951 case ISD::EXTRACT_ELEMENT: {
952 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
953 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000954 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +0000955 case Legal:
956 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
957 // 1 -> Hi
958 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
959 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
960 TLI.getShiftAmountTy()));
961 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
962 } else {
963 // 0 -> Lo
964 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
965 Node->getOperand(0));
966 }
Nate Begeman5172ce62005-10-19 00:06:56 +0000967 break;
968 case Expand:
969 // Get both the low and high parts.
970 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
971 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
972 Result = Tmp2; // 1 -> Hi
973 else
974 Result = Tmp1; // 0 -> Lo
975 break;
976 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000977 break;
Nate Begeman5172ce62005-10-19 00:06:56 +0000978 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000979
980 case ISD::CopyToReg:
981 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000982
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000983 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +0000984 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +0000985 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +0000986 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000987 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000988 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +0000989 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000990 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000991 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000992 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000993 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
994 Tmp3);
995 } else {
996 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +0000997 }
998
999 // Since this produces two values, make sure to remember that we legalized
1000 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001001 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001002 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001003 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001004 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001005 break;
1006
1007 case ISD::RET:
1008 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1009 switch (Node->getNumOperands()) {
1010 case 2: // ret val
1011 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1012 case Legal:
1013 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001014 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001015 break;
1016 case Expand: {
1017 SDOperand Lo, Hi;
1018 ExpandOp(Node->getOperand(1), Lo, Hi);
1019 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001020 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001021 }
1022 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001023 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001024 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1025 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001026 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001027 }
1028 break;
1029 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001030 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001031 break;
1032 default: { // ret <values>
1033 std::vector<SDOperand> NewValues;
1034 NewValues.push_back(Tmp1);
1035 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1036 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1037 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001038 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001039 break;
1040 case Expand: {
1041 SDOperand Lo, Hi;
1042 ExpandOp(Node->getOperand(i), Lo, Hi);
1043 NewValues.push_back(Lo);
1044 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001045 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001046 }
1047 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001048 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001049 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001050
1051 if (NewValues.size() == Node->getNumOperands())
1052 Result = DAG.UpdateNodeOperands(Result, NewValues);
1053 else
1054 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001055 break;
1056 }
1057 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001058
Chris Lattner4d1ea712006-01-29 21:02:23 +00001059 if (Result.getOpcode() == ISD::RET) {
1060 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1061 default: assert(0 && "This action is not supported yet!");
1062 case TargetLowering::Legal: break;
1063 case TargetLowering::Custom:
1064 Tmp1 = TLI.LowerOperation(Result, DAG);
1065 if (Tmp1.Val) Result = Tmp1;
1066 break;
1067 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001068 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001069 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001070 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001071 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1072 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1073
Chris Lattnere69daaf2005-01-08 06:25:56 +00001074 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001075 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001076 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001077 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001078 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001079 } else {
1080 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001081 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001082 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001083 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1084 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001085 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001086 }
1087
Chris Lattnerdc750592005-01-07 07:47:09 +00001088 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1089 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001090 Tmp3 = LegalizeOp(Node->getOperand(1));
1091 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1092 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001093
Chris Lattnerd02b0542006-01-28 10:58:55 +00001094 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001095 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1096 default: assert(0 && "This action is not supported yet!");
1097 case TargetLowering::Legal: break;
1098 case TargetLowering::Custom:
1099 Tmp1 = TLI.LowerOperation(Result, DAG);
1100 if (Tmp1.Val) Result = Tmp1;
1101 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001102 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001103 break;
1104 }
1105 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001106 // Truncate the value and store the result.
1107 Tmp3 = PromoteOp(Node->getOperand(1));
1108 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001109 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001110 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001111 break;
1112
Chris Lattnerdc750592005-01-07 07:47:09 +00001113 case Expand:
1114 SDOperand Lo, Hi;
1115 ExpandOp(Node->getOperand(1), Lo, Hi);
1116
1117 if (!TLI.isLittleEndian())
1118 std::swap(Lo, Hi);
1119
Chris Lattner55e9cde2005-05-11 04:51:16 +00001120 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1121 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001122 // If this is a vector type, then we have to calculate the increment as
1123 // the product of the element size in bytes, and the number of elements
1124 // in the high half of the vector.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001125 unsigned IncrementSize;
Nate Begemand37c1312005-11-22 18:16:00 +00001126 if (MVT::Vector == Hi.getValueType()) {
1127 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1128 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1129 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1130 } else {
1131 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1132 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001133 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1134 getIntPtrConstant(IncrementSize));
1135 assert(isTypeLegal(Tmp2.getValueType()) &&
1136 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001137 // FIXME: This sets the srcvalue of both halves to be the same, which is
1138 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001139 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1140 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001141 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1142 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001143 }
1144 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001145 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001146 case ISD::PCMARKER:
1147 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001148 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001149 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001150 case ISD::STACKSAVE:
1151 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001152 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1153 Tmp1 = Result.getValue(0);
1154 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001155
Chris Lattnerb3266452006-01-13 02:50:02 +00001156 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1157 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001158 case TargetLowering::Legal: break;
1159 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001160 Tmp3 = TLI.LowerOperation(Result, DAG);
1161 if (Tmp3.Val) {
1162 Tmp1 = LegalizeOp(Tmp3);
1163 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001164 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001165 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001166 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001167 // Expand to CopyFromReg if the target set
1168 // StackPointerRegisterToSaveRestore.
1169 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001170 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001171 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001172 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001173 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001174 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1175 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001176 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001177 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001178 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001179
1180 // Since stacksave produce two values, make sure to remember that we
1181 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001182 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1183 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1184 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001185
Chris Lattnerb3266452006-01-13 02:50:02 +00001186 case ISD::STACKRESTORE:
1187 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1188 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001189 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001190
1191 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1192 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001193 case TargetLowering::Legal: break;
1194 case TargetLowering::Custom:
1195 Tmp1 = TLI.LowerOperation(Result, DAG);
1196 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001197 break;
1198 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001199 // Expand to CopyToReg if the target set
1200 // StackPointerRegisterToSaveRestore.
1201 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1202 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1203 } else {
1204 Result = Tmp1;
1205 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001206 break;
1207 }
1208 break;
1209
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001210 case ISD::READCYCLECOUNTER:
1211 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001212 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001213
1214 // Since rdcc produce two values, make sure to remember that we legalized
1215 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001216 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001217 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001218 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001219
Evan Cheng31d15fa2005-12-23 07:29:34 +00001220 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1222 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1223
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001224 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1225 "Cannot handle illegal TRUNCSTORE yet!");
1226 Tmp2 = LegalizeOp(Node->getOperand(1));
1227
1228 // The only promote case we handle is TRUNCSTORE:i1 X into
1229 // -> TRUNCSTORE:i8 (and X, 1)
1230 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1231 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1232 TargetLowering::Promote) {
1233 // Promote the bool to a mask then store.
1234 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1235 DAG.getConstant(1, Tmp2.getValueType()));
1236 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1237 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001238
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001239 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1240 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001241 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1242 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001243 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001244
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001245 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1246 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1247 default: assert(0 && "This action is not supported yet!");
1248 case TargetLowering::Legal: break;
1249 case TargetLowering::Custom:
1250 Tmp1 = TLI.LowerOperation(Result, DAG);
1251 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001252 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001253 }
1254 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001255 }
Chris Lattner39c67442005-01-14 22:08:15 +00001256 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001257 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1258 case Expand: assert(0 && "It's impossible to expand bools");
1259 case Legal:
1260 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1261 break;
1262 case Promote:
1263 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1264 break;
1265 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001266 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001267 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001268
Chris Lattnerd02b0542006-01-28 10:58:55 +00001269 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001270
Nate Begeman987121a2005-08-23 04:29:48 +00001271 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001272 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001273 case TargetLowering::Legal: break;
1274 case TargetLowering::Custom: {
1275 Tmp1 = TLI.LowerOperation(Result, DAG);
1276 if (Tmp1.Val) Result = Tmp1;
1277 break;
1278 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001279 case TargetLowering::Expand:
1280 if (Tmp1.getOpcode() == ISD::SETCC) {
1281 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1282 Tmp2, Tmp3,
1283 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1284 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001285 // Make sure the condition is either zero or one. It may have been
1286 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001287 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1288 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1289 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001290 Result = DAG.getSelectCC(Tmp1,
1291 DAG.getConstant(0, Tmp1.getValueType()),
1292 Tmp2, Tmp3, ISD::SETNE);
1293 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001294 break;
1295 case TargetLowering::Promote: {
1296 MVT::ValueType NVT =
1297 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1298 unsigned ExtOp, TruncOp;
1299 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001300 ExtOp = ISD::ANY_EXTEND;
1301 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001302 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001303 ExtOp = ISD::FP_EXTEND;
1304 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001305 }
1306 // Promote each of the values to the new type.
1307 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1308 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1309 // Perform the larger operation, then round down.
1310 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1311 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1312 break;
1313 }
1314 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001315 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001316 case ISD::SELECT_CC:
1317 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1318 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1319
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001320 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001321 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1322 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00001323
1324 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4,
1325 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001326
Chris Lattner5f573412005-08-26 00:23:59 +00001327 // Everything is legal, see if we should expand this op or something.
1328 switch (TLI.getOperationAction(ISD::SELECT_CC,
1329 Node->getOperand(0).getValueType())) {
1330 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001331 case TargetLowering::Legal: break;
1332 case TargetLowering::Custom:
1333 Tmp1 = TLI.LowerOperation(Result, DAG);
1334 if (Tmp1.Val) Result = Tmp1;
Chris Lattner5f573412005-08-26 00:23:59 +00001335 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001336 }
1337 break;
1338 } else {
1339 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1340 Node->getOperand(0), // LHS
1341 Node->getOperand(1), // RHS
1342 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001343 // If we get a SETCC back from legalizing the SETCC node we just
1344 // created, then use its LHS, RHS, and CC directly in creating a new
1345 // node. Otherwise, select between the true and false value based on
1346 // comparing the result of the legalized with zero.
1347 if (Tmp1.getOpcode() == ISD::SETCC) {
1348 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1349 Tmp1.getOperand(0), Tmp1.getOperand(1),
1350 Tmp3, Tmp4, Tmp1.getOperand(2));
1351 } else {
1352 Result = DAG.getSelectCC(Tmp1,
1353 DAG.getConstant(0, Tmp1.getValueType()),
1354 Tmp3, Tmp4, ISD::SETNE);
1355 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001356 }
1357 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001358 case ISD::SETCC:
1359 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1360 case Legal:
1361 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1362 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001363 break;
1364 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001365 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1366 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1367
1368 // If this is an FP compare, the operands have already been extended.
1369 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1370 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001371 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001372
1373 // Otherwise, we have to insert explicit sign or zero extends. Note
1374 // that we could insert sign extends for ALL conditions, but zero extend
1375 // is cheaper on many machines (an AND instead of two shifts), so prefer
1376 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001377 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001378 default: assert(0 && "Unknown integer comparison!");
1379 case ISD::SETEQ:
1380 case ISD::SETNE:
1381 case ISD::SETUGE:
1382 case ISD::SETUGT:
1383 case ISD::SETULE:
1384 case ISD::SETULT:
1385 // ALL of these operations will work if we either sign or zero extend
1386 // the operands (including the unsigned comparisons!). Zero extend is
1387 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001388 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1389 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001390 break;
1391 case ISD::SETGE:
1392 case ISD::SETGT:
1393 case ISD::SETLT:
1394 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001395 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1396 DAG.getValueType(VT));
1397 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1398 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001399 break;
1400 }
Chris Lattner4d978642005-01-15 22:16:26 +00001401 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001402 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001403 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001404 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1405 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1406 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001407 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001408 case ISD::SETEQ:
1409 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001410 if (RHSLo == RHSHi)
1411 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1412 if (RHSCST->isAllOnesValue()) {
1413 // Comparison to -1.
1414 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001415 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001416 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001417 }
1418
Chris Lattnerdc750592005-01-07 07:47:09 +00001419 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1420 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1421 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001422 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001423 break;
1424 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001425 // If this is a comparison of the sign bit, just look at the top part.
1426 // X > -1, x < 0
1427 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001428 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001429 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001430 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001431 (CST->isAllOnesValue()))) { // X > -1
1432 Tmp1 = LHSHi;
1433 Tmp2 = RHSHi;
1434 break;
1435 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001436
Chris Lattnerdc750592005-01-07 07:47:09 +00001437 // FIXME: This generated code sucks.
1438 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001439 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001440 default: assert(0 && "Unknown integer setcc!");
1441 case ISD::SETLT:
1442 case ISD::SETULT: LowCC = ISD::SETULT; break;
1443 case ISD::SETGT:
1444 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1445 case ISD::SETLE:
1446 case ISD::SETULE: LowCC = ISD::SETULE; break;
1447 case ISD::SETGE:
1448 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1449 }
Misha Brukman835702a2005-04-21 22:36:52 +00001450
Chris Lattnerdc750592005-01-07 07:47:09 +00001451 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1452 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1453 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1454
1455 // NOTE: on targets without efficient SELECT of bools, we can always use
1456 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001457 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1458 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1459 Node->getOperand(2));
1460 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001461 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1462 Result, Tmp1, Tmp2));
Chris Lattner2af3ee42005-12-20 00:53:54 +00001463 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman987121a2005-08-23 04:29:48 +00001464 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001465 }
1466 }
Nate Begeman987121a2005-08-23 04:29:48 +00001467
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001468 switch (TLI.getOperationAction(ISD::SETCC,
1469 Node->getOperand(0).getValueType())) {
1470 default: assert(0 && "Cannot handle this action for SETCC yet!");
1471 case TargetLowering::Custom:
1472 isCustom = true;
1473 // FALLTHROUGH.
1474 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001475 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001476 if (isCustom) {
1477 Tmp3 = TLI.LowerOperation(Result, DAG);
1478 if (Tmp3.Val) Result = Tmp3;
1479 }
Nate Begeman987121a2005-08-23 04:29:48 +00001480 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001481 case TargetLowering::Promote: {
1482 // First step, figure out the appropriate operation to use.
1483 // Allow SETCC to not be supported for all legal data types
1484 // Mostly this targets FP
1485 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1486 MVT::ValueType OldVT = NewInTy;
1487
1488 // Scan for the appropriate larger type to use.
1489 while (1) {
1490 NewInTy = (MVT::ValueType)(NewInTy+1);
1491
1492 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1493 "Fell off of the edge of the integer world");
1494 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1495 "Fell off of the edge of the floating point world");
1496
1497 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001498 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001499 break;
1500 }
1501 if (MVT::isInteger(NewInTy))
1502 assert(0 && "Cannot promote Legal Integer SETCC yet");
1503 else {
1504 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1505 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1506 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001507 Tmp1 = LegalizeOp(Tmp1);
1508 Tmp2 = LegalizeOp(Tmp2);
1509 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001510 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001511 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001512 }
Nate Begeman987121a2005-08-23 04:29:48 +00001513 case TargetLowering::Expand:
1514 // Expand a setcc node into a select_cc of the same condition, lhs, and
1515 // rhs that selects between const 1 (true) and const 0 (false).
1516 MVT::ValueType VT = Node->getValueType(0);
1517 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1518 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1519 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001520 break;
1521 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001522 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001523 case ISD::MEMSET:
1524 case ISD::MEMCPY:
1525 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001526 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001527 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1528
1529 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1530 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1531 case Expand: assert(0 && "Cannot expand a byte!");
1532 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001533 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001534 break;
1535 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001536 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001537 break;
1538 }
1539 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001540 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001541 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001542
1543 SDOperand Tmp4;
1544 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001545 case Expand: {
1546 // Length is too big, just take the lo-part of the length.
1547 SDOperand HiPart;
1548 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1549 break;
1550 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001551 case Legal:
1552 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001553 break;
1554 case Promote:
1555 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001556 break;
1557 }
1558
1559 SDOperand Tmp5;
1560 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1561 case Expand: assert(0 && "Cannot expand this yet!");
1562 case Legal:
1563 Tmp5 = LegalizeOp(Node->getOperand(4));
1564 break;
1565 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001566 Tmp5 = PromoteOp(Node->getOperand(4));
1567 break;
1568 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001569
1570 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1571 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001572 case TargetLowering::Custom:
1573 isCustom = true;
1574 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001575 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001576 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001577 if (isCustom) {
1578 Tmp1 = TLI.LowerOperation(Result, DAG);
1579 if (Tmp1.Val) Result = Tmp1;
1580 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001581 break;
1582 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001583 // Otherwise, the target does not support this operation. Lower the
1584 // operation to an explicit libcall as appropriate.
1585 MVT::ValueType IntPtr = TLI.getPointerTy();
1586 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1587 std::vector<std::pair<SDOperand, const Type*> > Args;
1588
Reid Spencer6dced922005-01-12 14:53:45 +00001589 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001590 if (Node->getOpcode() == ISD::MEMSET) {
1591 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1592 // Extend the ubyte argument to be an int value for the call.
1593 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1594 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1595 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1596
1597 FnName = "memset";
1598 } else if (Node->getOpcode() == ISD::MEMCPY ||
1599 Node->getOpcode() == ISD::MEMMOVE) {
1600 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1601 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1602 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1603 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1604 } else {
1605 assert(0 && "Unknown op!");
1606 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001607
Chris Lattner85d70c62005-01-11 05:57:22 +00001608 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001609 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001610 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001611 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001612 break;
1613 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001614 }
1615 break;
1616 }
Chris Lattner5385db52005-05-09 20:23:03 +00001617
1618 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001619 Tmp1 = LegalizeOp(Node->getOperand(0));
1620 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001621 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001622
Chris Lattner5385db52005-05-09 20:23:03 +00001623 // Since these produce two values, make sure to remember that we legalized
1624 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001625 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner5385db52005-05-09 20:23:03 +00001626 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001627 return Result;
Chris Lattner5385db52005-05-09 20:23:03 +00001628 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001629 Tmp1 = LegalizeOp(Node->getOperand(0));
1630 Tmp2 = LegalizeOp(Node->getOperand(1));
1631 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001632 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner5385db52005-05-09 20:23:03 +00001633 break;
1634
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001635 case ISD::READIO:
1636 Tmp1 = LegalizeOp(Node->getOperand(0));
1637 Tmp2 = LegalizeOp(Node->getOperand(1));
1638
1639 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1640 case TargetLowering::Custom:
1641 default: assert(0 && "This action not implemented for this operation!");
1642 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001643 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001644 break;
1645 case TargetLowering::Expand:
1646 // Replace this with a load from memory.
1647 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1648 Node->getOperand(1), DAG.getSrcValue(NULL));
1649 Result = LegalizeOp(Result);
1650 break;
1651 }
1652
1653 // Since these produce two values, make sure to remember that we legalized
1654 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001655 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001656 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1657 return Result.getValue(Op.ResNo);
1658
1659 case ISD::WRITEIO:
1660 Tmp1 = LegalizeOp(Node->getOperand(0));
1661 Tmp2 = LegalizeOp(Node->getOperand(1));
1662 Tmp3 = LegalizeOp(Node->getOperand(2));
1663
1664 switch (TLI.getOperationAction(Node->getOpcode(),
1665 Node->getOperand(1).getValueType())) {
1666 case TargetLowering::Custom:
1667 default: assert(0 && "This action not implemented for this operation!");
1668 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001669 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001670 break;
1671 case TargetLowering::Expand:
1672 // Replace this with a store to memory.
1673 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1674 Node->getOperand(1), Node->getOperand(2),
1675 DAG.getSrcValue(NULL));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001676 break;
1677 }
1678 break;
1679
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001680 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001681 case ISD::SUB_PARTS:
1682 case ISD::SHL_PARTS:
1683 case ISD::SRA_PARTS:
1684 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001685 std::vector<SDOperand> Ops;
1686 bool Changed = false;
1687 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1688 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1689 Changed |= Ops.back() != Node->getOperand(i);
1690 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001691 if (Changed)
1692 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001693
Evan Cheng870e4f82006-01-09 18:31:59 +00001694 switch (TLI.getOperationAction(Node->getOpcode(),
1695 Node->getValueType(0))) {
1696 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001697 case TargetLowering::Legal: break;
1698 case TargetLowering::Custom:
1699 Tmp1 = TLI.LowerOperation(Result, DAG);
1700 if (Tmp1.Val) {
1701 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001702 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001703 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001704 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1705 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001706 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001707 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001708 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001709 return RetVal;
1710 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001711 break;
1712 }
1713
Chris Lattner13fe99c2005-04-02 05:00:07 +00001714 // Since these produce multiple values, make sure to remember that we
1715 // legalized all of them.
1716 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1717 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1718 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001719 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001720
1721 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001722 case ISD::ADD:
1723 case ISD::SUB:
1724 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001725 case ISD::MULHS:
1726 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001727 case ISD::UDIV:
1728 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001729 case ISD::AND:
1730 case ISD::OR:
1731 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001732 case ISD::SHL:
1733 case ISD::SRL:
1734 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001735 case ISD::FADD:
1736 case ISD::FSUB:
1737 case ISD::FMUL:
1738 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001739 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001740 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1741 case Expand: assert(0 && "Not possible");
1742 case Legal:
1743 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1744 break;
1745 case Promote:
1746 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1747 break;
1748 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001749
1750 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001751
Andrew Lenharth72594262005-12-24 23:42:32 +00001752 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001753 default: assert(0 && "Operation not supported");
1754 case TargetLowering::Legal: break;
1755 case TargetLowering::Custom:
1756 Tmp1 = TLI.LowerOperation(Result, DAG);
1757 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001758 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001759 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001760 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001761
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001762 case ISD::BUILD_PAIR: {
1763 MVT::ValueType PairTy = Node->getValueType(0);
1764 // TODO: handle the case where the Lo and Hi operands are not of legal type
1765 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1766 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1767 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001768 case TargetLowering::Promote:
1769 case TargetLowering::Custom:
1770 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001771 case TargetLowering::Legal:
1772 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1773 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1774 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001775 case TargetLowering::Expand:
1776 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1777 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1778 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1779 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1780 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001781 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001782 break;
1783 }
1784 break;
1785 }
1786
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001787 case ISD::UREM:
1788 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001789 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001790 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1791 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001792
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001793 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001794 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1795 case TargetLowering::Custom:
1796 isCustom = true;
1797 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001798 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001799 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001800 if (isCustom) {
1801 Tmp1 = TLI.LowerOperation(Result, DAG);
1802 if (Tmp1.Val) Result = Tmp1;
1803 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001804 break;
Chris Lattner81914422005-08-03 20:31:37 +00001805 case TargetLowering::Expand:
1806 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001807 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00001808 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001809 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00001810 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1811 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1812 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1813 } else {
1814 // Floating point mod -> fmod libcall.
1815 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1816 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00001817 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001818 }
1819 break;
1820 }
1821 break;
Nate Begemane74795c2006-01-25 18:21:52 +00001822 case ISD::VAARG: {
1823 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1824 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1825
Chris Lattner364b89a2006-01-28 07:42:08 +00001826 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00001827 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1828 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001829 case TargetLowering::Custom:
1830 isCustom = true;
1831 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001832 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001833 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1834 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001835 Tmp1 = Result.getValue(1);
1836
1837 if (isCustom) {
1838 Tmp2 = TLI.LowerOperation(Result, DAG);
1839 if (Tmp2.Val) {
1840 Result = LegalizeOp(Tmp2);
1841 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1842 }
1843 }
Nate Begemane74795c2006-01-25 18:21:52 +00001844 break;
1845 case TargetLowering::Expand: {
1846 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1847 Node->getOperand(2));
1848 // Increment the pointer, VAList, to the next vaarg
1849 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1850 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1851 TLI.getPointerTy()));
1852 // Store the incremented VAList to the legalized pointer
1853 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1854 Node->getOperand(2));
1855 // Load the actual argument out of the pointer VAList
1856 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001857 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00001858 Result = LegalizeOp(Result);
1859 break;
1860 }
1861 }
1862 // Since VAARG produces two values, make sure to remember that we
1863 // legalized both of them.
1864 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001865 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1866 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00001867 }
1868
1869 case ISD::VACOPY:
1870 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1871 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1872 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1873
1874 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1875 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001876 case TargetLowering::Custom:
1877 isCustom = true;
1878 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001879 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001880 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1881 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001882 if (isCustom) {
1883 Tmp1 = TLI.LowerOperation(Result, DAG);
1884 if (Tmp1.Val) Result = Tmp1;
1885 }
Nate Begemane74795c2006-01-25 18:21:52 +00001886 break;
1887 case TargetLowering::Expand:
1888 // This defaults to loading a pointer from the input and storing it to the
1889 // output, returning the chain.
1890 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
1891 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
1892 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00001893 break;
1894 }
1895 break;
1896
1897 case ISD::VAEND:
1898 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1899 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1900
1901 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
1902 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001903 case TargetLowering::Custom:
1904 isCustom = true;
1905 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001906 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001907 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001908 if (isCustom) {
1909 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
1910 if (Tmp1.Val) Result = Tmp1;
1911 }
Nate Begemane74795c2006-01-25 18:21:52 +00001912 break;
1913 case TargetLowering::Expand:
1914 Result = Tmp1; // Default to a no-op, return the chain
1915 break;
1916 }
1917 break;
1918
1919 case ISD::VASTART:
1920 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1921 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1922
Chris Lattnerd02b0542006-01-28 10:58:55 +00001923 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1924
Nate Begemane74795c2006-01-25 18:21:52 +00001925 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
1926 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001927 case TargetLowering::Legal: break;
1928 case TargetLowering::Custom:
1929 Tmp1 = TLI.LowerOperation(Result, DAG);
1930 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00001931 break;
1932 }
1933 break;
1934
Nate Begeman1b8121b2006-01-11 21:21:00 +00001935 case ISD::ROTL:
1936 case ISD::ROTR:
1937 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1938 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001939
1940 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
1941 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001942 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00001943 break;
1944
Nate Begeman2fba8a32006-01-14 03:14:10 +00001945 case ISD::BSWAP:
1946 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1947 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001948 case TargetLowering::Custom:
1949 assert(0 && "Cannot custom legalize this yet!");
1950 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001951 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001952 break;
1953 case TargetLowering::Promote: {
1954 MVT::ValueType OVT = Tmp1.getValueType();
1955 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1956 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00001957
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001958 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1959 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
1960 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
1961 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
1962 break;
1963 }
1964 case TargetLowering::Expand:
1965 Result = ExpandBSWAP(Tmp1);
1966 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00001967 }
1968 break;
1969
Andrew Lenharth5e177822005-05-03 17:19:30 +00001970 case ISD::CTPOP:
1971 case ISD::CTTZ:
1972 case ISD::CTLZ:
1973 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1974 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001975 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00001976 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001977 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00001978 break;
1979 case TargetLowering::Promote: {
1980 MVT::ValueType OVT = Tmp1.getValueType();
1981 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001982
1983 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001984 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1985 // Perform the larger operation, then subtract if needed.
1986 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001987 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00001988 case ISD::CTPOP:
1989 Result = Tmp1;
1990 break;
1991 case ISD::CTTZ:
1992 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001993 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1994 DAG.getConstant(getSizeInBits(NVT), NVT),
1995 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001996 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001997 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1998 break;
1999 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002000 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002001 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2002 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002003 getSizeInBits(OVT), NVT));
2004 break;
2005 }
2006 break;
2007 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002008 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002009 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002010 break;
2011 }
2012 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002013
Chris Lattner13fe99c2005-04-02 05:00:07 +00002014 // Unary operators
2015 case ISD::FABS:
2016 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002017 case ISD::FSQRT:
2018 case ISD::FSIN:
2019 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002020 Tmp1 = LegalizeOp(Node->getOperand(0));
2021 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002022 case TargetLowering::Promote:
2023 case TargetLowering::Custom:
2024 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002025 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002026 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner13fe99c2005-04-02 05:00:07 +00002027 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002028 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002029 switch (Node->getOpcode()) {
2030 default: assert(0 && "Unreachable!");
2031 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002032 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2033 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002034 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002035 break;
Chris Lattner80026402005-04-30 04:43:14 +00002036 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002037 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2038 MVT::ValueType VT = Node->getValueType(0);
2039 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002040 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002041 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2042 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002043 break;
2044 }
2045 case ISD::FSQRT:
2046 case ISD::FSIN:
2047 case ISD::FCOS: {
2048 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002049 const char *FnName = 0;
2050 switch(Node->getOpcode()) {
2051 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2052 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2053 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2054 default: assert(0 && "Unreachable!");
2055 }
Nate Begeman77558da2005-08-04 21:43:28 +00002056 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002057 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002058 break;
2059 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002060 }
2061 break;
2062 }
2063 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002064
2065 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002066 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002067 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002068 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002069 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2070 Node->getOperand(0).getValueType())) {
2071 default: assert(0 && "Unknown operation action!");
2072 case TargetLowering::Expand:
2073 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2074 break;
2075 case TargetLowering::Legal:
2076 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002077 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002078 break;
2079 }
2080 }
2081 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002082 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002083 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002084 case ISD::UINT_TO_FP: {
2085 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002086 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2087 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002088 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002089 Node->getOperand(0).getValueType())) {
2090 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002091 case TargetLowering::Custom:
2092 isCustom = true;
2093 // FALLTHROUGH
2094 case TargetLowering::Legal:
2095 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002096 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002097 if (isCustom) {
2098 Tmp1 = TLI.LowerOperation(Result, DAG);
2099 if (Tmp1.Val) Result = Tmp1;
2100 }
2101 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002102 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002103 Result = ExpandLegalINT_TO_FP(isSigned,
2104 LegalizeOp(Node->getOperand(0)),
2105 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002106 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002107 case TargetLowering::Promote:
2108 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2109 Node->getValueType(0),
2110 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002111 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002112 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002113 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002114 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002115 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2116 Node->getValueType(0), Node->getOperand(0));
2117 break;
2118 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002119 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002120 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002121 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2122 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002123 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002124 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2125 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002126 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002127 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2128 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002129 break;
2130 }
2131 break;
2132 }
2133 case ISD::TRUNCATE:
2134 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2135 case Legal:
2136 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002137 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002138 break;
2139 case Expand:
2140 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2141
2142 // Since the result is legal, we should just be able to truncate the low
2143 // part of the source.
2144 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2145 break;
2146 case Promote:
2147 Result = PromoteOp(Node->getOperand(0));
2148 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2149 break;
2150 }
2151 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002152
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002153 case ISD::FP_TO_SINT:
2154 case ISD::FP_TO_UINT:
2155 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2156 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002157 Tmp1 = LegalizeOp(Node->getOperand(0));
2158
Chris Lattner44fe26f2005-07-29 00:11:56 +00002159 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2160 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002161 case TargetLowering::Custom:
2162 isCustom = true;
2163 // FALLTHROUGH
2164 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002165 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002166 if (isCustom) {
2167 Tmp1 = TLI.LowerOperation(Result, DAG);
2168 if (Tmp1.Val) Result = Tmp1;
2169 }
2170 break;
2171 case TargetLowering::Promote:
2172 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2173 Node->getOpcode() == ISD::FP_TO_SINT);
2174 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002175 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002176 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2177 SDOperand True, False;
2178 MVT::ValueType VT = Node->getOperand(0).getValueType();
2179 MVT::ValueType NVT = Node->getValueType(0);
2180 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2181 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2182 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2183 Node->getOperand(0), Tmp2, ISD::SETLT);
2184 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2185 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002186 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002187 Tmp2));
2188 False = DAG.getNode(ISD::XOR, NVT, False,
2189 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002190 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2191 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002192 } else {
2193 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2194 }
2195 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002196 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002197 break;
2198 case Expand:
2199 assert(0 && "Shouldn't need to expand other operators here!");
2200 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002201 Tmp1 = PromoteOp(Node->getOperand(0));
2202 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2203 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002204 break;
2205 }
2206 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002207
Chris Lattner7753f172005-09-02 00:18:10 +00002208 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002209 case ISD::ZERO_EXTEND:
2210 case ISD::SIGN_EXTEND:
2211 case ISD::FP_EXTEND:
2212 case ISD::FP_ROUND:
2213 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002214 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002215 case Legal:
2216 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002217 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002218 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002219 case Promote:
2220 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002221 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002222 Tmp1 = PromoteOp(Node->getOperand(0));
2223 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002224 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002225 case ISD::ZERO_EXTEND:
2226 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002227 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002228 Result = DAG.getZeroExtendInReg(Result,
2229 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002230 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002231 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002232 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002233 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002234 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002235 Result,
2236 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002237 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002238 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002239 Result = PromoteOp(Node->getOperand(0));
2240 if (Result.getValueType() != Op.getValueType())
2241 // Dynamically dead while we have only 2 FP types.
2242 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2243 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002244 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002245 Result = PromoteOp(Node->getOperand(0));
2246 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2247 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002248 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002249 }
2250 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002251 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002252 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002253 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002254 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002255
2256 // If this operation is not supported, convert it to a shl/shr or load/store
2257 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002258 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2259 default: assert(0 && "This action not supported for this op yet!");
2260 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002261 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002262 break;
2263 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002264 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002265 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002266 // NOTE: we could fall back on load/store here too for targets without
2267 // SAR. However, it is doubtful that any exist.
2268 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2269 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002270 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002271 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2272 Node->getOperand(0), ShiftCst);
2273 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2274 Result, ShiftCst);
2275 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2276 // The only way we can lower this is to turn it into a STORETRUNC,
2277 // EXTLOAD pair, targetting a temporary location (a stack slot).
2278
2279 // NOTE: there is a choice here between constantly creating new stack
2280 // slots and always reusing the same one. We currently always create
2281 // new ones, as reuse may inhibit scheduling.
2282 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2283 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2284 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2285 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002286 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002287 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2288 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2289 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002290 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002291 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002292 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2293 Result, StackSlot, DAG.getSrcValue(NULL),
2294 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002295 } else {
2296 assert(0 && "Unknown op");
2297 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002298 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002299 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002300 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002301 }
Chris Lattner99222f72005-01-15 07:15:18 +00002302 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002303
2304 // Make sure that the generated code is itself legal.
2305 if (Result != Op)
2306 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002307
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002308 // Note that LegalizeOp may be reentered even from single-use nodes, which
2309 // means that we always must cache transformed nodes.
2310 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002311 return Result;
2312}
2313
Chris Lattner4d978642005-01-15 22:16:26 +00002314/// PromoteOp - Given an operation that produces a value in an invalid type,
2315/// promote it to compute the value into a larger type. The produced value will
2316/// have the correct bits for the low portion of the register, but no guarantee
2317/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002318SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2319 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002320 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002321 assert(getTypeAction(VT) == Promote &&
2322 "Caller should expand or legalize operands that are not promotable!");
2323 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2324 "Cannot promote to smaller type!");
2325
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002326 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002327 SDOperand Result;
2328 SDNode *Node = Op.Val;
2329
Chris Lattner1a570f12005-09-02 20:32:45 +00002330 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2331 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002332
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002333 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002334 case ISD::CopyFromReg:
2335 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002336 default:
2337 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2338 assert(0 && "Do not know how to promote this operator!");
2339 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002340 case ISD::UNDEF:
2341 Result = DAG.getNode(ISD::UNDEF, NVT);
2342 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002343 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002344 if (VT != MVT::i1)
2345 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2346 else
2347 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002348 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2349 break;
2350 case ISD::ConstantFP:
2351 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2352 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2353 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002354
Chris Lattner2cb338d2005-01-18 02:59:52 +00002355 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002356 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002357 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2358 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002359 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002360
2361 case ISD::TRUNCATE:
2362 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2363 case Legal:
2364 Result = LegalizeOp(Node->getOperand(0));
2365 assert(Result.getValueType() >= NVT &&
2366 "This truncation doesn't make sense!");
2367 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2368 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2369 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002370 case Promote:
2371 // The truncation is not required, because we don't guarantee anything
2372 // about high bits anyway.
2373 Result = PromoteOp(Node->getOperand(0));
2374 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002375 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002376 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2377 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002378 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002379 }
2380 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002381 case ISD::SIGN_EXTEND:
2382 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002383 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002384 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2385 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2386 case Legal:
2387 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002388 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002389 break;
2390 case Promote:
2391 // Promote the reg if it's smaller.
2392 Result = PromoteOp(Node->getOperand(0));
2393 // The high bits are not guaranteed to be anything. Insert an extend.
2394 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002395 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002396 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002397 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002398 Result = DAG.getZeroExtendInReg(Result,
2399 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002400 break;
2401 }
2402 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002403 case ISD::BIT_CONVERT:
2404 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2405 Result = PromoteOp(Result);
2406 break;
2407
Chris Lattner4d978642005-01-15 22:16:26 +00002408 case ISD::FP_EXTEND:
2409 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2410 case ISD::FP_ROUND:
2411 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2412 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2413 case Promote: assert(0 && "Unreachable with 2 FP types!");
2414 case Legal:
2415 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002416 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002417 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002418 break;
2419 }
2420 break;
2421
2422 case ISD::SINT_TO_FP:
2423 case ISD::UINT_TO_FP:
2424 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2425 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002426 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002427 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002428 break;
2429
2430 case Promote:
2431 Result = PromoteOp(Node->getOperand(0));
2432 if (Node->getOpcode() == ISD::SINT_TO_FP)
2433 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002434 Result,
2435 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002436 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002437 Result = DAG.getZeroExtendInReg(Result,
2438 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002439 // No extra round required here.
2440 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002441 break;
2442 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002443 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2444 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002445 // Round if we cannot tolerate excess precision.
2446 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002447 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2448 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002449 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002450 }
Chris Lattner4d978642005-01-15 22:16:26 +00002451 break;
2452
Chris Lattner268d4572005-12-09 17:32:47 +00002453 case ISD::SIGN_EXTEND_INREG:
2454 Result = PromoteOp(Node->getOperand(0));
2455 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2456 Node->getOperand(1));
2457 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002458 case ISD::FP_TO_SINT:
2459 case ISD::FP_TO_UINT:
2460 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2461 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002462 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002463 break;
2464 case Promote:
2465 // The input result is prerounded, so we don't have to do anything
2466 // special.
2467 Tmp1 = PromoteOp(Node->getOperand(0));
2468 break;
2469 case Expand:
2470 assert(0 && "not implemented");
2471 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002472 // If we're promoting a UINT to a larger size, check to see if the new node
2473 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2474 // we can use that instead. This allows us to generate better code for
2475 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2476 // legal, such as PowerPC.
2477 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002478 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002479 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2480 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002481 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2482 } else {
2483 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2484 }
Chris Lattner4d978642005-01-15 22:16:26 +00002485 break;
2486
Chris Lattner13fe99c2005-04-02 05:00:07 +00002487 case ISD::FABS:
2488 case ISD::FNEG:
2489 Tmp1 = PromoteOp(Node->getOperand(0));
2490 assert(Tmp1.getValueType() == NVT);
2491 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2492 // NOTE: we do not have to do any extra rounding here for
2493 // NoExcessFPPrecision, because we know the input will have the appropriate
2494 // precision, and these operations don't modify precision at all.
2495 break;
2496
Chris Lattner9d6fa982005-04-28 21:44:33 +00002497 case ISD::FSQRT:
2498 case ISD::FSIN:
2499 case ISD::FCOS:
2500 Tmp1 = PromoteOp(Node->getOperand(0));
2501 assert(Tmp1.getValueType() == NVT);
2502 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002503 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002504 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2505 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002506 break;
2507
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002508 case ISD::AND:
2509 case ISD::OR:
2510 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002511 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002512 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002513 case ISD::MUL:
2514 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002515 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002516 // that too is okay if they are integer operations.
2517 Tmp1 = PromoteOp(Node->getOperand(0));
2518 Tmp2 = PromoteOp(Node->getOperand(1));
2519 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2520 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002521 break;
2522 case ISD::FADD:
2523 case ISD::FSUB:
2524 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002525 Tmp1 = PromoteOp(Node->getOperand(0));
2526 Tmp2 = PromoteOp(Node->getOperand(1));
2527 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2528 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2529
2530 // Floating point operations will give excess precision that we may not be
2531 // able to tolerate. If we DO allow excess precision, just leave it,
2532 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002533 // FIXME: Why would we need to round FP ops more than integer ones?
2534 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002535 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002536 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2537 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002538 break;
2539
Chris Lattner4d978642005-01-15 22:16:26 +00002540 case ISD::SDIV:
2541 case ISD::SREM:
2542 // These operators require that their input be sign extended.
2543 Tmp1 = PromoteOp(Node->getOperand(0));
2544 Tmp2 = PromoteOp(Node->getOperand(1));
2545 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002546 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2547 DAG.getValueType(VT));
2548 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2549 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002550 }
2551 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2552
2553 // Perform FP_ROUND: this is probably overly pessimistic.
2554 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002555 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2556 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002557 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002558 case ISD::FDIV:
2559 case ISD::FREM:
2560 // These operators require that their input be fp extended.
2561 Tmp1 = PromoteOp(Node->getOperand(0));
2562 Tmp2 = PromoteOp(Node->getOperand(1));
2563 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2564
2565 // Perform FP_ROUND: this is probably overly pessimistic.
2566 if (NoExcessFPPrecision)
2567 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2568 DAG.getValueType(VT));
2569 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002570
2571 case ISD::UDIV:
2572 case ISD::UREM:
2573 // These operators require that their input be zero extended.
2574 Tmp1 = PromoteOp(Node->getOperand(0));
2575 Tmp2 = PromoteOp(Node->getOperand(1));
2576 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002577 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2578 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002579 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2580 break;
2581
2582 case ISD::SHL:
2583 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002584 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002585 break;
2586 case ISD::SRA:
2587 // The input value must be properly sign extended.
2588 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002589 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2590 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002591 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002592 break;
2593 case ISD::SRL:
2594 // The input value must be properly zero extended.
2595 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002596 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002597 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002598 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002599
2600 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002601 Tmp1 = Node->getOperand(0); // Get the chain.
2602 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002603 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2604 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2605 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2606 } else {
2607 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2608 Node->getOperand(2));
2609 // Increment the pointer, VAList, to the next vaarg
2610 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2611 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2612 TLI.getPointerTy()));
2613 // Store the incremented VAList to the legalized pointer
2614 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2615 Node->getOperand(2));
2616 // Load the actual argument out of the pointer VAList
2617 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2618 DAG.getSrcValue(0), VT);
2619 }
2620 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002621 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002622 break;
2623
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002624 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002625 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2626 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002627 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002628 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002629 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002630 case ISD::SEXTLOAD:
2631 case ISD::ZEXTLOAD:
2632 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002633 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2634 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002635 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002636 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002637 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002638 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002639 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002640 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2641 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002642 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002643 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002644 case ISD::SELECT_CC:
2645 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2646 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2647 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002648 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002649 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002650 case ISD::BSWAP:
2651 Tmp1 = Node->getOperand(0);
2652 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2653 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2654 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2655 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2656 TLI.getShiftAmountTy()));
2657 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002658 case ISD::CTPOP:
2659 case ISD::CTTZ:
2660 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002661 // Zero extend the argument
2662 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002663 // Perform the larger operation, then subtract if needed.
2664 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002665 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002666 case ISD::CTPOP:
2667 Result = Tmp1;
2668 break;
2669 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002670 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002671 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002672 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002673 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002674 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002675 break;
2676 case ISD::CTLZ:
2677 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002678 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2679 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002680 getSizeInBits(VT), NVT));
2681 break;
2682 }
2683 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002684 }
2685
2686 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002687
2688 // Make sure the result is itself legal.
2689 Result = LegalizeOp(Result);
2690
2691 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002692 AddPromotedOperand(Op, Result);
2693 return Result;
2694}
Chris Lattnerdc750592005-01-07 07:47:09 +00002695
Chris Lattner36e663d2005-12-23 00:16:34 +00002696/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002697/// The resultant code need not be legal. Note that SrcOp is the input operand
2698/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00002699SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2700 SDOperand SrcOp) {
2701 // Create the stack frame object.
2702 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2703 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002704 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner36e663d2005-12-23 00:16:34 +00002705 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2706
2707 // Emit a store to the stack slot.
2708 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00002709 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00002710 // Result is a load from the stack slot.
2711 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2712}
2713
Chris Lattner4157c412005-04-02 04:00:59 +00002714void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2715 SDOperand Op, SDOperand Amt,
2716 SDOperand &Lo, SDOperand &Hi) {
2717 // Expand the subcomponents.
2718 SDOperand LHSL, LHSH;
2719 ExpandOp(Op, LHSL, LHSH);
2720
2721 std::vector<SDOperand> Ops;
2722 Ops.push_back(LHSL);
2723 Ops.push_back(LHSH);
2724 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002725 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002726 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002727 Hi = Lo.getValue(1);
2728}
2729
2730
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002731/// ExpandShift - Try to find a clever way to expand this shift operation out to
2732/// smaller elements. If we can't find a way that is more efficient than a
2733/// libcall on this target, return false. Otherwise, return true with the
2734/// low-parts expanded into Lo and Hi.
2735bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2736 SDOperand &Lo, SDOperand &Hi) {
2737 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2738 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002739
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002740 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002741 SDOperand ShAmt = LegalizeOp(Amt);
2742 MVT::ValueType ShTy = ShAmt.getValueType();
2743 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2744 unsigned NVTBits = MVT::getSizeInBits(NVT);
2745
2746 // Handle the case when Amt is an immediate. Other cases are currently broken
2747 // and are disabled.
2748 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2749 unsigned Cst = CN->getValue();
2750 // Expand the incoming operand to be shifted, so that we have its parts
2751 SDOperand InL, InH;
2752 ExpandOp(Op, InL, InH);
2753 switch(Opc) {
2754 case ISD::SHL:
2755 if (Cst > VTBits) {
2756 Lo = DAG.getConstant(0, NVT);
2757 Hi = DAG.getConstant(0, NVT);
2758 } else if (Cst > NVTBits) {
2759 Lo = DAG.getConstant(0, NVT);
2760 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002761 } else if (Cst == NVTBits) {
2762 Lo = DAG.getConstant(0, NVT);
2763 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002764 } else {
2765 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2766 Hi = DAG.getNode(ISD::OR, NVT,
2767 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2768 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2769 }
2770 return true;
2771 case ISD::SRL:
2772 if (Cst > VTBits) {
2773 Lo = DAG.getConstant(0, NVT);
2774 Hi = DAG.getConstant(0, NVT);
2775 } else if (Cst > NVTBits) {
2776 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2777 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002778 } else if (Cst == NVTBits) {
2779 Lo = InH;
2780 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002781 } else {
2782 Lo = DAG.getNode(ISD::OR, NVT,
2783 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2784 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2785 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2786 }
2787 return true;
2788 case ISD::SRA:
2789 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002790 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002791 DAG.getConstant(NVTBits-1, ShTy));
2792 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002793 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002794 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002795 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002796 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002797 } else if (Cst == NVTBits) {
2798 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002799 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002800 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002801 } else {
2802 Lo = DAG.getNode(ISD::OR, NVT,
2803 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2804 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2805 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2806 }
2807 return true;
2808 }
2809 }
Nate Begemanb0674922005-04-06 21:13:14 +00002810 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002811}
Chris Lattneraac464e2005-01-21 06:05:23 +00002812
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002813/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2814/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002815/// Found.
Chris Lattner90ba5442006-01-09 23:21:49 +00002816static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found,
2817 std::set<SDNode*> &Visited) {
Chris Lattner15afe462006-01-20 18:40:10 +00002818 if (Node->getNodeDepth() <= Found->getNodeDepth() ||
Chris Lattner90ba5442006-01-09 23:21:49 +00002819 !Visited.insert(Node).second) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002820
Chris Lattner2dce7032005-05-12 23:24:06 +00002821 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002822 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002823 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002824 Found = Node;
2825 return;
2826 }
2827
2828 // Otherwise, scan the operands of Node to see if any of them is a call.
2829 assert(Node->getNumOperands() != 0 &&
2830 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002831 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner90ba5442006-01-09 23:21:49 +00002832 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002833
2834 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002835 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner90ba5442006-01-09 23:21:49 +00002836 Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002837}
2838
2839
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002840/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2841/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002842/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002843static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2844 std::set<SDNode*> &Visited) {
Chris Lattner15afe462006-01-20 18:40:10 +00002845 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
Chris Lattner96ad3132005-08-05 18:10:27 +00002846 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002847
Chris Lattner2dce7032005-05-12 23:24:06 +00002848 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002849 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002850 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002851 Found = Node;
2852 return;
2853 }
2854
2855 // Otherwise, scan the operands of Node to see if any of them is a call.
2856 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2857 if (UI == E) return;
2858 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002859 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002860
2861 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002862 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002863}
2864
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002865/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002866/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002867static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002868 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002869 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002870 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002871 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002872
Chris Lattner02011c92006-01-14 22:41:46 +00002873 // The chain is usually at the end.
Chris Lattner4add7e32005-01-23 04:42:50 +00002874 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner02011c92006-01-14 22:41:46 +00002875 if (TheChain.getValueType() != MVT::Other) {
2876 // Sometimes it's at the beginning.
Chris Lattner3268f242005-05-14 08:34:53 +00002877 TheChain = SDOperand(Node, 0);
Chris Lattner02011c92006-01-14 22:41:46 +00002878 if (TheChain.getValueType() != MVT::Other) {
2879 // Otherwise, hunt for it.
2880 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
2881 if (Node->getValueType(i) == MVT::Other) {
2882 TheChain = SDOperand(Node, i);
2883 break;
2884 }
2885
2886 // Otherwise, we walked into a node without a chain.
2887 if (TheChain.getValueType() != MVT::Other)
2888 return 0;
2889 }
2890 }
Misha Brukman835702a2005-04-21 22:36:52 +00002891
2892 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002893 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002894
Chris Lattner4add7e32005-01-23 04:42:50 +00002895 // Make sure to only follow users of our token chain.
2896 SDNode *User = *UI;
2897 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2898 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002899 if (SDNode *Result = FindCallSeqEnd(User))
2900 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002901 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002902 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002903}
2904
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002905/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002906/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002907static SDNode *FindCallSeqStart(SDNode *Node) {
2908 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002909 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002910
2911 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2912 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002913 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002914}
2915
2916
Chris Lattner4add7e32005-01-23 04:42:50 +00002917/// FindInputOutputChains - If we are replacing an operation with a call we need
2918/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002919/// properly serialize the calls in the block. The returned operand is the
2920/// input chain value for the new call (e.g. the entry node or the previous
2921/// call), and OutChain is set to be the chain node to update to point to the
2922/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002923static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2924 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002925 SDNode *LatestCallSeqStart = Entry.Val;
2926 SDNode *LatestCallSeqEnd = 0;
Chris Lattner90ba5442006-01-09 23:21:49 +00002927 std::set<SDNode*> Visited;
2928 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited);
2929 Visited.clear();
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002930 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002931
Chris Lattner2dce7032005-05-12 23:24:06 +00002932 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002933 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002934 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2935 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00002936 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002937 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00002938 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2939 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002940 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00002941 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002942 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002943
Chris Lattner06bbeb62005-05-11 19:02:11 +00002944 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002945 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002946 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002947 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner90ba5442006-01-09 23:21:49 +00002948 Visited.clear();
Chris Lattner4add7e32005-01-23 04:42:50 +00002949
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002950 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002951 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002952 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002953
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002954 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002955}
2956
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002957/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002958void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2959 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002960 // Nothing to splice it into?
2961 if (OutChain == 0) return;
2962
2963 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2964 //OutChain->dump();
2965
2966 // Form a token factor node merging the old inval and the new inval.
2967 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2968 OutChain->getOperand(0));
2969 // Change the node to refer to the new token.
Chris Lattnerccb44762006-01-29 07:58:15 +00002970 std::vector<SDOperand> Ops(OutChain->op_begin(), OutChain->op_end());
2971 Ops[0] = InToken;
2972 SDOperand Res = DAG.UpdateNodeOperands(SDOperand(OutChain, 0), Ops);
2973 assert(Res.Val == OutChain && "Didn't update in place!");
Chris Lattner06bbeb62005-05-11 19:02:11 +00002974}
Chris Lattner4add7e32005-01-23 04:42:50 +00002975
2976
Chris Lattneraac464e2005-01-21 06:05:23 +00002977// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2978// does not fit into a register, return the lo part and set the hi part to the
2979// by-reg argument. If it does fit into a single register, return the result
2980// and leave the Hi part unset.
2981SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2982 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002983 SDNode *OutChain;
2984 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2985 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002986 if (InChain.Val == 0)
2987 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002988
Chris Lattneraac464e2005-01-21 06:05:23 +00002989 TargetLowering::ArgListTy Args;
2990 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2991 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2992 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2993 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2994 }
2995 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002996
Chris Lattner06bbeb62005-05-11 19:02:11 +00002997 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002998 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002999 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003000 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3001 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003002
Chris Lattner63022662005-09-02 20:26:58 +00003003 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003004 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003005 default: assert(0 && "Unknown thing");
3006 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003007 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003008 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003009 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003010 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003011 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003012 }
Chris Lattner10f67752006-01-28 04:28:26 +00003013
3014 CallInfo.second = LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003015 SpliceCallInto(CallInfo.second, OutChain);
Chris Lattner63022662005-09-02 20:26:58 +00003016 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003017}
3018
Chris Lattner4add7e32005-01-23 04:42:50 +00003019
Chris Lattneraac464e2005-01-21 06:05:23 +00003020/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3021/// destination type is legal.
3022SDOperand SelectionDAGLegalize::
3023ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003024 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003025 assert(getTypeAction(Source.getValueType()) == Expand &&
3026 "This is not an expansion!");
3027 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3028
Chris Lattner06bbeb62005-05-11 19:02:11 +00003029 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003030 assert(Source.getValueType() == MVT::i64 &&
3031 "This only works for 64-bit -> FP");
3032 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3033 // incoming integer is set. To handle this, we dynamically test to see if
3034 // it is set, and, if so, add a fudge factor.
3035 SDOperand Lo, Hi;
3036 ExpandOp(Source, Lo, Hi);
3037
Chris Lattner2a4f7312005-05-13 04:45:13 +00003038 // If this is unsigned, and not supported, first perform the conversion to
3039 // signed, then adjust the result if the sign bit is set.
3040 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3041 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3042
Chris Lattnerd47675e2005-08-09 20:20:18 +00003043 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3044 DAG.getConstant(0, Hi.getValueType()),
3045 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003046 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3047 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3048 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003049 uint64_t FF = 0x5f800000ULL;
3050 if (TLI.isLittleEndian()) FF <<= 32;
3051 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003052
Chris Lattnerc30405e2005-08-26 17:15:30 +00003053 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003054 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3055 SDOperand FudgeInReg;
3056 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003057 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3058 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003059 else {
3060 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003061 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3062 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003063 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003064 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003065 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003066
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003067 // Check to see if the target has a custom way to lower this. If so, use it.
3068 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3069 default: assert(0 && "This action not implemented for this operation!");
3070 case TargetLowering::Legal:
3071 case TargetLowering::Expand:
3072 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003073 case TargetLowering::Custom: {
3074 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3075 Source), DAG);
3076 if (NV.Val)
3077 return LegalizeOp(NV);
3078 break; // The target decided this was legal after all
3079 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003080 }
3081
Chris Lattner153587e2005-05-12 07:00:44 +00003082 // Expand the source, then glue it back together for the call. We must expand
3083 // the source in case it is shared (this pass of legalize must traverse it).
3084 SDOperand SrcLo, SrcHi;
3085 ExpandOp(Source, SrcLo, SrcHi);
3086 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3087
Chris Lattner06bbeb62005-05-11 19:02:11 +00003088 SDNode *OutChain = 0;
3089 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3090 DAG.getEntryNode());
3091 const char *FnName = 0;
3092 if (DestTy == MVT::f32)
3093 FnName = "__floatdisf";
3094 else {
3095 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3096 FnName = "__floatdidf";
3097 }
3098
Chris Lattneraac464e2005-01-21 06:05:23 +00003099 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3100
3101 TargetLowering::ArgListTy Args;
3102 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003103
Chris Lattneraac464e2005-01-21 06:05:23 +00003104 Args.push_back(std::make_pair(Source, ArgTy));
3105
3106 // We don't care about token chains for libcalls. We just use the entry
3107 // node as our input and ignore the output chain. This allows us to place
3108 // calls wherever we need them to satisfy data dependences.
3109 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003110
3111 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003112 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3113 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003114
Chris Lattnera5bf1032005-05-12 04:49:08 +00003115 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003116 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003117}
Misha Brukman835702a2005-04-21 22:36:52 +00003118
Chris Lattner689bdcc2006-01-28 08:25:58 +00003119/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3120/// INT_TO_FP operation of the specified operand when the target requests that
3121/// we expand it. At this point, we know that the result and operand types are
3122/// legal for the target.
3123SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3124 SDOperand Op0,
3125 MVT::ValueType DestVT) {
3126 if (Op0.getValueType() == MVT::i32) {
3127 // simple 32-bit [signed|unsigned] integer to float/double expansion
3128
3129 // get the stack frame index of a 8 byte buffer
3130 MachineFunction &MF = DAG.getMachineFunction();
3131 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3132 // get address of 8 byte buffer
3133 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3134 // word offset constant for Hi/Lo address computation
3135 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3136 // set up Hi and Lo (into buffer) address based on endian
3137 SDOperand Hi, Lo;
3138 if (TLI.isLittleEndian()) {
3139 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3140 Lo = StackSlot;
3141 } else {
3142 Hi = StackSlot;
3143 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3144 }
3145 // if signed map to unsigned space
3146 SDOperand Op0Mapped;
3147 if (isSigned) {
3148 // constant used to invert sign bit (signed to unsigned mapping)
3149 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3150 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3151 } else {
3152 Op0Mapped = Op0;
3153 }
3154 // store the lo of the constructed double - based on integer input
3155 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3156 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3157 // initial hi portion of constructed double
3158 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3159 // store the hi of the constructed double - biased exponent
3160 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3161 InitialHi, Hi, DAG.getSrcValue(NULL));
3162 // load the constructed double
3163 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3164 DAG.getSrcValue(NULL));
3165 // FP constant to bias correct the final result
3166 SDOperand Bias = DAG.getConstantFP(isSigned ?
3167 BitsToDouble(0x4330000080000000ULL)
3168 : BitsToDouble(0x4330000000000000ULL),
3169 MVT::f64);
3170 // subtract the bias
3171 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3172 // final result
3173 SDOperand Result;
3174 // handle final rounding
3175 if (DestVT == MVT::f64) {
3176 // do nothing
3177 Result = Sub;
3178 } else {
3179 // if f32 then cast to f32
3180 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3181 }
3182 return Result;
3183 }
3184 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3185 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3186
3187 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3188 DAG.getConstant(0, Op0.getValueType()),
3189 ISD::SETLT);
3190 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3191 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3192 SignSet, Four, Zero);
3193
3194 // If the sign bit of the integer is set, the large number will be treated
3195 // as a negative number. To counteract this, the dynamic code adds an
3196 // offset depending on the data type.
3197 uint64_t FF;
3198 switch (Op0.getValueType()) {
3199 default: assert(0 && "Unsupported integer type!");
3200 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3201 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3202 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3203 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3204 }
3205 if (TLI.isLittleEndian()) FF <<= 32;
3206 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3207
3208 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3209 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3210 SDOperand FudgeInReg;
3211 if (DestVT == MVT::f32)
3212 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3213 DAG.getSrcValue(NULL));
3214 else {
3215 assert(DestVT == MVT::f64 && "Unexpected conversion");
3216 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3217 DAG.getEntryNode(), CPIdx,
3218 DAG.getSrcValue(NULL), MVT::f32));
3219 }
3220
3221 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3222}
3223
3224/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3225/// *INT_TO_FP operation of the specified operand when the target requests that
3226/// we promote it. At this point, we know that the result and operand types are
3227/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3228/// operation that takes a larger input.
3229SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3230 MVT::ValueType DestVT,
3231 bool isSigned) {
3232 // First step, figure out the appropriate *INT_TO_FP operation to use.
3233 MVT::ValueType NewInTy = LegalOp.getValueType();
3234
3235 unsigned OpToUse = 0;
3236
3237 // Scan for the appropriate larger type to use.
3238 while (1) {
3239 NewInTy = (MVT::ValueType)(NewInTy+1);
3240 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3241
3242 // If the target supports SINT_TO_FP of this type, use it.
3243 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3244 default: break;
3245 case TargetLowering::Legal:
3246 if (!TLI.isTypeLegal(NewInTy))
3247 break; // Can't use this datatype.
3248 // FALL THROUGH.
3249 case TargetLowering::Custom:
3250 OpToUse = ISD::SINT_TO_FP;
3251 break;
3252 }
3253 if (OpToUse) break;
3254 if (isSigned) continue;
3255
3256 // If the target supports UINT_TO_FP of this type, use it.
3257 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3258 default: break;
3259 case TargetLowering::Legal:
3260 if (!TLI.isTypeLegal(NewInTy))
3261 break; // Can't use this datatype.
3262 // FALL THROUGH.
3263 case TargetLowering::Custom:
3264 OpToUse = ISD::UINT_TO_FP;
3265 break;
3266 }
3267 if (OpToUse) break;
3268
3269 // Otherwise, try a larger type.
3270 }
3271
3272 // Okay, we found the operation and type to use. Zero extend our input to the
3273 // desired type then run the operation on it.
3274 return DAG.getNode(OpToUse, DestVT,
3275 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3276 NewInTy, LegalOp));
3277}
3278
3279/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3280/// FP_TO_*INT operation of the specified operand when the target requests that
3281/// we promote it. At this point, we know that the result and operand types are
3282/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3283/// operation that returns a larger result.
3284SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3285 MVT::ValueType DestVT,
3286 bool isSigned) {
3287 // First step, figure out the appropriate FP_TO*INT operation to use.
3288 MVT::ValueType NewOutTy = DestVT;
3289
3290 unsigned OpToUse = 0;
3291
3292 // Scan for the appropriate larger type to use.
3293 while (1) {
3294 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3295 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3296
3297 // If the target supports FP_TO_SINT returning this type, use it.
3298 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3299 default: break;
3300 case TargetLowering::Legal:
3301 if (!TLI.isTypeLegal(NewOutTy))
3302 break; // Can't use this datatype.
3303 // FALL THROUGH.
3304 case TargetLowering::Custom:
3305 OpToUse = ISD::FP_TO_SINT;
3306 break;
3307 }
3308 if (OpToUse) break;
3309
3310 // If the target supports FP_TO_UINT of this type, use it.
3311 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3312 default: break;
3313 case TargetLowering::Legal:
3314 if (!TLI.isTypeLegal(NewOutTy))
3315 break; // Can't use this datatype.
3316 // FALL THROUGH.
3317 case TargetLowering::Custom:
3318 OpToUse = ISD::FP_TO_UINT;
3319 break;
3320 }
3321 if (OpToUse) break;
3322
3323 // Otherwise, try a larger type.
3324 }
3325
3326 // Okay, we found the operation and type to use. Truncate the result of the
3327 // extended FP_TO_*INT operation to the desired size.
3328 return DAG.getNode(ISD::TRUNCATE, DestVT,
3329 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3330}
3331
3332/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3333///
3334SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3335 MVT::ValueType VT = Op.getValueType();
3336 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3337 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3338 switch (VT) {
3339 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3340 case MVT::i16:
3341 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3342 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3343 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3344 case MVT::i32:
3345 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3346 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3347 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3348 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3349 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3350 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3351 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3352 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3353 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3354 case MVT::i64:
3355 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3356 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3357 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3358 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3359 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3360 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3361 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3362 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3363 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3364 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3365 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3366 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3367 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3368 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3369 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3370 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3371 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3372 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3373 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3374 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3375 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3376 }
3377}
3378
3379/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3380///
3381SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3382 switch (Opc) {
3383 default: assert(0 && "Cannot expand this yet!");
3384 case ISD::CTPOP: {
3385 static const uint64_t mask[6] = {
3386 0x5555555555555555ULL, 0x3333333333333333ULL,
3387 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3388 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3389 };
3390 MVT::ValueType VT = Op.getValueType();
3391 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3392 unsigned len = getSizeInBits(VT);
3393 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3394 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3395 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3396 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3397 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3398 DAG.getNode(ISD::AND, VT,
3399 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3400 }
3401 return Op;
3402 }
3403 case ISD::CTLZ: {
3404 // for now, we do this:
3405 // x = x | (x >> 1);
3406 // x = x | (x >> 2);
3407 // ...
3408 // x = x | (x >>16);
3409 // x = x | (x >>32); // for 64-bit input
3410 // return popcount(~x);
3411 //
3412 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3413 MVT::ValueType VT = Op.getValueType();
3414 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3415 unsigned len = getSizeInBits(VT);
3416 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3417 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3418 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3419 }
3420 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3421 return DAG.getNode(ISD::CTPOP, VT, Op);
3422 }
3423 case ISD::CTTZ: {
3424 // for now, we use: { return popcount(~x & (x - 1)); }
3425 // unless the target has ctlz but not ctpop, in which case we use:
3426 // { return 32 - nlz(~x & (x-1)); }
3427 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3428 MVT::ValueType VT = Op.getValueType();
3429 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3430 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3431 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3432 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3433 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3434 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3435 TLI.isOperationLegal(ISD::CTLZ, VT))
3436 return DAG.getNode(ISD::SUB, VT,
3437 DAG.getConstant(getSizeInBits(VT), VT),
3438 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3439 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3440 }
3441 }
3442}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003443
3444
Chris Lattnerdc750592005-01-07 07:47:09 +00003445/// ExpandOp - Expand the specified SDOperand into its two component pieces
3446/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3447/// LegalizeNodes map is filled in for any results that are not expanded, the
3448/// ExpandedNodes map is filled in for any results that are expanded, and the
3449/// Lo/Hi values are returned.
3450void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3451 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003452 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003453 SDNode *Node = Op.Val;
3454 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003455 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3456 "Cannot expand FP values!");
3457 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003458 "Cannot expand to FP value or to larger int value!");
3459
Chris Lattner1a570f12005-09-02 20:32:45 +00003460 // See if we already expanded it.
3461 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3462 = ExpandedNodes.find(Op);
3463 if (I != ExpandedNodes.end()) {
3464 Lo = I->second.first;
3465 Hi = I->second.second;
3466 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003467 }
3468
Chris Lattnerdc750592005-01-07 07:47:09 +00003469 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003470 case ISD::CopyFromReg:
3471 assert(0 && "CopyFromReg must be legal!");
3472 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003473 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3474 assert(0 && "Do not know how to expand this operator!");
3475 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003476 case ISD::UNDEF:
3477 Lo = DAG.getNode(ISD::UNDEF, NVT);
3478 Hi = DAG.getNode(ISD::UNDEF, NVT);
3479 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003480 case ISD::Constant: {
3481 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3482 Lo = DAG.getConstant(Cst, NVT);
3483 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3484 break;
3485 }
Nate Begemanae89d862005-12-07 19:48:11 +00003486 case ISD::ConstantVec: {
3487 unsigned NumElements = Node->getNumOperands();
3488 // If we only have two elements left in the constant vector, just break it
3489 // apart into the two scalar constants it contains. Otherwise, bisect the
3490 // ConstantVec, and return each half as a new ConstantVec.
3491 // FIXME: this is hard coded as big endian, it may have to change to support
3492 // SSE and Alpha MVI
3493 if (NumElements == 2) {
3494 Hi = Node->getOperand(0);
3495 Lo = Node->getOperand(1);
3496 } else {
3497 NumElements /= 2;
3498 std::vector<SDOperand> LoOps, HiOps;
3499 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3500 HiOps.push_back(Node->getOperand(I));
3501 LoOps.push_back(Node->getOperand(I+NumElements));
3502 }
3503 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3504 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3505 }
3506 break;
3507 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003508
Chris Lattner32e08b72005-03-28 22:03:13 +00003509 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003510 // Return the operands.
3511 Lo = Node->getOperand(0);
3512 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003513 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003514
3515 case ISD::SIGN_EXTEND_INREG:
3516 ExpandOp(Node->getOperand(0), Lo, Hi);
3517 // Sign extend the lo-part.
3518 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3519 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3520 TLI.getShiftAmountTy()));
3521 // sext_inreg the low part if needed.
3522 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3523 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003524
Nate Begeman2fba8a32006-01-14 03:14:10 +00003525 case ISD::BSWAP: {
3526 ExpandOp(Node->getOperand(0), Lo, Hi);
3527 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3528 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3529 Lo = TempLo;
3530 break;
3531 }
3532
Chris Lattner55e9cde2005-05-11 04:51:16 +00003533 case ISD::CTPOP:
3534 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003535 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3536 DAG.getNode(ISD::CTPOP, NVT, Lo),
3537 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003538 Hi = DAG.getConstant(0, NVT);
3539 break;
3540
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003541 case ISD::CTLZ: {
3542 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003543 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003544 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3545 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003546 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3547 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003548 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3549 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3550
3551 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3552 Hi = DAG.getConstant(0, NVT);
3553 break;
3554 }
3555
3556 case ISD::CTTZ: {
3557 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003558 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003559 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3560 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003561 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3562 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003563 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3564 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3565
3566 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3567 Hi = DAG.getConstant(0, NVT);
3568 break;
3569 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003570
Nate Begemane74795c2006-01-25 18:21:52 +00003571 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003572 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3573 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003574 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3575 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3576
3577 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003578 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003579 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3580 if (!TLI.isLittleEndian())
3581 std::swap(Lo, Hi);
3582 break;
3583 }
3584
Chris Lattnerdc750592005-01-07 07:47:09 +00003585 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003586 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3587 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003588 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003589
3590 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003591 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003592 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3593 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003594 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003595 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003596
3597 // Build a factor node to remember that this load is independent of the
3598 // other one.
3599 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3600 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003601
Chris Lattnerdc750592005-01-07 07:47:09 +00003602 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003603 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00003604 if (!TLI.isLittleEndian())
3605 std::swap(Lo, Hi);
3606 break;
3607 }
Nate Begemand37c1312005-11-22 18:16:00 +00003608 case ISD::VLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003609 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3610 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemand37c1312005-11-22 18:16:00 +00003611 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3612 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3613
3614 // If we only have two elements, turn into a pair of scalar loads.
3615 // FIXME: handle case where a vector of two elements is fine, such as
3616 // 2 x double on SSE2.
3617 if (NumElements == 2) {
3618 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3619 // Increment the pointer to the other half.
3620 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3621 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3622 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003623 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003624 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3625 } else {
3626 NumElements /= 2; // Split the vector in half
3627 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3628 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3629 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3630 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003631 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003632 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3633 }
3634
3635 // Build a factor node to remember that this load is independent of the
3636 // other one.
3637 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3638 Hi.getValue(1));
3639
3640 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003641 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemand37c1312005-11-22 18:16:00 +00003642 if (!TLI.isLittleEndian())
3643 std::swap(Lo, Hi);
3644 break;
3645 }
3646 case ISD::VADD:
3647 case ISD::VSUB:
3648 case ISD::VMUL: {
3649 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3650 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3651 SDOperand LL, LH, RL, RH;
3652
3653 ExpandOp(Node->getOperand(0), LL, LH);
3654 ExpandOp(Node->getOperand(1), RL, RH);
3655
3656 // If we only have two elements, turn into a pair of scalar loads.
3657 // FIXME: handle case where a vector of two elements is fine, such as
3658 // 2 x double on SSE2.
3659 if (NumElements == 2) {
3660 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3661 Lo = DAG.getNode(Opc, EVT, LL, RL);
3662 Hi = DAG.getNode(Opc, EVT, LH, RH);
3663 } else {
3664 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3665 LL.getOperand(3));
3666 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3667 LH.getOperand(3));
3668 }
3669 break;
3670 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003671 case ISD::AND:
3672 case ISD::OR:
3673 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3674 SDOperand LL, LH, RL, RH;
3675 ExpandOp(Node->getOperand(0), LL, LH);
3676 ExpandOp(Node->getOperand(1), RL, RH);
3677 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3678 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3679 break;
3680 }
3681 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003682 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00003683 ExpandOp(Node->getOperand(1), LL, LH);
3684 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003685 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3686 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00003687 break;
3688 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003689 case ISD::SELECT_CC: {
3690 SDOperand TL, TH, FL, FH;
3691 ExpandOp(Node->getOperand(2), TL, TH);
3692 ExpandOp(Node->getOperand(3), FL, FH);
3693 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3694 Node->getOperand(1), TL, FL, Node->getOperand(4));
3695 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3696 Node->getOperand(1), TH, FH, Node->getOperand(4));
3697 break;
3698 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003699 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003700 SDOperand Chain = Node->getOperand(0);
3701 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003702 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3703
3704 if (EVT == NVT)
3705 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3706 else
3707 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3708 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003709
3710 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003711 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003712
Nate Begemanc3a89c52005-10-13 17:15:37 +00003713 // The high part is obtained by SRA'ing all but one of the bits of the lo
3714 // part.
3715 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3716 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3717 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003718 break;
3719 }
3720 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003721 SDOperand Chain = Node->getOperand(0);
3722 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003723 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3724
3725 if (EVT == NVT)
3726 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3727 else
3728 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3729 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003730
3731 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003732 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003733
Nate Begemanc3a89c52005-10-13 17:15:37 +00003734 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003735 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003736 break;
3737 }
3738 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003739 SDOperand Chain = Node->getOperand(0);
3740 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00003741 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3742
3743 if (EVT == NVT)
3744 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3745 else
3746 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3747 EVT);
3748
3749 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003750 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003751
3752 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00003753 Hi = DAG.getNode(ISD::UNDEF, NVT);
3754 break;
3755 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003756 case ISD::ANY_EXTEND:
3757 // The low part is any extension of the input (which degenerates to a copy).
3758 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3759 // The high part is undefined.
3760 Hi = DAG.getNode(ISD::UNDEF, NVT);
3761 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003762 case ISD::SIGN_EXTEND: {
3763 // The low part is just a sign extension of the input (which degenerates to
3764 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003765 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003766
Chris Lattnerdc750592005-01-07 07:47:09 +00003767 // The high part is obtained by SRA'ing all but one of the bits of the lo
3768 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003769 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003770 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3771 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003772 break;
3773 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003774 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00003775 // The low part is just a zero extension of the input (which degenerates to
3776 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003777 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003778
Chris Lattnerdc750592005-01-07 07:47:09 +00003779 // The high part is just a zero.
3780 Hi = DAG.getConstant(0, NVT);
3781 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003782
3783 case ISD::BIT_CONVERT: {
3784 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3785 Node->getOperand(0));
3786 ExpandOp(Tmp, Lo, Hi);
3787 break;
3788 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003789
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003790 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00003791 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3792 TargetLowering::Custom &&
3793 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003794 Lo = TLI.LowerOperation(Op, DAG);
3795 assert(Lo.Val && "Node must be custom expanded!");
3796 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00003797 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003798 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003799 break;
3800
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003801 // These operators cannot be expanded directly, emit them as calls to
3802 // library functions.
3803 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003804 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003805 SDOperand Op;
3806 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3807 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003808 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3809 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00003810 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003811
Chris Lattner941d84a2005-07-30 01:40:57 +00003812 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3813
Chris Lattnerfe68d752005-07-29 00:33:32 +00003814 // Now that the custom expander is done, expand the result, which is still
3815 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003816 if (Op.Val) {
3817 ExpandOp(Op, Lo, Hi);
3818 break;
3819 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003820 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003821
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003822 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003823 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003824 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003825 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003826 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003827
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003828 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003829 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003830 SDOperand Op;
3831 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3832 case Expand: assert(0 && "cannot expand FP!");
3833 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3834 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3835 }
3836
3837 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3838
3839 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003840 if (Op.Val) {
3841 ExpandOp(Op, Lo, Hi);
3842 break;
3843 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003844 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003845
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003846 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003847 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003848 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003849 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003850 break;
3851
Evan Cheng870e4f82006-01-09 18:31:59 +00003852 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003853 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003854 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003855 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003856 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003857 Op = TLI.LowerOperation(Op, DAG);
3858 if (Op.Val) {
3859 // Now that the custom expander is done, expand the result, which is
3860 // still VT.
3861 ExpandOp(Op, Lo, Hi);
3862 break;
3863 }
3864 }
3865
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003866 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003867 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003868 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003869
3870 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003871 TargetLowering::LegalizeAction Action =
3872 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3873 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3874 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003875 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003876 break;
3877 }
3878
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003879 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003880 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003881 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003882 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003883
Evan Cheng870e4f82006-01-09 18:31:59 +00003884 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003885 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003886 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003887 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003888 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003889 Op = TLI.LowerOperation(Op, DAG);
3890 if (Op.Val) {
3891 // Now that the custom expander is done, expand the result, which is
3892 // still VT.
3893 ExpandOp(Op, Lo, Hi);
3894 break;
3895 }
3896 }
3897
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003898 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003899 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003900 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003901
3902 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003903 TargetLowering::LegalizeAction Action =
3904 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3905 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3906 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003907 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003908 break;
3909 }
3910
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003911 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003912 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003913 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003914 }
3915
3916 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003917 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003918 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003919 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003920 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003921 Op = TLI.LowerOperation(Op, DAG);
3922 if (Op.Val) {
3923 // Now that the custom expander is done, expand the result, which is
3924 // still VT.
3925 ExpandOp(Op, Lo, Hi);
3926 break;
3927 }
3928 }
3929
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003930 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003931 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003932 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003933
3934 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003935 TargetLowering::LegalizeAction Action =
3936 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3937 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3938 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003939 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003940 break;
3941 }
3942
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003943 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003944 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003945 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003946 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003947
Misha Brukman835702a2005-04-21 22:36:52 +00003948 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003949 case ISD::SUB: {
3950 // If the target wants to custom expand this, let them.
3951 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3952 TargetLowering::Custom) {
3953 Op = TLI.LowerOperation(Op, DAG);
3954 if (Op.Val) {
3955 ExpandOp(Op, Lo, Hi);
3956 break;
3957 }
3958 }
3959
3960 // Expand the subcomponents.
3961 SDOperand LHSL, LHSH, RHSL, RHSH;
3962 ExpandOp(Node->getOperand(0), LHSL, LHSH);
3963 ExpandOp(Node->getOperand(1), RHSL, RHSH);
3964
3965 std::vector<SDOperand> Ops;
3966 Ops.push_back(LHSL);
3967 Ops.push_back(LHSH);
3968 Ops.push_back(RHSL);
3969 Ops.push_back(RHSH);
3970 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
3971 unsigned Opc =
3972 Node->getOpcode() == ISD::ADD ? ISD::ADD_PARTS : ISD::SUB_PARTS;
3973 Lo = DAG.getNode(Opc, VTs, Ops);
3974 Hi = Lo.getValue(1);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003975 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003976 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003977 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003978 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003979 SDOperand LL, LH, RL, RH;
3980 ExpandOp(Node->getOperand(0), LL, LH);
3981 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003982 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3983 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3984 // extended the sign bit of the low half through the upper half, and if so
3985 // emit a MULHS instead of the alternate sequence that is valid for any
3986 // i64 x i64 multiply.
3987 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3988 // is RH an extension of the sign bit of RL?
3989 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3990 RH.getOperand(1).getOpcode() == ISD::Constant &&
3991 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3992 // is LH an extension of the sign bit of LL?
3993 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3994 LH.getOperand(1).getOpcode() == ISD::Constant &&
3995 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3996 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3997 } else {
3998 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3999 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4000 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4001 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4002 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4003 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004004 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4005 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004006 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004007 }
4008 break;
4009 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004010 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4011 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4012 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4013 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004014 }
4015
Chris Lattnerac12f682005-12-21 18:02:52 +00004016 // Make sure the resultant values have been legalized themselves, unless this
4017 // is a type that requires multi-step expansion.
4018 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4019 Lo = LegalizeOp(Lo);
4020 Hi = LegalizeOp(Hi);
4021 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004022
4023 // Remember in a map if the values will be reused later.
4024 bool isNew =
4025 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4026 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004027}
4028
4029
4030// SelectionDAG::Legalize - This is the entry point for the file.
4031//
Chris Lattner4add7e32005-01-23 04:42:50 +00004032void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004033 /// run - This is the main entry point to this class.
4034 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004035 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004036}
4037