blob: f09fa625006adf489be6232f421ec06396a3ee63 [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.
Chris Lattnere9721b22006-01-31 05:04:52 +0000706 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
707 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
708 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner539c3fa2005-08-21 18:03:09 +0000709
Nate Begeman371e4952005-08-16 19:49:35 +0000710 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
711 DAG.getCondCode(ISD::SETNE), Tmp2,
712 DAG.getConstant(0, Tmp2.getValueType()),
713 Node->getOperand(2));
714 }
715 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000716 }
717 break;
718 case ISD::BR_CC:
719 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000720 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000721 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
722 Node->getOperand(2), // LHS
723 Node->getOperand(3), // RHS
724 Node->getOperand(1)));
725 // If we get a SETCC back from legalizing the SETCC node we just
726 // created, then use its LHS, RHS, and CC directly in creating a new
727 // node. Otherwise, select between the true and false value based on
728 // comparing the result of the legalized with zero.
729 if (Tmp2.getOpcode() == ISD::SETCC) {
730 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
731 Tmp2.getOperand(0), Tmp2.getOperand(1),
732 Node->getOperand(4));
733 } else {
734 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
735 DAG.getCondCode(ISD::SETNE),
736 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
737 Node->getOperand(4));
738 }
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000739 break;
740 }
741
742 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
743 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
744
Chris Lattnerd02b0542006-01-28 10:58:55 +0000745 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
746 Tmp3, Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000747
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000748 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
749 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000750 case TargetLowering::Legal: break;
751 case TargetLowering::Custom:
752 Tmp4 = TLI.LowerOperation(Result, DAG);
753 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000754 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000755 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000756 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000757 case ISD::BRCONDTWOWAY:
758 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
759 switch (getTypeAction(Node->getOperand(1).getValueType())) {
760 case Expand: assert(0 && "It's impossible to expand bools");
761 case Legal:
762 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
763 break;
764 case Promote:
765 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
766 break;
767 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000768
Chris Lattnerfd986782005-04-09 03:30:19 +0000769 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
770 // pair.
771 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
772 case TargetLowering::Promote:
773 default: assert(0 && "This action is not supported yet!");
774 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000775 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
776 Node->getOperand(3));
Chris Lattnerfd986782005-04-09 03:30:19 +0000777 break;
778 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000779 // If BRTWOWAY_CC is legal for this target, then simply expand this node
780 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
781 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000782 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000783 if (Tmp2.getOpcode() == ISD::SETCC) {
Chris Lattner678da982006-01-29 06:00:45 +0000784 Tmp3 = Tmp2.getOperand(0);
785 Tmp4 = Tmp2.getOperand(1);
786 Tmp2 = Tmp2.getOperand(2);
Nate Begeman371e4952005-08-16 19:49:35 +0000787 } else {
Chris Lattner678da982006-01-29 06:00:45 +0000788 Tmp3 = Tmp2;
789 Tmp4 = DAG.getConstant(0, Tmp2.getValueType());
790 Tmp2 = DAG.getCondCode(ISD::SETNE);
Nate Begeman371e4952005-08-16 19:49:35 +0000791 }
Chris Lattner678da982006-01-29 06:00:45 +0000792 std::vector<SDOperand> Ops;
793 Ops.push_back(Tmp1);
794 Ops.push_back(Tmp2);
795 Ops.push_back(Tmp3);
796 Ops.push_back(Tmp4);
797 Ops.push_back(Node->getOperand(2));
798 Ops.push_back(Node->getOperand(3));
799 Result = DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
Nate Begeman371e4952005-08-16 19:49:35 +0000800 } else {
801 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000802 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000803 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
804 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000805 break;
806 }
807 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000808 case ISD::BRTWOWAY_CC:
809 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000810 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000811 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
812 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
813 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
814 Tmp3 != Node->getOperand(3)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000815 std::vector<SDOperand> Ops;
816 Ops.push_back(Tmp1);
817 Ops.push_back(Node->getOperand(1));
818 Ops.push_back(Tmp2);
819 Ops.push_back(Tmp3);
820 Ops.push_back(Node->getOperand(4));
821 Ops.push_back(Node->getOperand(5));
822 Result = DAG.UpdateNodeOperands(Result, Ops);
Nate Begeman371e4952005-08-16 19:49:35 +0000823 }
824 break;
825 } else {
826 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
827 Node->getOperand(2), // LHS
828 Node->getOperand(3), // RHS
829 Node->getOperand(1)));
830 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
831 // pair.
832 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
833 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000834 case TargetLowering::Legal: {
Nate Begeman371e4952005-08-16 19:49:35 +0000835 // If we get a SETCC back from legalizing the SETCC node we just
836 // created, then use its LHS, RHS, and CC directly in creating a new
837 // node. Otherwise, select between the true and false value based on
838 // comparing the result of the legalized with zero.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000839 std::vector<SDOperand> Ops;
840 Ops.push_back(Tmp1);
Nate Begeman371e4952005-08-16 19:49:35 +0000841 if (Tmp2.getOpcode() == ISD::SETCC) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000842 Ops.push_back(Tmp2.getOperand(2));
843 Ops.push_back(Tmp2.getOperand(0));
844 Ops.push_back(Tmp2.getOperand(1));
Nate Begeman371e4952005-08-16 19:49:35 +0000845 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000846 Ops.push_back(DAG.getCondCode(ISD::SETNE));
847 Ops.push_back(Tmp2);
848 Ops.push_back(DAG.getConstant(0, Tmp2.getValueType()));
Nate Begeman371e4952005-08-16 19:49:35 +0000849 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000850 Ops.push_back(Node->getOperand(4));
851 Ops.push_back(Node->getOperand(5));
852 Result = DAG.UpdateNodeOperands(Result, Ops);
Nate Begeman371e4952005-08-16 19:49:35 +0000853 break;
Chris Lattnerd02b0542006-01-28 10:58:55 +0000854 }
Nate Begeman371e4952005-08-16 19:49:35 +0000855 case TargetLowering::Expand:
856 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
857 Node->getOperand(4));
858 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
859 break;
860 }
861 }
862 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +0000863 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000864 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
865 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000866
Evan Cheng31d15fa2005-12-23 07:29:34 +0000867 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000868 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
869 Tmp2 = Result.getValue(0);
870 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000871
Evan Cheng31d15fa2005-12-23 07:29:34 +0000872 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
873 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000874 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000875 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000876 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000877 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000878 Tmp2 = LegalizeOp(Tmp1);
879 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +0000880 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000881 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +0000882 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000883 // Since loads produce two values, make sure to remember that we
884 // legalized both of them.
885 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
886 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
887 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +0000888 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000889 case ISD::EXTLOAD:
890 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000891 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000892 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
893 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000894
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000895 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000896 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000897 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000898 case TargetLowering::Promote:
899 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000900 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
901 DAG.getValueType(MVT::i8));
902 Tmp1 = Result.getValue(0);
903 Tmp2 = Result.getValue(1);
904 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000905 case TargetLowering::Custom:
906 isCustom = true;
907 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000908 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000909 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
910 Node->getOperand(3));
911 Tmp1 = Result.getValue(0);
912 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000913
914 if (isCustom) {
915 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
916 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000917 Tmp1 = LegalizeOp(Tmp3);
918 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000919 }
920 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000921 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000922 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +0000923 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000924 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
925 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000926 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000927 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
928 Tmp2 = LegalizeOp(Load.getValue(1));
929 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000930 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000931 assert(Node->getOpcode() != ISD::EXTLOAD &&
932 "EXTLOAD should always be supported!");
933 // Turn the unsupported load into an EXTLOAD followed by an explicit
934 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000935 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
936 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000937 SDOperand ValRes;
938 if (Node->getOpcode() == ISD::SEXTLOAD)
939 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000940 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000941 else
942 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000943 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
944 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
945 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000946 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000947 // Since loads produce two values, make sure to remember that we legalized
948 // both of them.
949 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
950 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
951 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000952 }
Nate Begeman5172ce62005-10-19 00:06:56 +0000953 case ISD::EXTRACT_ELEMENT: {
954 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
955 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000956 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +0000957 case Legal:
958 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
959 // 1 -> Hi
960 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
961 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
962 TLI.getShiftAmountTy()));
963 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
964 } else {
965 // 0 -> Lo
966 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
967 Node->getOperand(0));
968 }
Nate Begeman5172ce62005-10-19 00:06:56 +0000969 break;
970 case Expand:
971 // Get both the low and high parts.
972 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
973 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
974 Result = Tmp2; // 1 -> Hi
975 else
976 Result = Tmp1; // 0 -> Lo
977 break;
978 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000979 break;
Nate Begeman5172ce62005-10-19 00:06:56 +0000980 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000981
982 case ISD::CopyToReg:
983 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000984
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000985 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +0000986 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +0000987 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +0000988 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000989 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000990 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +0000991 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000992 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000993 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000994 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000995 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
996 Tmp3);
997 } else {
998 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +0000999 }
1000
1001 // Since this produces two values, make sure to remember that we legalized
1002 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001003 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001004 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001005 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001006 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001007 break;
1008
1009 case ISD::RET:
1010 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1011 switch (Node->getNumOperands()) {
1012 case 2: // ret val
1013 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1014 case Legal:
1015 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001016 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001017 break;
1018 case Expand: {
1019 SDOperand Lo, Hi;
1020 ExpandOp(Node->getOperand(1), Lo, Hi);
1021 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001022 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001023 }
1024 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001025 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001026 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1027 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001028 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001029 }
1030 break;
1031 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001032 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001033 break;
1034 default: { // ret <values>
1035 std::vector<SDOperand> NewValues;
1036 NewValues.push_back(Tmp1);
1037 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1038 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1039 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001040 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001041 break;
1042 case Expand: {
1043 SDOperand Lo, Hi;
1044 ExpandOp(Node->getOperand(i), Lo, Hi);
1045 NewValues.push_back(Lo);
1046 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001047 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001048 }
1049 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001050 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001051 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001052
1053 if (NewValues.size() == Node->getNumOperands())
1054 Result = DAG.UpdateNodeOperands(Result, NewValues);
1055 else
1056 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001057 break;
1058 }
1059 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001060
Chris Lattner4d1ea712006-01-29 21:02:23 +00001061 if (Result.getOpcode() == ISD::RET) {
1062 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1063 default: assert(0 && "This action is not supported yet!");
1064 case TargetLowering::Legal: break;
1065 case TargetLowering::Custom:
1066 Tmp1 = TLI.LowerOperation(Result, DAG);
1067 if (Tmp1.Val) Result = Tmp1;
1068 break;
1069 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001070 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001071 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001072 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001073 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1074 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1075
Chris Lattnere69daaf2005-01-08 06:25:56 +00001076 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001077 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001078 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001079 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001080 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001081 } else {
1082 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001083 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001084 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001085 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1086 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001087 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001088 }
1089
Chris Lattnerdc750592005-01-07 07:47:09 +00001090 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1091 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001092 Tmp3 = LegalizeOp(Node->getOperand(1));
1093 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1094 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001095
Chris Lattnerd02b0542006-01-28 10:58:55 +00001096 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001097 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1098 default: assert(0 && "This action is not supported yet!");
1099 case TargetLowering::Legal: break;
1100 case TargetLowering::Custom:
1101 Tmp1 = TLI.LowerOperation(Result, DAG);
1102 if (Tmp1.Val) Result = Tmp1;
1103 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001104 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001105 break;
1106 }
1107 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001108 // Truncate the value and store the result.
1109 Tmp3 = PromoteOp(Node->getOperand(1));
1110 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001111 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001112 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001113 break;
1114
Chris Lattnerdc750592005-01-07 07:47:09 +00001115 case Expand:
1116 SDOperand Lo, Hi;
1117 ExpandOp(Node->getOperand(1), Lo, Hi);
1118
1119 if (!TLI.isLittleEndian())
1120 std::swap(Lo, Hi);
1121
Chris Lattner55e9cde2005-05-11 04:51:16 +00001122 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1123 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001124 // If this is a vector type, then we have to calculate the increment as
1125 // the product of the element size in bytes, and the number of elements
1126 // in the high half of the vector.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001127 unsigned IncrementSize;
Nate Begemand37c1312005-11-22 18:16:00 +00001128 if (MVT::Vector == Hi.getValueType()) {
1129 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1130 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1131 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1132 } else {
1133 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1134 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001135 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1136 getIntPtrConstant(IncrementSize));
1137 assert(isTypeLegal(Tmp2.getValueType()) &&
1138 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001139 // FIXME: This sets the srcvalue of both halves to be the same, which is
1140 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001141 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1142 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001143 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1144 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001145 }
1146 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001147 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001148 case ISD::PCMARKER:
1149 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001151 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001152 case ISD::STACKSAVE:
1153 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001154 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1155 Tmp1 = Result.getValue(0);
1156 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001157
Chris Lattnerb3266452006-01-13 02:50:02 +00001158 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1159 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001160 case TargetLowering::Legal: break;
1161 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001162 Tmp3 = TLI.LowerOperation(Result, DAG);
1163 if (Tmp3.Val) {
1164 Tmp1 = LegalizeOp(Tmp3);
1165 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001166 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001167 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001168 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001169 // Expand to CopyFromReg if the target set
1170 // StackPointerRegisterToSaveRestore.
1171 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001172 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001173 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001174 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001175 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001176 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1177 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001178 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001179 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001180 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001181
1182 // Since stacksave produce two values, make sure to remember that we
1183 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001184 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1185 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1186 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001187
Chris Lattnerb3266452006-01-13 02:50:02 +00001188 case ISD::STACKRESTORE:
1189 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1190 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001191 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001192
1193 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1194 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001195 case TargetLowering::Legal: break;
1196 case TargetLowering::Custom:
1197 Tmp1 = TLI.LowerOperation(Result, DAG);
1198 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001199 break;
1200 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001201 // Expand to CopyToReg if the target set
1202 // StackPointerRegisterToSaveRestore.
1203 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1204 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1205 } else {
1206 Result = Tmp1;
1207 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001208 break;
1209 }
1210 break;
1211
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001212 case ISD::READCYCLECOUNTER:
1213 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001214 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001215
1216 // Since rdcc produce two values, make sure to remember that we legalized
1217 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001218 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001219 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001220 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001221
Evan Cheng31d15fa2005-12-23 07:29:34 +00001222 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001223 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1224 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1225
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001226 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1227 "Cannot handle illegal TRUNCSTORE yet!");
1228 Tmp2 = LegalizeOp(Node->getOperand(1));
1229
1230 // The only promote case we handle is TRUNCSTORE:i1 X into
1231 // -> TRUNCSTORE:i8 (and X, 1)
1232 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1233 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1234 TargetLowering::Promote) {
1235 // Promote the bool to a mask then store.
1236 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1237 DAG.getConstant(1, Tmp2.getValueType()));
1238 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1239 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001240
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001241 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1242 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001243 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1244 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001245 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001246
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001247 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1248 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1249 default: assert(0 && "This action is not supported yet!");
1250 case TargetLowering::Legal: break;
1251 case TargetLowering::Custom:
1252 Tmp1 = TLI.LowerOperation(Result, DAG);
1253 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001254 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001255 }
1256 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001257 }
Chris Lattner39c67442005-01-14 22:08:15 +00001258 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001259 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1260 case Expand: assert(0 && "It's impossible to expand bools");
1261 case Legal:
1262 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1263 break;
1264 case Promote:
1265 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1266 break;
1267 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001268 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001269 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001270
Chris Lattnerd02b0542006-01-28 10:58:55 +00001271 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001272
Nate Begeman987121a2005-08-23 04:29:48 +00001273 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001274 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001275 case TargetLowering::Legal: break;
1276 case TargetLowering::Custom: {
1277 Tmp1 = TLI.LowerOperation(Result, DAG);
1278 if (Tmp1.Val) Result = Tmp1;
1279 break;
1280 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001281 case TargetLowering::Expand:
1282 if (Tmp1.getOpcode() == ISD::SETCC) {
1283 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1284 Tmp2, Tmp3,
1285 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1286 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001287 // Make sure the condition is either zero or one. It may have been
1288 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001289 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1290 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1291 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001292 Result = DAG.getSelectCC(Tmp1,
1293 DAG.getConstant(0, Tmp1.getValueType()),
1294 Tmp2, Tmp3, ISD::SETNE);
1295 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001296 break;
1297 case TargetLowering::Promote: {
1298 MVT::ValueType NVT =
1299 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1300 unsigned ExtOp, TruncOp;
1301 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001302 ExtOp = ISD::ANY_EXTEND;
1303 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001304 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001305 ExtOp = ISD::FP_EXTEND;
1306 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001307 }
1308 // Promote each of the values to the new type.
1309 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1310 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1311 // Perform the larger operation, then round down.
1312 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1313 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1314 break;
1315 }
1316 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001317 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001318 case ISD::SELECT_CC:
1319 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1320 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1321
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001322 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001323 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1324 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00001325
1326 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4,
1327 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001328
Chris Lattner5f573412005-08-26 00:23:59 +00001329 // Everything is legal, see if we should expand this op or something.
1330 switch (TLI.getOperationAction(ISD::SELECT_CC,
1331 Node->getOperand(0).getValueType())) {
1332 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001333 case TargetLowering::Legal: break;
1334 case TargetLowering::Custom:
1335 Tmp1 = TLI.LowerOperation(Result, DAG);
1336 if (Tmp1.Val) Result = Tmp1;
Chris Lattner5f573412005-08-26 00:23:59 +00001337 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001338 }
1339 break;
1340 } else {
1341 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1342 Node->getOperand(0), // LHS
1343 Node->getOperand(1), // RHS
1344 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001345 // If we get a SETCC back from legalizing the SETCC node we just
1346 // created, then use its LHS, RHS, and CC directly in creating a new
1347 // node. Otherwise, select between the true and false value based on
1348 // comparing the result of the legalized with zero.
1349 if (Tmp1.getOpcode() == ISD::SETCC) {
1350 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1351 Tmp1.getOperand(0), Tmp1.getOperand(1),
1352 Tmp3, Tmp4, Tmp1.getOperand(2));
1353 } else {
1354 Result = DAG.getSelectCC(Tmp1,
1355 DAG.getConstant(0, Tmp1.getValueType()),
1356 Tmp3, Tmp4, ISD::SETNE);
1357 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001358 }
1359 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001360 case ISD::SETCC:
1361 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1362 case Legal:
1363 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1364 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001365 break;
1366 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001367 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1368 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1369
1370 // If this is an FP compare, the operands have already been extended.
1371 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1372 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001373 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001374
1375 // Otherwise, we have to insert explicit sign or zero extends. Note
1376 // that we could insert sign extends for ALL conditions, but zero extend
1377 // is cheaper on many machines (an AND instead of two shifts), so prefer
1378 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001379 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001380 default: assert(0 && "Unknown integer comparison!");
1381 case ISD::SETEQ:
1382 case ISD::SETNE:
1383 case ISD::SETUGE:
1384 case ISD::SETUGT:
1385 case ISD::SETULE:
1386 case ISD::SETULT:
1387 // ALL of these operations will work if we either sign or zero extend
1388 // the operands (including the unsigned comparisons!). Zero extend is
1389 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001390 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1391 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001392 break;
1393 case ISD::SETGE:
1394 case ISD::SETGT:
1395 case ISD::SETLT:
1396 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001397 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1398 DAG.getValueType(VT));
1399 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1400 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001401 break;
1402 }
Chris Lattner4d978642005-01-15 22:16:26 +00001403 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001404 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001405 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001406 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1407 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1408 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001409 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001410 case ISD::SETEQ:
1411 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001412 if (RHSLo == RHSHi)
1413 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1414 if (RHSCST->isAllOnesValue()) {
1415 // Comparison to -1.
1416 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001417 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001418 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001419 }
1420
Chris Lattnerdc750592005-01-07 07:47:09 +00001421 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1422 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1423 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001424 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001425 break;
1426 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001427 // If this is a comparison of the sign bit, just look at the top part.
1428 // X > -1, x < 0
1429 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001430 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001431 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001432 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001433 (CST->isAllOnesValue()))) { // X > -1
1434 Tmp1 = LHSHi;
1435 Tmp2 = RHSHi;
1436 break;
1437 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001438
Chris Lattnerdc750592005-01-07 07:47:09 +00001439 // FIXME: This generated code sucks.
1440 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001441 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001442 default: assert(0 && "Unknown integer setcc!");
1443 case ISD::SETLT:
1444 case ISD::SETULT: LowCC = ISD::SETULT; break;
1445 case ISD::SETGT:
1446 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1447 case ISD::SETLE:
1448 case ISD::SETULE: LowCC = ISD::SETULE; break;
1449 case ISD::SETGE:
1450 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1451 }
Misha Brukman835702a2005-04-21 22:36:52 +00001452
Chris Lattnerdc750592005-01-07 07:47:09 +00001453 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1454 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1455 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1456
1457 // NOTE: on targets without efficient SELECT of bools, we can always use
1458 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001459 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1460 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1461 Node->getOperand(2));
1462 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001463 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1464 Result, Tmp1, Tmp2));
Chris Lattner2af3ee42005-12-20 00:53:54 +00001465 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman987121a2005-08-23 04:29:48 +00001466 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001467 }
1468 }
Nate Begeman987121a2005-08-23 04:29:48 +00001469
Chris Lattnerf263a232006-01-30 22:43:50 +00001470 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001471 default: assert(0 && "Cannot handle this action for SETCC yet!");
1472 case TargetLowering::Custom:
1473 isCustom = true;
1474 // FALLTHROUGH.
1475 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001476 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001477 if (isCustom) {
1478 Tmp3 = TLI.LowerOperation(Result, DAG);
1479 if (Tmp3.Val) Result = Tmp3;
1480 }
Nate Begeman987121a2005-08-23 04:29:48 +00001481 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001482 case TargetLowering::Promote: {
1483 // First step, figure out the appropriate operation to use.
1484 // Allow SETCC to not be supported for all legal data types
1485 // Mostly this targets FP
1486 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1487 MVT::ValueType OldVT = NewInTy;
1488
1489 // Scan for the appropriate larger type to use.
1490 while (1) {
1491 NewInTy = (MVT::ValueType)(NewInTy+1);
1492
1493 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1494 "Fell off of the edge of the integer world");
1495 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1496 "Fell off of the edge of the floating point world");
1497
1498 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001499 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001500 break;
1501 }
1502 if (MVT::isInteger(NewInTy))
1503 assert(0 && "Cannot promote Legal Integer SETCC yet");
1504 else {
1505 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1506 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1507 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001508 Tmp1 = LegalizeOp(Tmp1);
1509 Tmp2 = LegalizeOp(Tmp2);
1510 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001511 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001512 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001513 }
Nate Begeman987121a2005-08-23 04:29:48 +00001514 case TargetLowering::Expand:
1515 // Expand a setcc node into a select_cc of the same condition, lhs, and
1516 // rhs that selects between const 1 (true) and const 0 (false).
1517 MVT::ValueType VT = Node->getValueType(0);
1518 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1519 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1520 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001521 break;
1522 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001523 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001524 case ISD::MEMSET:
1525 case ISD::MEMCPY:
1526 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001527 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001528 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1529
1530 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1531 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1532 case Expand: assert(0 && "Cannot expand a byte!");
1533 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001534 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001535 break;
1536 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001537 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001538 break;
1539 }
1540 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001541 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001542 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001543
1544 SDOperand Tmp4;
1545 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001546 case Expand: {
1547 // Length is too big, just take the lo-part of the length.
1548 SDOperand HiPart;
1549 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1550 break;
1551 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001552 case Legal:
1553 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001554 break;
1555 case Promote:
1556 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001557 break;
1558 }
1559
1560 SDOperand Tmp5;
1561 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1562 case Expand: assert(0 && "Cannot expand this yet!");
1563 case Legal:
1564 Tmp5 = LegalizeOp(Node->getOperand(4));
1565 break;
1566 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001567 Tmp5 = PromoteOp(Node->getOperand(4));
1568 break;
1569 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001570
1571 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1572 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001573 case TargetLowering::Custom:
1574 isCustom = true;
1575 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001576 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001577 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001578 if (isCustom) {
1579 Tmp1 = TLI.LowerOperation(Result, DAG);
1580 if (Tmp1.Val) Result = Tmp1;
1581 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001582 break;
1583 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001584 // Otherwise, the target does not support this operation. Lower the
1585 // operation to an explicit libcall as appropriate.
1586 MVT::ValueType IntPtr = TLI.getPointerTy();
1587 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1588 std::vector<std::pair<SDOperand, const Type*> > Args;
1589
Reid Spencer6dced922005-01-12 14:53:45 +00001590 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001591 if (Node->getOpcode() == ISD::MEMSET) {
1592 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1593 // Extend the ubyte argument to be an int value for the call.
1594 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1595 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1596 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1597
1598 FnName = "memset";
1599 } else if (Node->getOpcode() == ISD::MEMCPY ||
1600 Node->getOpcode() == ISD::MEMMOVE) {
1601 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1602 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1603 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1604 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1605 } else {
1606 assert(0 && "Unknown op!");
1607 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001608
Chris Lattner85d70c62005-01-11 05:57:22 +00001609 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001610 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001611 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001612 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001613 break;
1614 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001615 }
1616 break;
1617 }
Chris Lattner5385db52005-05-09 20:23:03 +00001618
1619 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001620 Tmp1 = LegalizeOp(Node->getOperand(0));
1621 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001622 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001623
Chris Lattner5385db52005-05-09 20:23:03 +00001624 // Since these produce two values, make sure to remember that we legalized
1625 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001626 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner5385db52005-05-09 20:23:03 +00001627 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001628 return Result;
Chris Lattner5385db52005-05-09 20:23:03 +00001629 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001630 Tmp1 = LegalizeOp(Node->getOperand(0));
1631 Tmp2 = LegalizeOp(Node->getOperand(1));
1632 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001633 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner5385db52005-05-09 20:23:03 +00001634 break;
1635
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001636 case ISD::READIO:
1637 Tmp1 = LegalizeOp(Node->getOperand(0));
1638 Tmp2 = LegalizeOp(Node->getOperand(1));
1639
1640 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1641 case TargetLowering::Custom:
1642 default: assert(0 && "This action not implemented for this operation!");
1643 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001644 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001645 break;
1646 case TargetLowering::Expand:
1647 // Replace this with a load from memory.
1648 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1649 Node->getOperand(1), DAG.getSrcValue(NULL));
1650 Result = LegalizeOp(Result);
1651 break;
1652 }
1653
1654 // Since these produce two values, make sure to remember that we legalized
1655 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001656 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001657 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1658 return Result.getValue(Op.ResNo);
1659
1660 case ISD::WRITEIO:
1661 Tmp1 = LegalizeOp(Node->getOperand(0));
1662 Tmp2 = LegalizeOp(Node->getOperand(1));
1663 Tmp3 = LegalizeOp(Node->getOperand(2));
1664
1665 switch (TLI.getOperationAction(Node->getOpcode(),
1666 Node->getOperand(1).getValueType())) {
1667 case TargetLowering::Custom:
1668 default: assert(0 && "This action not implemented for this operation!");
1669 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001670 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001671 break;
1672 case TargetLowering::Expand:
1673 // Replace this with a store to memory.
1674 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1675 Node->getOperand(1), Node->getOperand(2),
1676 DAG.getSrcValue(NULL));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001677 break;
1678 }
1679 break;
1680
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001681 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001682 case ISD::SUB_PARTS:
1683 case ISD::SHL_PARTS:
1684 case ISD::SRA_PARTS:
1685 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001686 std::vector<SDOperand> Ops;
1687 bool Changed = false;
1688 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1689 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1690 Changed |= Ops.back() != Node->getOperand(i);
1691 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001692 if (Changed)
1693 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001694
Evan Cheng870e4f82006-01-09 18:31:59 +00001695 switch (TLI.getOperationAction(Node->getOpcode(),
1696 Node->getValueType(0))) {
1697 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001698 case TargetLowering::Legal: break;
1699 case TargetLowering::Custom:
1700 Tmp1 = TLI.LowerOperation(Result, DAG);
1701 if (Tmp1.Val) {
1702 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001703 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001704 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001705 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1706 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001707 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001708 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001709 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001710 return RetVal;
1711 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001712 break;
1713 }
1714
Chris Lattner13fe99c2005-04-02 05:00:07 +00001715 // Since these produce multiple values, make sure to remember that we
1716 // legalized all of them.
1717 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1718 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1719 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001720 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001721
1722 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001723 case ISD::ADD:
1724 case ISD::SUB:
1725 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001726 case ISD::MULHS:
1727 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001728 case ISD::UDIV:
1729 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001730 case ISD::AND:
1731 case ISD::OR:
1732 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001733 case ISD::SHL:
1734 case ISD::SRL:
1735 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001736 case ISD::FADD:
1737 case ISD::FSUB:
1738 case ISD::FMUL:
1739 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001740 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001741 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1742 case Expand: assert(0 && "Not possible");
1743 case Legal:
1744 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1745 break;
1746 case Promote:
1747 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1748 break;
1749 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001750
1751 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001752
Andrew Lenharth72594262005-12-24 23:42:32 +00001753 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001754 default: assert(0 && "Operation not supported");
1755 case TargetLowering::Legal: break;
1756 case TargetLowering::Custom:
1757 Tmp1 = TLI.LowerOperation(Result, DAG);
1758 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001759 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001760 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001761 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001762
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001763 case ISD::BUILD_PAIR: {
1764 MVT::ValueType PairTy = Node->getValueType(0);
1765 // TODO: handle the case where the Lo and Hi operands are not of legal type
1766 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1767 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1768 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001769 case TargetLowering::Promote:
1770 case TargetLowering::Custom:
1771 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001772 case TargetLowering::Legal:
1773 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1774 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1775 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001776 case TargetLowering::Expand:
1777 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1778 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1779 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1780 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1781 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001782 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001783 break;
1784 }
1785 break;
1786 }
1787
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001788 case ISD::UREM:
1789 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001790 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001791 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1792 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001793
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001794 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001795 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1796 case TargetLowering::Custom:
1797 isCustom = true;
1798 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001799 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001800 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001801 if (isCustom) {
1802 Tmp1 = TLI.LowerOperation(Result, DAG);
1803 if (Tmp1.Val) Result = Tmp1;
1804 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001805 break;
Chris Lattner81914422005-08-03 20:31:37 +00001806 case TargetLowering::Expand:
1807 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001808 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00001809 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001810 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00001811 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1812 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1813 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1814 } else {
1815 // Floating point mod -> fmod libcall.
1816 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1817 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00001818 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001819 }
1820 break;
1821 }
1822 break;
Nate Begemane74795c2006-01-25 18:21:52 +00001823 case ISD::VAARG: {
1824 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1825 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1826
Chris Lattner364b89a2006-01-28 07:42:08 +00001827 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00001828 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1829 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001830 case TargetLowering::Custom:
1831 isCustom = true;
1832 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001833 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001834 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1835 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001836 Tmp1 = Result.getValue(1);
1837
1838 if (isCustom) {
1839 Tmp2 = TLI.LowerOperation(Result, DAG);
1840 if (Tmp2.Val) {
1841 Result = LegalizeOp(Tmp2);
1842 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1843 }
1844 }
Nate Begemane74795c2006-01-25 18:21:52 +00001845 break;
1846 case TargetLowering::Expand: {
1847 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1848 Node->getOperand(2));
1849 // Increment the pointer, VAList, to the next vaarg
1850 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1851 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1852 TLI.getPointerTy()));
1853 // Store the incremented VAList to the legalized pointer
1854 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1855 Node->getOperand(2));
1856 // Load the actual argument out of the pointer VAList
1857 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001858 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00001859 Result = LegalizeOp(Result);
1860 break;
1861 }
1862 }
1863 // Since VAARG produces two values, make sure to remember that we
1864 // legalized both of them.
1865 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001866 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1867 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00001868 }
1869
1870 case ISD::VACOPY:
1871 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1872 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1873 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1874
1875 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1876 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001877 case TargetLowering::Custom:
1878 isCustom = true;
1879 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001880 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001881 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1882 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001883 if (isCustom) {
1884 Tmp1 = TLI.LowerOperation(Result, DAG);
1885 if (Tmp1.Val) Result = Tmp1;
1886 }
Nate Begemane74795c2006-01-25 18:21:52 +00001887 break;
1888 case TargetLowering::Expand:
1889 // This defaults to loading a pointer from the input and storing it to the
1890 // output, returning the chain.
1891 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
1892 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
1893 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00001894 break;
1895 }
1896 break;
1897
1898 case ISD::VAEND:
1899 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1900 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1901
1902 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
1903 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001904 case TargetLowering::Custom:
1905 isCustom = true;
1906 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001907 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001908 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001909 if (isCustom) {
1910 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
1911 if (Tmp1.Val) Result = Tmp1;
1912 }
Nate Begemane74795c2006-01-25 18:21:52 +00001913 break;
1914 case TargetLowering::Expand:
1915 Result = Tmp1; // Default to a no-op, return the chain
1916 break;
1917 }
1918 break;
1919
1920 case ISD::VASTART:
1921 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1922 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1923
Chris Lattnerd02b0542006-01-28 10:58:55 +00001924 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1925
Nate Begemane74795c2006-01-25 18:21:52 +00001926 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
1927 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001928 case TargetLowering::Legal: break;
1929 case TargetLowering::Custom:
1930 Tmp1 = TLI.LowerOperation(Result, DAG);
1931 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00001932 break;
1933 }
1934 break;
1935
Nate Begeman1b8121b2006-01-11 21:21:00 +00001936 case ISD::ROTL:
1937 case ISD::ROTR:
1938 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1939 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001940
1941 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
1942 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001943 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00001944 break;
1945
Nate Begeman2fba8a32006-01-14 03:14:10 +00001946 case ISD::BSWAP:
1947 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1948 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001949 case TargetLowering::Custom:
1950 assert(0 && "Cannot custom legalize this yet!");
1951 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001952 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001953 break;
1954 case TargetLowering::Promote: {
1955 MVT::ValueType OVT = Tmp1.getValueType();
1956 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1957 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00001958
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001959 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1960 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
1961 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
1962 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
1963 break;
1964 }
1965 case TargetLowering::Expand:
1966 Result = ExpandBSWAP(Tmp1);
1967 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00001968 }
1969 break;
1970
Andrew Lenharth5e177822005-05-03 17:19:30 +00001971 case ISD::CTPOP:
1972 case ISD::CTTZ:
1973 case ISD::CTLZ:
1974 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1975 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001976 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00001977 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001978 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00001979 break;
1980 case TargetLowering::Promote: {
1981 MVT::ValueType OVT = Tmp1.getValueType();
1982 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001983
1984 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001985 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1986 // Perform the larger operation, then subtract if needed.
1987 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001988 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00001989 case ISD::CTPOP:
1990 Result = Tmp1;
1991 break;
1992 case ISD::CTTZ:
1993 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001994 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1995 DAG.getConstant(getSizeInBits(NVT), NVT),
1996 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001997 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001998 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1999 break;
2000 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002001 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002002 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2003 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002004 getSizeInBits(OVT), NVT));
2005 break;
2006 }
2007 break;
2008 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002009 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002010 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002011 break;
2012 }
2013 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002014
Chris Lattner13fe99c2005-04-02 05:00:07 +00002015 // Unary operators
2016 case ISD::FABS:
2017 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002018 case ISD::FSQRT:
2019 case ISD::FSIN:
2020 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002021 Tmp1 = LegalizeOp(Node->getOperand(0));
2022 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002023 case TargetLowering::Promote:
2024 case TargetLowering::Custom:
2025 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002026 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002027 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner13fe99c2005-04-02 05:00:07 +00002028 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002029 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002030 switch (Node->getOpcode()) {
2031 default: assert(0 && "Unreachable!");
2032 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002033 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2034 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002035 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002036 break;
Chris Lattner80026402005-04-30 04:43:14 +00002037 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002038 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2039 MVT::ValueType VT = Node->getValueType(0);
2040 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002041 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002042 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2043 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002044 break;
2045 }
2046 case ISD::FSQRT:
2047 case ISD::FSIN:
2048 case ISD::FCOS: {
2049 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002050 const char *FnName = 0;
2051 switch(Node->getOpcode()) {
2052 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2053 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2054 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2055 default: assert(0 && "Unreachable!");
2056 }
Nate Begeman77558da2005-08-04 21:43:28 +00002057 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002058 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002059 break;
2060 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002061 }
2062 break;
2063 }
2064 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002065
2066 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002067 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002068 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002069 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002070 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2071 Node->getOperand(0).getValueType())) {
2072 default: assert(0 && "Unknown operation action!");
2073 case TargetLowering::Expand:
2074 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2075 break;
2076 case TargetLowering::Legal:
2077 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002078 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002079 break;
2080 }
2081 }
2082 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002083 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002084 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002085 case ISD::UINT_TO_FP: {
2086 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002087 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2088 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002089 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002090 Node->getOperand(0).getValueType())) {
2091 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002092 case TargetLowering::Custom:
2093 isCustom = true;
2094 // FALLTHROUGH
2095 case TargetLowering::Legal:
2096 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002097 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002098 if (isCustom) {
2099 Tmp1 = TLI.LowerOperation(Result, DAG);
2100 if (Tmp1.Val) Result = Tmp1;
2101 }
2102 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002103 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002104 Result = ExpandLegalINT_TO_FP(isSigned,
2105 LegalizeOp(Node->getOperand(0)),
2106 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002107 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002108 case TargetLowering::Promote:
2109 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2110 Node->getValueType(0),
2111 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002112 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002113 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002114 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002115 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002116 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2117 Node->getValueType(0), Node->getOperand(0));
2118 break;
2119 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002120 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002121 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002122 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2123 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002124 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002125 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2126 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002127 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002128 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2129 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002130 break;
2131 }
2132 break;
2133 }
2134 case ISD::TRUNCATE:
2135 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2136 case Legal:
2137 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002138 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002139 break;
2140 case Expand:
2141 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2142
2143 // Since the result is legal, we should just be able to truncate the low
2144 // part of the source.
2145 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2146 break;
2147 case Promote:
2148 Result = PromoteOp(Node->getOperand(0));
2149 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2150 break;
2151 }
2152 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002153
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002154 case ISD::FP_TO_SINT:
2155 case ISD::FP_TO_UINT:
2156 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2157 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002158 Tmp1 = LegalizeOp(Node->getOperand(0));
2159
Chris Lattner44fe26f2005-07-29 00:11:56 +00002160 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2161 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002162 case TargetLowering::Custom:
2163 isCustom = true;
2164 // FALLTHROUGH
2165 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002166 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002167 if (isCustom) {
2168 Tmp1 = TLI.LowerOperation(Result, DAG);
2169 if (Tmp1.Val) Result = Tmp1;
2170 }
2171 break;
2172 case TargetLowering::Promote:
2173 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2174 Node->getOpcode() == ISD::FP_TO_SINT);
2175 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002176 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002177 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2178 SDOperand True, False;
2179 MVT::ValueType VT = Node->getOperand(0).getValueType();
2180 MVT::ValueType NVT = Node->getValueType(0);
2181 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2182 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2183 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2184 Node->getOperand(0), Tmp2, ISD::SETLT);
2185 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2186 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002187 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002188 Tmp2));
2189 False = DAG.getNode(ISD::XOR, NVT, False,
2190 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002191 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2192 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002193 } else {
2194 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2195 }
2196 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002197 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002198 break;
2199 case Expand:
2200 assert(0 && "Shouldn't need to expand other operators here!");
2201 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002202 Tmp1 = PromoteOp(Node->getOperand(0));
2203 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2204 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002205 break;
2206 }
2207 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002208
Chris Lattner7753f172005-09-02 00:18:10 +00002209 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002210 case ISD::ZERO_EXTEND:
2211 case ISD::SIGN_EXTEND:
2212 case ISD::FP_EXTEND:
2213 case ISD::FP_ROUND:
2214 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002215 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002216 case Legal:
2217 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002218 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002219 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002220 case Promote:
2221 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002222 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002223 Tmp1 = PromoteOp(Node->getOperand(0));
2224 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002225 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002226 case ISD::ZERO_EXTEND:
2227 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002228 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002229 Result = DAG.getZeroExtendInReg(Result,
2230 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002231 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002232 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002233 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002234 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002235 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002236 Result,
2237 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002238 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002239 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002240 Result = PromoteOp(Node->getOperand(0));
2241 if (Result.getValueType() != Op.getValueType())
2242 // Dynamically dead while we have only 2 FP types.
2243 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2244 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002245 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002246 Result = PromoteOp(Node->getOperand(0));
2247 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2248 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002249 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002250 }
2251 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002252 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002253 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002254 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002255 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002256
2257 // If this operation is not supported, convert it to a shl/shr or load/store
2258 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002259 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2260 default: assert(0 && "This action not supported for this op yet!");
2261 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002263 break;
2264 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002265 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002266 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002267 // NOTE: we could fall back on load/store here too for targets without
2268 // SAR. However, it is doubtful that any exist.
2269 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2270 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002271 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002272 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2273 Node->getOperand(0), ShiftCst);
2274 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2275 Result, ShiftCst);
2276 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2277 // The only way we can lower this is to turn it into a STORETRUNC,
2278 // EXTLOAD pair, targetting a temporary location (a stack slot).
2279
2280 // NOTE: there is a choice here between constantly creating new stack
2281 // slots and always reusing the same one. We currently always create
2282 // new ones, as reuse may inhibit scheduling.
2283 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2284 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2285 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2286 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002287 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002288 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2289 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2290 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002291 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002292 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002293 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2294 Result, StackSlot, DAG.getSrcValue(NULL),
2295 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002296 } else {
2297 assert(0 && "Unknown op");
2298 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002299 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002300 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002301 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002302 }
Chris Lattner99222f72005-01-15 07:15:18 +00002303 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002304
2305 // Make sure that the generated code is itself legal.
2306 if (Result != Op)
2307 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002308
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002309 // Note that LegalizeOp may be reentered even from single-use nodes, which
2310 // means that we always must cache transformed nodes.
2311 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002312 return Result;
2313}
2314
Chris Lattner4d978642005-01-15 22:16:26 +00002315/// PromoteOp - Given an operation that produces a value in an invalid type,
2316/// promote it to compute the value into a larger type. The produced value will
2317/// have the correct bits for the low portion of the register, but no guarantee
2318/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002319SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2320 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002321 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002322 assert(getTypeAction(VT) == Promote &&
2323 "Caller should expand or legalize operands that are not promotable!");
2324 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2325 "Cannot promote to smaller type!");
2326
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002327 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002328 SDOperand Result;
2329 SDNode *Node = Op.Val;
2330
Chris Lattner1a570f12005-09-02 20:32:45 +00002331 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2332 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002333
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002334 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002335 case ISD::CopyFromReg:
2336 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002337 default:
2338 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2339 assert(0 && "Do not know how to promote this operator!");
2340 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002341 case ISD::UNDEF:
2342 Result = DAG.getNode(ISD::UNDEF, NVT);
2343 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002344 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002345 if (VT != MVT::i1)
2346 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2347 else
2348 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002349 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2350 break;
2351 case ISD::ConstantFP:
2352 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2353 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2354 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002355
Chris Lattner2cb338d2005-01-18 02:59:52 +00002356 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002357 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002358 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2359 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002360 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002361
2362 case ISD::TRUNCATE:
2363 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2364 case Legal:
2365 Result = LegalizeOp(Node->getOperand(0));
2366 assert(Result.getValueType() >= NVT &&
2367 "This truncation doesn't make sense!");
2368 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2369 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2370 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002371 case Promote:
2372 // The truncation is not required, because we don't guarantee anything
2373 // about high bits anyway.
2374 Result = PromoteOp(Node->getOperand(0));
2375 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002376 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002377 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2378 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002379 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002380 }
2381 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002382 case ISD::SIGN_EXTEND:
2383 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002384 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002385 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2386 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2387 case Legal:
2388 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002389 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002390 break;
2391 case Promote:
2392 // Promote the reg if it's smaller.
2393 Result = PromoteOp(Node->getOperand(0));
2394 // The high bits are not guaranteed to be anything. Insert an extend.
2395 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002396 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002397 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002398 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002399 Result = DAG.getZeroExtendInReg(Result,
2400 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002401 break;
2402 }
2403 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002404 case ISD::BIT_CONVERT:
2405 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2406 Result = PromoteOp(Result);
2407 break;
2408
Chris Lattner4d978642005-01-15 22:16:26 +00002409 case ISD::FP_EXTEND:
2410 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2411 case ISD::FP_ROUND:
2412 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2413 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2414 case Promote: assert(0 && "Unreachable with 2 FP types!");
2415 case Legal:
2416 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002417 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002418 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002419 break;
2420 }
2421 break;
2422
2423 case ISD::SINT_TO_FP:
2424 case ISD::UINT_TO_FP:
2425 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2426 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002427 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002428 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002429 break;
2430
2431 case Promote:
2432 Result = PromoteOp(Node->getOperand(0));
2433 if (Node->getOpcode() == ISD::SINT_TO_FP)
2434 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002435 Result,
2436 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002437 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002438 Result = DAG.getZeroExtendInReg(Result,
2439 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002440 // No extra round required here.
2441 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002442 break;
2443 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002444 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2445 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002446 // Round if we cannot tolerate excess precision.
2447 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002448 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2449 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002450 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002451 }
Chris Lattner4d978642005-01-15 22:16:26 +00002452 break;
2453
Chris Lattner268d4572005-12-09 17:32:47 +00002454 case ISD::SIGN_EXTEND_INREG:
2455 Result = PromoteOp(Node->getOperand(0));
2456 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2457 Node->getOperand(1));
2458 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002459 case ISD::FP_TO_SINT:
2460 case ISD::FP_TO_UINT:
2461 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2462 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002463 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002464 break;
2465 case Promote:
2466 // The input result is prerounded, so we don't have to do anything
2467 // special.
2468 Tmp1 = PromoteOp(Node->getOperand(0));
2469 break;
2470 case Expand:
2471 assert(0 && "not implemented");
2472 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002473 // If we're promoting a UINT to a larger size, check to see if the new node
2474 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2475 // we can use that instead. This allows us to generate better code for
2476 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2477 // legal, such as PowerPC.
2478 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002479 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002480 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2481 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002482 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2483 } else {
2484 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2485 }
Chris Lattner4d978642005-01-15 22:16:26 +00002486 break;
2487
Chris Lattner13fe99c2005-04-02 05:00:07 +00002488 case ISD::FABS:
2489 case ISD::FNEG:
2490 Tmp1 = PromoteOp(Node->getOperand(0));
2491 assert(Tmp1.getValueType() == NVT);
2492 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2493 // NOTE: we do not have to do any extra rounding here for
2494 // NoExcessFPPrecision, because we know the input will have the appropriate
2495 // precision, and these operations don't modify precision at all.
2496 break;
2497
Chris Lattner9d6fa982005-04-28 21:44:33 +00002498 case ISD::FSQRT:
2499 case ISD::FSIN:
2500 case ISD::FCOS:
2501 Tmp1 = PromoteOp(Node->getOperand(0));
2502 assert(Tmp1.getValueType() == NVT);
2503 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002504 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002505 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2506 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002507 break;
2508
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002509 case ISD::AND:
2510 case ISD::OR:
2511 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002512 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002513 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002514 case ISD::MUL:
2515 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002516 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002517 // that too is okay if they are integer operations.
2518 Tmp1 = PromoteOp(Node->getOperand(0));
2519 Tmp2 = PromoteOp(Node->getOperand(1));
2520 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2521 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002522 break;
2523 case ISD::FADD:
2524 case ISD::FSUB:
2525 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002526 Tmp1 = PromoteOp(Node->getOperand(0));
2527 Tmp2 = PromoteOp(Node->getOperand(1));
2528 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2529 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2530
2531 // Floating point operations will give excess precision that we may not be
2532 // able to tolerate. If we DO allow excess precision, just leave it,
2533 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002534 // FIXME: Why would we need to round FP ops more than integer ones?
2535 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002536 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002537 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2538 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002539 break;
2540
Chris Lattner4d978642005-01-15 22:16:26 +00002541 case ISD::SDIV:
2542 case ISD::SREM:
2543 // These operators require that their input be sign extended.
2544 Tmp1 = PromoteOp(Node->getOperand(0));
2545 Tmp2 = PromoteOp(Node->getOperand(1));
2546 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002547 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2548 DAG.getValueType(VT));
2549 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2550 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002551 }
2552 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2553
2554 // Perform FP_ROUND: this is probably overly pessimistic.
2555 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002556 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2557 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002558 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002559 case ISD::FDIV:
2560 case ISD::FREM:
2561 // These operators require that their input be fp extended.
2562 Tmp1 = PromoteOp(Node->getOperand(0));
2563 Tmp2 = PromoteOp(Node->getOperand(1));
2564 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2565
2566 // Perform FP_ROUND: this is probably overly pessimistic.
2567 if (NoExcessFPPrecision)
2568 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2569 DAG.getValueType(VT));
2570 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002571
2572 case ISD::UDIV:
2573 case ISD::UREM:
2574 // These operators require that their input be zero extended.
2575 Tmp1 = PromoteOp(Node->getOperand(0));
2576 Tmp2 = PromoteOp(Node->getOperand(1));
2577 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002578 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2579 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002580 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2581 break;
2582
2583 case ISD::SHL:
2584 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002585 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002586 break;
2587 case ISD::SRA:
2588 // The input value must be properly sign extended.
2589 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002590 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2591 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002592 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002593 break;
2594 case ISD::SRL:
2595 // The input value must be properly zero extended.
2596 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002597 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002598 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002599 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002600
2601 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002602 Tmp1 = Node->getOperand(0); // Get the chain.
2603 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002604 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2605 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2606 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2607 } else {
2608 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2609 Node->getOperand(2));
2610 // Increment the pointer, VAList, to the next vaarg
2611 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2612 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2613 TLI.getPointerTy()));
2614 // Store the incremented VAList to the legalized pointer
2615 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2616 Node->getOperand(2));
2617 // Load the actual argument out of the pointer VAList
2618 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2619 DAG.getSrcValue(0), VT);
2620 }
2621 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002622 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002623 break;
2624
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002625 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002626 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2627 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002628 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002629 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002630 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002631 case ISD::SEXTLOAD:
2632 case ISD::ZEXTLOAD:
2633 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002634 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2635 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002636 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002637 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002638 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002639 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002640 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002641 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2642 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002643 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002644 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002645 case ISD::SELECT_CC:
2646 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2647 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2648 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002649 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002650 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002651 case ISD::BSWAP:
2652 Tmp1 = Node->getOperand(0);
2653 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2654 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2655 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2656 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2657 TLI.getShiftAmountTy()));
2658 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002659 case ISD::CTPOP:
2660 case ISD::CTTZ:
2661 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002662 // Zero extend the argument
2663 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002664 // Perform the larger operation, then subtract if needed.
2665 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002666 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002667 case ISD::CTPOP:
2668 Result = Tmp1;
2669 break;
2670 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002671 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002672 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002673 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002674 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002675 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002676 break;
2677 case ISD::CTLZ:
2678 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002679 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2680 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002681 getSizeInBits(VT), NVT));
2682 break;
2683 }
2684 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002685 }
2686
2687 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002688
2689 // Make sure the result is itself legal.
2690 Result = LegalizeOp(Result);
2691
2692 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002693 AddPromotedOperand(Op, Result);
2694 return Result;
2695}
Chris Lattnerdc750592005-01-07 07:47:09 +00002696
Chris Lattner36e663d2005-12-23 00:16:34 +00002697/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002698/// The resultant code need not be legal. Note that SrcOp is the input operand
2699/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00002700SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2701 SDOperand SrcOp) {
2702 // Create the stack frame object.
2703 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2704 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002705 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner36e663d2005-12-23 00:16:34 +00002706 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2707
2708 // Emit a store to the stack slot.
2709 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00002710 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00002711 // Result is a load from the stack slot.
2712 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2713}
2714
Chris Lattner4157c412005-04-02 04:00:59 +00002715void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2716 SDOperand Op, SDOperand Amt,
2717 SDOperand &Lo, SDOperand &Hi) {
2718 // Expand the subcomponents.
2719 SDOperand LHSL, LHSH;
2720 ExpandOp(Op, LHSL, LHSH);
2721
2722 std::vector<SDOperand> Ops;
2723 Ops.push_back(LHSL);
2724 Ops.push_back(LHSH);
2725 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002726 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002727 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002728 Hi = Lo.getValue(1);
2729}
2730
2731
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002732/// ExpandShift - Try to find a clever way to expand this shift operation out to
2733/// smaller elements. If we can't find a way that is more efficient than a
2734/// libcall on this target, return false. Otherwise, return true with the
2735/// low-parts expanded into Lo and Hi.
2736bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2737 SDOperand &Lo, SDOperand &Hi) {
2738 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2739 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002740
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002741 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002742 SDOperand ShAmt = LegalizeOp(Amt);
2743 MVT::ValueType ShTy = ShAmt.getValueType();
2744 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2745 unsigned NVTBits = MVT::getSizeInBits(NVT);
2746
2747 // Handle the case when Amt is an immediate. Other cases are currently broken
2748 // and are disabled.
2749 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2750 unsigned Cst = CN->getValue();
2751 // Expand the incoming operand to be shifted, so that we have its parts
2752 SDOperand InL, InH;
2753 ExpandOp(Op, InL, InH);
2754 switch(Opc) {
2755 case ISD::SHL:
2756 if (Cst > VTBits) {
2757 Lo = DAG.getConstant(0, NVT);
2758 Hi = DAG.getConstant(0, NVT);
2759 } else if (Cst > NVTBits) {
2760 Lo = DAG.getConstant(0, NVT);
2761 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002762 } else if (Cst == NVTBits) {
2763 Lo = DAG.getConstant(0, NVT);
2764 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002765 } else {
2766 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2767 Hi = DAG.getNode(ISD::OR, NVT,
2768 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2769 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2770 }
2771 return true;
2772 case ISD::SRL:
2773 if (Cst > VTBits) {
2774 Lo = DAG.getConstant(0, NVT);
2775 Hi = DAG.getConstant(0, NVT);
2776 } else if (Cst > NVTBits) {
2777 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2778 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002779 } else if (Cst == NVTBits) {
2780 Lo = InH;
2781 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002782 } else {
2783 Lo = DAG.getNode(ISD::OR, NVT,
2784 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2785 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2786 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2787 }
2788 return true;
2789 case ISD::SRA:
2790 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002791 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002792 DAG.getConstant(NVTBits-1, ShTy));
2793 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002794 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002795 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002796 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002797 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002798 } else if (Cst == NVTBits) {
2799 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002800 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002801 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002802 } else {
2803 Lo = DAG.getNode(ISD::OR, NVT,
2804 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2805 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2806 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2807 }
2808 return true;
2809 }
2810 }
Nate Begemanb0674922005-04-06 21:13:14 +00002811 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002812}
Chris Lattneraac464e2005-01-21 06:05:23 +00002813
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002814/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2815/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002816/// Found.
Chris Lattner90ba5442006-01-09 23:21:49 +00002817static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found,
2818 std::set<SDNode*> &Visited) {
Chris Lattner15afe462006-01-20 18:40:10 +00002819 if (Node->getNodeDepth() <= Found->getNodeDepth() ||
Chris Lattner90ba5442006-01-09 23:21:49 +00002820 !Visited.insert(Node).second) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002821
Chris Lattner2dce7032005-05-12 23:24:06 +00002822 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002823 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002824 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002825 Found = Node;
2826 return;
2827 }
2828
2829 // Otherwise, scan the operands of Node to see if any of them is a call.
2830 assert(Node->getNumOperands() != 0 &&
2831 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002832 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner90ba5442006-01-09 23:21:49 +00002833 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002834
2835 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002836 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner90ba5442006-01-09 23:21:49 +00002837 Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002838}
2839
2840
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002841/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2842/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002843/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002844static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2845 std::set<SDNode*> &Visited) {
Chris Lattner15afe462006-01-20 18:40:10 +00002846 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
Chris Lattner96ad3132005-08-05 18:10:27 +00002847 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002848
Chris Lattner2dce7032005-05-12 23:24:06 +00002849 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002850 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002851 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002852 Found = Node;
2853 return;
2854 }
2855
2856 // Otherwise, scan the operands of Node to see if any of them is a call.
2857 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2858 if (UI == E) return;
2859 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002860 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002861
2862 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002863 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002864}
2865
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002866/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002867/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002868static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002869 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002870 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002871 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002872 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002873
Chris Lattner02011c92006-01-14 22:41:46 +00002874 // The chain is usually at the end.
Chris Lattner4add7e32005-01-23 04:42:50 +00002875 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner02011c92006-01-14 22:41:46 +00002876 if (TheChain.getValueType() != MVT::Other) {
2877 // Sometimes it's at the beginning.
Chris Lattner3268f242005-05-14 08:34:53 +00002878 TheChain = SDOperand(Node, 0);
Chris Lattner02011c92006-01-14 22:41:46 +00002879 if (TheChain.getValueType() != MVT::Other) {
2880 // Otherwise, hunt for it.
2881 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
2882 if (Node->getValueType(i) == MVT::Other) {
2883 TheChain = SDOperand(Node, i);
2884 break;
2885 }
2886
2887 // Otherwise, we walked into a node without a chain.
2888 if (TheChain.getValueType() != MVT::Other)
2889 return 0;
2890 }
2891 }
Misha Brukman835702a2005-04-21 22:36:52 +00002892
2893 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002894 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002895
Chris Lattner4add7e32005-01-23 04:42:50 +00002896 // Make sure to only follow users of our token chain.
2897 SDNode *User = *UI;
2898 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2899 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002900 if (SDNode *Result = FindCallSeqEnd(User))
2901 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002902 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002903 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002904}
2905
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002906/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002907/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002908static SDNode *FindCallSeqStart(SDNode *Node) {
2909 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002910 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002911
2912 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2913 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002914 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002915}
2916
2917
Chris Lattner4add7e32005-01-23 04:42:50 +00002918/// FindInputOutputChains - If we are replacing an operation with a call we need
2919/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002920/// properly serialize the calls in the block. The returned operand is the
2921/// input chain value for the new call (e.g. the entry node or the previous
2922/// call), and OutChain is set to be the chain node to update to point to the
2923/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002924static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2925 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002926 SDNode *LatestCallSeqStart = Entry.Val;
2927 SDNode *LatestCallSeqEnd = 0;
Chris Lattner90ba5442006-01-09 23:21:49 +00002928 std::set<SDNode*> Visited;
2929 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited);
2930 Visited.clear();
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002931 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002932
Chris Lattner2dce7032005-05-12 23:24:06 +00002933 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002934 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002935 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2936 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00002937 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002938 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00002939 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2940 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002941 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00002942 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002943 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002944
Chris Lattner06bbeb62005-05-11 19:02:11 +00002945 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002946 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002947 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002948 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner90ba5442006-01-09 23:21:49 +00002949 Visited.clear();
Chris Lattner4add7e32005-01-23 04:42:50 +00002950
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002951 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002952 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002953 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002954
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002955 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002956}
2957
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002958/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002959void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2960 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002961 // Nothing to splice it into?
2962 if (OutChain == 0) return;
2963
2964 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2965 //OutChain->dump();
2966
2967 // Form a token factor node merging the old inval and the new inval.
2968 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2969 OutChain->getOperand(0));
2970 // Change the node to refer to the new token.
Chris Lattnerccb44762006-01-29 07:58:15 +00002971 std::vector<SDOperand> Ops(OutChain->op_begin(), OutChain->op_end());
2972 Ops[0] = InToken;
2973 SDOperand Res = DAG.UpdateNodeOperands(SDOperand(OutChain, 0), Ops);
2974 assert(Res.Val == OutChain && "Didn't update in place!");
Chris Lattner06bbeb62005-05-11 19:02:11 +00002975}
Chris Lattner4add7e32005-01-23 04:42:50 +00002976
2977
Chris Lattneraac464e2005-01-21 06:05:23 +00002978// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2979// does not fit into a register, return the lo part and set the hi part to the
2980// by-reg argument. If it does fit into a single register, return the result
2981// and leave the Hi part unset.
2982SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2983 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002984 SDNode *OutChain;
2985 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2986 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002987 if (InChain.Val == 0)
2988 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002989
Chris Lattneraac464e2005-01-21 06:05:23 +00002990 TargetLowering::ArgListTy Args;
2991 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2992 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2993 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2994 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2995 }
2996 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002997
Chris Lattner06bbeb62005-05-11 19:02:11 +00002998 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002999 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003000 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003001 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3002 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003003
Chris Lattner63022662005-09-02 20:26:58 +00003004 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003005 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003006 default: assert(0 && "Unknown thing");
3007 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003008 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003009 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003010 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003011 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003012 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003013 }
Chris Lattner10f67752006-01-28 04:28:26 +00003014
3015 CallInfo.second = LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003016 SpliceCallInto(CallInfo.second, OutChain);
Chris Lattner63022662005-09-02 20:26:58 +00003017 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003018}
3019
Chris Lattner4add7e32005-01-23 04:42:50 +00003020
Chris Lattneraac464e2005-01-21 06:05:23 +00003021/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3022/// destination type is legal.
3023SDOperand SelectionDAGLegalize::
3024ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003025 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003026 assert(getTypeAction(Source.getValueType()) == Expand &&
3027 "This is not an expansion!");
3028 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3029
Chris Lattner06bbeb62005-05-11 19:02:11 +00003030 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003031 assert(Source.getValueType() == MVT::i64 &&
3032 "This only works for 64-bit -> FP");
3033 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3034 // incoming integer is set. To handle this, we dynamically test to see if
3035 // it is set, and, if so, add a fudge factor.
3036 SDOperand Lo, Hi;
3037 ExpandOp(Source, Lo, Hi);
3038
Chris Lattner2a4f7312005-05-13 04:45:13 +00003039 // If this is unsigned, and not supported, first perform the conversion to
3040 // signed, then adjust the result if the sign bit is set.
3041 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3042 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3043
Chris Lattnerd47675e2005-08-09 20:20:18 +00003044 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3045 DAG.getConstant(0, Hi.getValueType()),
3046 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003047 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3048 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3049 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003050 uint64_t FF = 0x5f800000ULL;
3051 if (TLI.isLittleEndian()) FF <<= 32;
3052 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003053
Chris Lattnerc30405e2005-08-26 17:15:30 +00003054 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003055 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3056 SDOperand FudgeInReg;
3057 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003058 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3059 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003060 else {
3061 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003062 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3063 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003064 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003065 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003066 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003067
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003068 // Check to see if the target has a custom way to lower this. If so, use it.
3069 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3070 default: assert(0 && "This action not implemented for this operation!");
3071 case TargetLowering::Legal:
3072 case TargetLowering::Expand:
3073 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003074 case TargetLowering::Custom: {
3075 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3076 Source), DAG);
3077 if (NV.Val)
3078 return LegalizeOp(NV);
3079 break; // The target decided this was legal after all
3080 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003081 }
3082
Chris Lattner153587e2005-05-12 07:00:44 +00003083 // Expand the source, then glue it back together for the call. We must expand
3084 // the source in case it is shared (this pass of legalize must traverse it).
3085 SDOperand SrcLo, SrcHi;
3086 ExpandOp(Source, SrcLo, SrcHi);
3087 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3088
Chris Lattner06bbeb62005-05-11 19:02:11 +00003089 SDNode *OutChain = 0;
3090 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3091 DAG.getEntryNode());
3092 const char *FnName = 0;
3093 if (DestTy == MVT::f32)
3094 FnName = "__floatdisf";
3095 else {
3096 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3097 FnName = "__floatdidf";
3098 }
3099
Chris Lattneraac464e2005-01-21 06:05:23 +00003100 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3101
3102 TargetLowering::ArgListTy Args;
3103 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003104
Chris Lattneraac464e2005-01-21 06:05:23 +00003105 Args.push_back(std::make_pair(Source, ArgTy));
3106
3107 // We don't care about token chains for libcalls. We just use the entry
3108 // node as our input and ignore the output chain. This allows us to place
3109 // calls wherever we need them to satisfy data dependences.
3110 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003111
3112 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003113 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3114 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003115
Chris Lattnera5bf1032005-05-12 04:49:08 +00003116 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003117 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003118}
Misha Brukman835702a2005-04-21 22:36:52 +00003119
Chris Lattner689bdcc2006-01-28 08:25:58 +00003120/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3121/// INT_TO_FP operation of the specified operand when the target requests that
3122/// we expand it. At this point, we know that the result and operand types are
3123/// legal for the target.
3124SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3125 SDOperand Op0,
3126 MVT::ValueType DestVT) {
3127 if (Op0.getValueType() == MVT::i32) {
3128 // simple 32-bit [signed|unsigned] integer to float/double expansion
3129
3130 // get the stack frame index of a 8 byte buffer
3131 MachineFunction &MF = DAG.getMachineFunction();
3132 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3133 // get address of 8 byte buffer
3134 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3135 // word offset constant for Hi/Lo address computation
3136 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3137 // set up Hi and Lo (into buffer) address based on endian
3138 SDOperand Hi, Lo;
3139 if (TLI.isLittleEndian()) {
3140 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3141 Lo = StackSlot;
3142 } else {
3143 Hi = StackSlot;
3144 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3145 }
3146 // if signed map to unsigned space
3147 SDOperand Op0Mapped;
3148 if (isSigned) {
3149 // constant used to invert sign bit (signed to unsigned mapping)
3150 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3151 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3152 } else {
3153 Op0Mapped = Op0;
3154 }
3155 // store the lo of the constructed double - based on integer input
3156 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3157 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3158 // initial hi portion of constructed double
3159 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3160 // store the hi of the constructed double - biased exponent
3161 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3162 InitialHi, Hi, DAG.getSrcValue(NULL));
3163 // load the constructed double
3164 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3165 DAG.getSrcValue(NULL));
3166 // FP constant to bias correct the final result
3167 SDOperand Bias = DAG.getConstantFP(isSigned ?
3168 BitsToDouble(0x4330000080000000ULL)
3169 : BitsToDouble(0x4330000000000000ULL),
3170 MVT::f64);
3171 // subtract the bias
3172 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3173 // final result
3174 SDOperand Result;
3175 // handle final rounding
3176 if (DestVT == MVT::f64) {
3177 // do nothing
3178 Result = Sub;
3179 } else {
3180 // if f32 then cast to f32
3181 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3182 }
3183 return Result;
3184 }
3185 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3186 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3187
3188 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3189 DAG.getConstant(0, Op0.getValueType()),
3190 ISD::SETLT);
3191 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3192 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3193 SignSet, Four, Zero);
3194
3195 // If the sign bit of the integer is set, the large number will be treated
3196 // as a negative number. To counteract this, the dynamic code adds an
3197 // offset depending on the data type.
3198 uint64_t FF;
3199 switch (Op0.getValueType()) {
3200 default: assert(0 && "Unsupported integer type!");
3201 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3202 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3203 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3204 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3205 }
3206 if (TLI.isLittleEndian()) FF <<= 32;
3207 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3208
3209 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3210 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3211 SDOperand FudgeInReg;
3212 if (DestVT == MVT::f32)
3213 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3214 DAG.getSrcValue(NULL));
3215 else {
3216 assert(DestVT == MVT::f64 && "Unexpected conversion");
3217 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3218 DAG.getEntryNode(), CPIdx,
3219 DAG.getSrcValue(NULL), MVT::f32));
3220 }
3221
3222 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3223}
3224
3225/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3226/// *INT_TO_FP operation of the specified operand when the target requests that
3227/// we promote it. At this point, we know that the result and operand types are
3228/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3229/// operation that takes a larger input.
3230SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3231 MVT::ValueType DestVT,
3232 bool isSigned) {
3233 // First step, figure out the appropriate *INT_TO_FP operation to use.
3234 MVT::ValueType NewInTy = LegalOp.getValueType();
3235
3236 unsigned OpToUse = 0;
3237
3238 // Scan for the appropriate larger type to use.
3239 while (1) {
3240 NewInTy = (MVT::ValueType)(NewInTy+1);
3241 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3242
3243 // If the target supports SINT_TO_FP of this type, use it.
3244 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3245 default: break;
3246 case TargetLowering::Legal:
3247 if (!TLI.isTypeLegal(NewInTy))
3248 break; // Can't use this datatype.
3249 // FALL THROUGH.
3250 case TargetLowering::Custom:
3251 OpToUse = ISD::SINT_TO_FP;
3252 break;
3253 }
3254 if (OpToUse) break;
3255 if (isSigned) continue;
3256
3257 // If the target supports UINT_TO_FP of this type, use it.
3258 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3259 default: break;
3260 case TargetLowering::Legal:
3261 if (!TLI.isTypeLegal(NewInTy))
3262 break; // Can't use this datatype.
3263 // FALL THROUGH.
3264 case TargetLowering::Custom:
3265 OpToUse = ISD::UINT_TO_FP;
3266 break;
3267 }
3268 if (OpToUse) break;
3269
3270 // Otherwise, try a larger type.
3271 }
3272
3273 // Okay, we found the operation and type to use. Zero extend our input to the
3274 // desired type then run the operation on it.
3275 return DAG.getNode(OpToUse, DestVT,
3276 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3277 NewInTy, LegalOp));
3278}
3279
3280/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3281/// FP_TO_*INT operation of the specified operand when the target requests that
3282/// we promote it. At this point, we know that the result and operand types are
3283/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3284/// operation that returns a larger result.
3285SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3286 MVT::ValueType DestVT,
3287 bool isSigned) {
3288 // First step, figure out the appropriate FP_TO*INT operation to use.
3289 MVT::ValueType NewOutTy = DestVT;
3290
3291 unsigned OpToUse = 0;
3292
3293 // Scan for the appropriate larger type to use.
3294 while (1) {
3295 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3296 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3297
3298 // If the target supports FP_TO_SINT returning this type, use it.
3299 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3300 default: break;
3301 case TargetLowering::Legal:
3302 if (!TLI.isTypeLegal(NewOutTy))
3303 break; // Can't use this datatype.
3304 // FALL THROUGH.
3305 case TargetLowering::Custom:
3306 OpToUse = ISD::FP_TO_SINT;
3307 break;
3308 }
3309 if (OpToUse) break;
3310
3311 // If the target supports FP_TO_UINT of this type, use it.
3312 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3313 default: break;
3314 case TargetLowering::Legal:
3315 if (!TLI.isTypeLegal(NewOutTy))
3316 break; // Can't use this datatype.
3317 // FALL THROUGH.
3318 case TargetLowering::Custom:
3319 OpToUse = ISD::FP_TO_UINT;
3320 break;
3321 }
3322 if (OpToUse) break;
3323
3324 // Otherwise, try a larger type.
3325 }
3326
3327 // Okay, we found the operation and type to use. Truncate the result of the
3328 // extended FP_TO_*INT operation to the desired size.
3329 return DAG.getNode(ISD::TRUNCATE, DestVT,
3330 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3331}
3332
3333/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3334///
3335SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3336 MVT::ValueType VT = Op.getValueType();
3337 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3338 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3339 switch (VT) {
3340 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3341 case MVT::i16:
3342 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3343 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3344 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3345 case MVT::i32:
3346 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3347 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3348 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3349 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3350 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3351 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3352 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3353 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3354 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3355 case MVT::i64:
3356 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3357 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3358 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3359 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3360 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3361 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3362 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3363 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3364 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3365 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3366 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3367 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3368 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3369 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3370 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3371 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3372 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3373 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3374 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3375 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3376 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3377 }
3378}
3379
3380/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3381///
3382SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3383 switch (Opc) {
3384 default: assert(0 && "Cannot expand this yet!");
3385 case ISD::CTPOP: {
3386 static const uint64_t mask[6] = {
3387 0x5555555555555555ULL, 0x3333333333333333ULL,
3388 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3389 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3390 };
3391 MVT::ValueType VT = Op.getValueType();
3392 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3393 unsigned len = getSizeInBits(VT);
3394 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3395 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3396 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3397 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3398 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3399 DAG.getNode(ISD::AND, VT,
3400 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3401 }
3402 return Op;
3403 }
3404 case ISD::CTLZ: {
3405 // for now, we do this:
3406 // x = x | (x >> 1);
3407 // x = x | (x >> 2);
3408 // ...
3409 // x = x | (x >>16);
3410 // x = x | (x >>32); // for 64-bit input
3411 // return popcount(~x);
3412 //
3413 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3414 MVT::ValueType VT = Op.getValueType();
3415 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3416 unsigned len = getSizeInBits(VT);
3417 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3418 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3419 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3420 }
3421 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3422 return DAG.getNode(ISD::CTPOP, VT, Op);
3423 }
3424 case ISD::CTTZ: {
3425 // for now, we use: { return popcount(~x & (x - 1)); }
3426 // unless the target has ctlz but not ctpop, in which case we use:
3427 // { return 32 - nlz(~x & (x-1)); }
3428 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3429 MVT::ValueType VT = Op.getValueType();
3430 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3431 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3432 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3433 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3434 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3435 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3436 TLI.isOperationLegal(ISD::CTLZ, VT))
3437 return DAG.getNode(ISD::SUB, VT,
3438 DAG.getConstant(getSizeInBits(VT), VT),
3439 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3440 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3441 }
3442 }
3443}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003444
3445
Chris Lattnerdc750592005-01-07 07:47:09 +00003446/// ExpandOp - Expand the specified SDOperand into its two component pieces
3447/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3448/// LegalizeNodes map is filled in for any results that are not expanded, the
3449/// ExpandedNodes map is filled in for any results that are expanded, and the
3450/// Lo/Hi values are returned.
3451void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3452 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003453 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003454 SDNode *Node = Op.Val;
3455 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003456 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3457 "Cannot expand FP values!");
3458 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003459 "Cannot expand to FP value or to larger int value!");
3460
Chris Lattner1a570f12005-09-02 20:32:45 +00003461 // See if we already expanded it.
3462 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3463 = ExpandedNodes.find(Op);
3464 if (I != ExpandedNodes.end()) {
3465 Lo = I->second.first;
3466 Hi = I->second.second;
3467 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003468 }
3469
Chris Lattnerdc750592005-01-07 07:47:09 +00003470 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003471 case ISD::CopyFromReg:
3472 assert(0 && "CopyFromReg must be legal!");
3473 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003474 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3475 assert(0 && "Do not know how to expand this operator!");
3476 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003477 case ISD::UNDEF:
3478 Lo = DAG.getNode(ISD::UNDEF, NVT);
3479 Hi = DAG.getNode(ISD::UNDEF, NVT);
3480 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003481 case ISD::Constant: {
3482 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3483 Lo = DAG.getConstant(Cst, NVT);
3484 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3485 break;
3486 }
Nate Begemanae89d862005-12-07 19:48:11 +00003487 case ISD::ConstantVec: {
3488 unsigned NumElements = Node->getNumOperands();
3489 // If we only have two elements left in the constant vector, just break it
3490 // apart into the two scalar constants it contains. Otherwise, bisect the
3491 // ConstantVec, and return each half as a new ConstantVec.
3492 // FIXME: this is hard coded as big endian, it may have to change to support
3493 // SSE and Alpha MVI
3494 if (NumElements == 2) {
3495 Hi = Node->getOperand(0);
3496 Lo = Node->getOperand(1);
3497 } else {
3498 NumElements /= 2;
3499 std::vector<SDOperand> LoOps, HiOps;
3500 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3501 HiOps.push_back(Node->getOperand(I));
3502 LoOps.push_back(Node->getOperand(I+NumElements));
3503 }
3504 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3505 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3506 }
3507 break;
3508 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003509
Chris Lattner32e08b72005-03-28 22:03:13 +00003510 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003511 // Return the operands.
3512 Lo = Node->getOperand(0);
3513 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003514 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003515
3516 case ISD::SIGN_EXTEND_INREG:
3517 ExpandOp(Node->getOperand(0), Lo, Hi);
3518 // Sign extend the lo-part.
3519 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3520 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3521 TLI.getShiftAmountTy()));
3522 // sext_inreg the low part if needed.
3523 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3524 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003525
Nate Begeman2fba8a32006-01-14 03:14:10 +00003526 case ISD::BSWAP: {
3527 ExpandOp(Node->getOperand(0), Lo, Hi);
3528 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3529 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3530 Lo = TempLo;
3531 break;
3532 }
3533
Chris Lattner55e9cde2005-05-11 04:51:16 +00003534 case ISD::CTPOP:
3535 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003536 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3537 DAG.getNode(ISD::CTPOP, NVT, Lo),
3538 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003539 Hi = DAG.getConstant(0, NVT);
3540 break;
3541
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003542 case ISD::CTLZ: {
3543 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003544 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003545 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3546 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003547 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3548 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003549 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3550 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3551
3552 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3553 Hi = DAG.getConstant(0, NVT);
3554 break;
3555 }
3556
3557 case ISD::CTTZ: {
3558 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003559 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003560 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3561 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003562 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3563 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003564 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3565 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3566
3567 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3568 Hi = DAG.getConstant(0, NVT);
3569 break;
3570 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003571
Nate Begemane74795c2006-01-25 18:21:52 +00003572 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003573 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3574 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003575 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3576 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3577
3578 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003579 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003580 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3581 if (!TLI.isLittleEndian())
3582 std::swap(Lo, Hi);
3583 break;
3584 }
3585
Chris Lattnerdc750592005-01-07 07:47:09 +00003586 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003587 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3588 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003589 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003590
3591 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003592 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003593 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3594 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003595 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003596 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003597
3598 // Build a factor node to remember that this load is independent of the
3599 // other one.
3600 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3601 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003602
Chris Lattnerdc750592005-01-07 07:47:09 +00003603 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003604 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00003605 if (!TLI.isLittleEndian())
3606 std::swap(Lo, Hi);
3607 break;
3608 }
Nate Begemand37c1312005-11-22 18:16:00 +00003609 case ISD::VLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003610 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3611 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemand37c1312005-11-22 18:16:00 +00003612 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3613 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3614
3615 // If we only have two elements, turn into a pair of scalar loads.
3616 // FIXME: handle case where a vector of two elements is fine, such as
3617 // 2 x double on SSE2.
3618 if (NumElements == 2) {
3619 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3620 // Increment the pointer to the other half.
3621 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3622 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3623 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003624 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003625 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3626 } else {
3627 NumElements /= 2; // Split the vector in half
3628 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3629 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3630 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3631 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003632 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003633 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3634 }
3635
3636 // Build a factor node to remember that this load is independent of the
3637 // other one.
3638 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3639 Hi.getValue(1));
3640
3641 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003642 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemand37c1312005-11-22 18:16:00 +00003643 if (!TLI.isLittleEndian())
3644 std::swap(Lo, Hi);
3645 break;
3646 }
3647 case ISD::VADD:
3648 case ISD::VSUB:
3649 case ISD::VMUL: {
3650 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3651 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3652 SDOperand LL, LH, RL, RH;
3653
3654 ExpandOp(Node->getOperand(0), LL, LH);
3655 ExpandOp(Node->getOperand(1), RL, RH);
3656
3657 // If we only have two elements, turn into a pair of scalar loads.
3658 // FIXME: handle case where a vector of two elements is fine, such as
3659 // 2 x double on SSE2.
3660 if (NumElements == 2) {
3661 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3662 Lo = DAG.getNode(Opc, EVT, LL, RL);
3663 Hi = DAG.getNode(Opc, EVT, LH, RH);
3664 } else {
3665 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3666 LL.getOperand(3));
3667 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3668 LH.getOperand(3));
3669 }
3670 break;
3671 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003672 case ISD::AND:
3673 case ISD::OR:
3674 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3675 SDOperand LL, LH, RL, RH;
3676 ExpandOp(Node->getOperand(0), LL, LH);
3677 ExpandOp(Node->getOperand(1), RL, RH);
3678 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3679 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3680 break;
3681 }
3682 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003683 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00003684 ExpandOp(Node->getOperand(1), LL, LH);
3685 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003686 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3687 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00003688 break;
3689 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003690 case ISD::SELECT_CC: {
3691 SDOperand TL, TH, FL, FH;
3692 ExpandOp(Node->getOperand(2), TL, TH);
3693 ExpandOp(Node->getOperand(3), FL, FH);
3694 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3695 Node->getOperand(1), TL, FL, Node->getOperand(4));
3696 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3697 Node->getOperand(1), TH, FH, Node->getOperand(4));
3698 break;
3699 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003700 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003701 SDOperand Chain = Node->getOperand(0);
3702 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003703 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3704
3705 if (EVT == NVT)
3706 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3707 else
3708 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3709 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003710
3711 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003712 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003713
Nate Begemanc3a89c52005-10-13 17:15:37 +00003714 // The high part is obtained by SRA'ing all but one of the bits of the lo
3715 // part.
3716 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3717 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3718 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003719 break;
3720 }
3721 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003722 SDOperand Chain = Node->getOperand(0);
3723 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003724 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3725
3726 if (EVT == NVT)
3727 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3728 else
3729 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3730 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003731
3732 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003733 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003734
Nate Begemanc3a89c52005-10-13 17:15:37 +00003735 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003736 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003737 break;
3738 }
3739 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003740 SDOperand Chain = Node->getOperand(0);
3741 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00003742 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3743
3744 if (EVT == NVT)
3745 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3746 else
3747 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3748 EVT);
3749
3750 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003751 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003752
3753 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00003754 Hi = DAG.getNode(ISD::UNDEF, NVT);
3755 break;
3756 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003757 case ISD::ANY_EXTEND:
3758 // The low part is any extension of the input (which degenerates to a copy).
3759 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3760 // The high part is undefined.
3761 Hi = DAG.getNode(ISD::UNDEF, NVT);
3762 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003763 case ISD::SIGN_EXTEND: {
3764 // The low part is just a sign extension of the input (which degenerates to
3765 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003766 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003767
Chris Lattnerdc750592005-01-07 07:47:09 +00003768 // The high part is obtained by SRA'ing all but one of the bits of the lo
3769 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003770 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003771 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3772 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003773 break;
3774 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003775 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00003776 // The low part is just a zero extension of the input (which degenerates to
3777 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003778 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003779
Chris Lattnerdc750592005-01-07 07:47:09 +00003780 // The high part is just a zero.
3781 Hi = DAG.getConstant(0, NVT);
3782 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003783
3784 case ISD::BIT_CONVERT: {
3785 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3786 Node->getOperand(0));
3787 ExpandOp(Tmp, Lo, Hi);
3788 break;
3789 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003790
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003791 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00003792 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3793 TargetLowering::Custom &&
3794 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003795 Lo = TLI.LowerOperation(Op, DAG);
3796 assert(Lo.Val && "Node must be custom expanded!");
3797 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00003798 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003799 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003800 break;
3801
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003802 // These operators cannot be expanded directly, emit them as calls to
3803 // library functions.
3804 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003805 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003806 SDOperand Op;
3807 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3808 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003809 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3810 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00003811 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003812
Chris Lattner941d84a2005-07-30 01:40:57 +00003813 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3814
Chris Lattnerfe68d752005-07-29 00:33:32 +00003815 // Now that the custom expander is done, expand the result, which is still
3816 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003817 if (Op.Val) {
3818 ExpandOp(Op, Lo, Hi);
3819 break;
3820 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003821 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003822
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003823 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003824 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003825 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003826 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003827 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003828
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003829 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003830 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003831 SDOperand Op;
3832 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3833 case Expand: assert(0 && "cannot expand FP!");
3834 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3835 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3836 }
3837
3838 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3839
3840 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003841 if (Op.Val) {
3842 ExpandOp(Op, Lo, Hi);
3843 break;
3844 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003845 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003846
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003847 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003848 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003849 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003850 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003851 break;
3852
Evan Cheng870e4f82006-01-09 18:31:59 +00003853 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003854 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003855 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003856 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003857 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003858 Op = TLI.LowerOperation(Op, DAG);
3859 if (Op.Val) {
3860 // Now that the custom expander is done, expand the result, which is
3861 // still VT.
3862 ExpandOp(Op, Lo, Hi);
3863 break;
3864 }
3865 }
3866
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003867 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003868 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003869 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003870
3871 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003872 TargetLowering::LegalizeAction Action =
3873 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3874 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3875 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003876 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003877 break;
3878 }
3879
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003880 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003881 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003882 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003883 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003884
Evan Cheng870e4f82006-01-09 18:31:59 +00003885 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003886 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003887 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003888 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003889 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003890 Op = TLI.LowerOperation(Op, DAG);
3891 if (Op.Val) {
3892 // Now that the custom expander is done, expand the result, which is
3893 // still VT.
3894 ExpandOp(Op, Lo, Hi);
3895 break;
3896 }
3897 }
3898
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003899 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003900 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003901 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003902
3903 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003904 TargetLowering::LegalizeAction Action =
3905 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3906 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3907 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003908 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003909 break;
3910 }
3911
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003912 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003913 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003914 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003915 }
3916
3917 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003918 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003919 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003920 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003921 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003922 Op = TLI.LowerOperation(Op, DAG);
3923 if (Op.Val) {
3924 // Now that the custom expander is done, expand the result, which is
3925 // still VT.
3926 ExpandOp(Op, Lo, Hi);
3927 break;
3928 }
3929 }
3930
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003931 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003932 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003933 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003934
3935 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003936 TargetLowering::LegalizeAction Action =
3937 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3938 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3939 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003940 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003941 break;
3942 }
3943
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003944 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003945 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003946 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003947 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003948
Misha Brukman835702a2005-04-21 22:36:52 +00003949 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003950 case ISD::SUB: {
3951 // If the target wants to custom expand this, let them.
3952 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3953 TargetLowering::Custom) {
3954 Op = TLI.LowerOperation(Op, DAG);
3955 if (Op.Val) {
3956 ExpandOp(Op, Lo, Hi);
3957 break;
3958 }
3959 }
3960
3961 // Expand the subcomponents.
3962 SDOperand LHSL, LHSH, RHSL, RHSH;
3963 ExpandOp(Node->getOperand(0), LHSL, LHSH);
3964 ExpandOp(Node->getOperand(1), RHSL, RHSH);
3965
3966 std::vector<SDOperand> Ops;
3967 Ops.push_back(LHSL);
3968 Ops.push_back(LHSH);
3969 Ops.push_back(RHSL);
3970 Ops.push_back(RHSH);
3971 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
3972 unsigned Opc =
3973 Node->getOpcode() == ISD::ADD ? ISD::ADD_PARTS : ISD::SUB_PARTS;
3974 Lo = DAG.getNode(Opc, VTs, Ops);
3975 Hi = Lo.getValue(1);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003976 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003977 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003978 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003979 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003980 SDOperand LL, LH, RL, RH;
3981 ExpandOp(Node->getOperand(0), LL, LH);
3982 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003983 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3984 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3985 // extended the sign bit of the low half through the upper half, and if so
3986 // emit a MULHS instead of the alternate sequence that is valid for any
3987 // i64 x i64 multiply.
3988 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3989 // is RH an extension of the sign bit of RL?
3990 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3991 RH.getOperand(1).getOpcode() == ISD::Constant &&
3992 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3993 // is LH an extension of the sign bit of LL?
3994 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3995 LH.getOperand(1).getOpcode() == ISD::Constant &&
3996 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3997 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3998 } else {
3999 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4000 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4001 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4002 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4003 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4004 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004005 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4006 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004007 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004008 }
4009 break;
4010 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004011 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4012 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4013 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4014 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004015 }
4016
Chris Lattnerac12f682005-12-21 18:02:52 +00004017 // Make sure the resultant values have been legalized themselves, unless this
4018 // is a type that requires multi-step expansion.
4019 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4020 Lo = LegalizeOp(Lo);
4021 Hi = LegalizeOp(Hi);
4022 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004023
4024 // Remember in a map if the values will be reused later.
4025 bool isNew =
4026 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4027 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004028}
4029
4030
4031// SelectionDAG::Legalize - This is the entry point for the file.
4032//
Chris Lattner4add7e32005-01-23 04:42:50 +00004033void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004034 /// run - This is the main entry point to this class.
4035 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004036 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004037}
4038