blob: 195d81dd91f6237454fd1142e57e50f862864bc4 [file] [log] [blame]
Nate Begemana9795f82005-03-24 04:41:43 +00001//===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Nate Begeman5e966612005-03-24 06:28:42 +00005// This file was developed by Nate Begeman and is distributed under
Nate Begemana9795f82005-03-24 04:41:43 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PowerPC.h"
15#include "PowerPCInstrBuilder.h"
16#include "PowerPCInstrInfo.h"
17#include "PPC32RegisterInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
30#include "llvm/ADT/Statistic.h"
31#include <set>
32#include <algorithm>
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
37namespace {
38 class PPC32TargetLowering : public TargetLowering {
39 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
40 int ReturnAddrIndex; // FrameIndex for return slot.
41 public:
42 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
43 // Set up the TargetLowering object.
44
45 // Set up the register classes.
46 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
47 addRegisterClass(MVT::f32, PPC32::GPRCRegisterClass);
48 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
49
50 computeRegisterProperties();
51 }
52
53 /// LowerArguments - This hook must be implemented to indicate how we should
54 /// lower the arguments for the specified function, into the specified DAG.
55 virtual std::vector<SDOperand>
56 LowerArguments(Function &F, SelectionDAG &DAG);
57
58 /// LowerCallTo - This hook lowers an abstract call to a function into an
59 /// actual call.
60 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000061 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
62 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000063
64 virtual std::pair<SDOperand, SDOperand>
65 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
66
67 virtual std::pair<SDOperand,SDOperand>
68 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
69 const Type *ArgTy, SelectionDAG &DAG);
70
71 virtual std::pair<SDOperand, SDOperand>
72 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
73 SelectionDAG &DAG);
74 };
75}
76
77
78std::vector<SDOperand>
79PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
80 //
81 // add beautiful description of PPC stack frame format, or at least some docs
82 //
83 MachineFunction &MF = DAG.getMachineFunction();
84 MachineFrameInfo *MFI = MF.getFrameInfo();
85 MachineBasicBlock& BB = MF.front();
86 std::vector<SDOperand> ArgValues;
87
88 // Due to the rather complicated nature of the PowerPC ABI, rather than a
89 // fixed size array of physical args, for the sake of simplicity let the STL
90 // handle tracking them for us.
91 std::vector<unsigned> argVR, argPR, argOp;
92 unsigned ArgOffset = 24;
93 unsigned GPR_remaining = 8;
94 unsigned FPR_remaining = 13;
95 unsigned GPR_idx = 0, FPR_idx = 0;
96 static const unsigned GPR[] = {
97 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
98 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
99 };
100 static const unsigned FPR[] = {
101 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
102 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
103 };
104
105 // Add DAG nodes to load the arguments... On entry to a function on PPC,
106 // the arguments start at offset 24, although they are likely to be passed
107 // in registers.
108 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
109 SDOperand newroot, argt;
110 unsigned ObjSize;
111 bool needsLoad = false;
112 MVT::ValueType ObjectVT = getValueType(I->getType());
113
114 switch (ObjectVT) {
115 default: assert(0 && "Unhandled argument type!");
116 case MVT::i1:
117 case MVT::i8:
118 case MVT::i16:
119 case MVT::i32:
120 ObjSize = 4;
121 if (GPR_remaining > 0) {
122 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
123 unsigned virtReg =
124 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i32));
125 argt = newroot = DAG.getCopyFromReg(virtReg, MVT::i32, DAG.getRoot());
126 if (ObjectVT != MVT::i32)
127 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
128 argVR.push_back(virtReg);
129 argPR.push_back(GPR[GPR_idx]);
130 argOp.push_back(PPC::OR);
131 } else {
132 needsLoad = true;
133 }
134 break;
135 case MVT::i64: ObjSize = 8;
136 if (GPR_remaining > 1) {
137 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
138 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
139 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i32));
140 unsigned virtReg =
141 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i32))-1;
142 // FIXME: is this correct?
143 argt = newroot = DAG.getCopyFromReg(virtReg, MVT::i32, DAG.getRoot());
144 argt = DAG.getCopyFromReg(virtReg+1, MVT::i32, newroot);
145 // Push the arguments for emitting into BB later
146 argVR.push_back(virtReg); argVR.push_back(virtReg+1);
147 argPR.push_back(GPR[GPR_idx]); argPR.push_back(GPR[GPR_idx+1]);
148 argOp.push_back(PPC::OR); argOp.push_back(PPC::OR);
149 } else {
150 needsLoad = true;
151 }
152 break;
153 case MVT::f32: ObjSize = 4;
154 case MVT::f64: ObjSize = 8;
155 if (FPR_remaining > 0) {
156 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
157 unsigned virtReg =
158 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(ObjectVT));
159 argt = newroot = DAG.getCopyFromReg(virtReg, ObjectVT, DAG.getRoot());
160 argVR.push_back(virtReg);
161 argPR.push_back(FPR[FPR_idx]);
162 argOp.push_back(PPC::FMR);
163 --FPR_remaining;
164 ++FPR_idx;
165 } else {
166 needsLoad = true;
167 }
168 break;
169 }
170
171 // We need to load the argument to a virtual register if we determined above
172 // that we ran out of physical registers of the appropriate type
173 if (needsLoad) {
174 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
175 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
176 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
177 }
178
179 // Every 4 bytes of argument space consumes one of the GPRs available for
180 // argument passing.
181 if (GPR_remaining > 0) {
182 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
183 GPR_remaining -= delta;
184 GPR_idx += delta;
185 }
186 ArgOffset += ObjSize;
187
188 DAG.setRoot(newroot.getValue(1));
189 ArgValues.push_back(argt);
190 }
191
192 for (int i = 0, count = argVR.size(); i < count; ++i) {
193 if (argOp[i] == PPC::FMR)
194 BuildMI(&BB, argOp[i], 1, argVR[i]).addReg(argPR[i]);
195 else
196 BuildMI(&BB, argOp[i], 2, argVR[i]).addReg(argPR[i]).addReg(argPR[i]);
197 }
198
199 // If the function takes variable number of arguments, make a frame index for
200 // the start of the first vararg value... for expansion of llvm.va_start.
201 if (F.isVarArg())
202 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
203
204 return ArgValues;
205}
206
207std::pair<SDOperand, SDOperand>
208PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000209 const Type *RetTy, bool isVarArg,
210 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
211 // args_to_use will accumulate outgoing args for the ISD::CALL case in
212 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000213 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000214
215 // Count how many bytes are to be pushed on the stack, including the linkage
216 // area, and parameter passing area.
217 unsigned NumBytes = 24;
218
219 if (Args.empty()) {
220 NumBytes = 0; // Save zero bytes.
221 } else {
222 for (unsigned i = 0, e = Args.size(); i != e; ++i)
223 switch (getValueType(Args[i].second)) {
224 default: assert(0 && "Unknown value type!");
225 case MVT::i1:
226 case MVT::i8:
227 case MVT::i16:
228 case MVT::i32:
229 case MVT::f32:
230 NumBytes += 4;
231 break;
232 case MVT::i64:
233 case MVT::f64:
234 NumBytes += 8;
235 break;
236 }
237
238 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
239 // plus 32 bytes of argument space in case any called code gets funky on us.
240 if (NumBytes < 56) NumBytes = 56;
241
242 // Adjust the stack pointer for the new arguments...
243 // These operations are automatically eliminated by the prolog/epilog pass
244 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
245 DAG.getConstant(NumBytes, getPointerTy()));
246
247 // Set up a copy of the stack pointer for use loading and storing any
248 // arguments that may not fit in the registers available for argument
249 // passing.
250 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
251 DAG.getEntryNode());
252
253 // Figure out which arguments are going to go in registers, and which in
254 // memory. Also, if this is a vararg function, floating point operations
255 // must be stored to our stack, and loaded into integer regs as well, if
256 // any integer regs are available for argument passing.
257 unsigned ArgOffset = 24;
258 unsigned GPR_remaining = 8;
259 unsigned FPR_remaining = 13;
260 std::vector<SDOperand> Stores;
261 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
262 // PtrOff will be used to store the current argument to the stack if a
263 // register cannot be found for it.
264 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
265 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
266
267 switch (getValueType(Args[i].second)) {
268 default: assert(0 && "Unexpected ValueType for argument!");
269 case MVT::i1:
270 case MVT::i8:
271 case MVT::i16:
272 // Promote the integer to 32 bits. If the input type is signed use a
273 // sign extend, otherwise use a zero extend.
274 if (Args[i].second->isSigned())
275 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
276 else
277 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
278 // FALL THROUGH
279 case MVT::i32:
280 if (GPR_remaining > 0) {
281 args_to_use.push_back(Args[i].first);
282 --GPR_remaining;
283 } else {
284 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
285 Args[i].first, PtrOff));
286 }
287 ArgOffset += 4;
288 break;
289 case MVT::i64:
290 // If we have 2 or more GPRs, we won't do anything and let the ISD::CALL
291 // functionality in SelectExpr move pieces for us.
292 if (GPR_remaining > 1) {
293 args_to_use.push_back(Args[i].first);
294 GPR_remaining -= 2;
295 } else if (GPR_remaining > 0) {
296 args_to_use.push_back(Args[i].first);
297 SDOperand LowPart =
298 DAG.getNode(ISD::TRUNCATE, MVT::i32, Args[i].first);
299 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
300 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
301 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
302 LowPart, PtrOff));
303 --GPR_remaining;
304 } else {
305 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
306 Args[i].first, PtrOff));
307 }
308 ArgOffset += 8;
309 break;
310 case MVT::f32:
311 if (FPR_remaining > 0 && GPR_remaining > 0 && isVarArg) {
312 --FPR_remaining;
313 ArgOffset += 4;
314 } else if (FPR_remaining > 0) {
315 --FPR_remaining;
316 if (GPR_remaining > 0) --GPR_remaining;
317 } else {
318 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
319 Args[i].first, PtrOff));
320 }
321 ArgOffset += 4;
322 break;
323 case MVT::f64:
324 if (FPR_remaining > 0 && GPR_remaining > 0 && isVarArg) {
325 --FPR_remaining;
326 } else if (FPR_remaining > 0) {
327 --FPR_remaining;
328 if (GPR_remaining > 0) --GPR_remaining;
329 if (GPR_remaining > 0) --GPR_remaining;
330 } else {
331 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
332 Args[i].first, PtrOff));
333 }
334 ArgOffset += 8;
335 break;
336 }
Nate Begemana9795f82005-03-24 04:41:43 +0000337 }
Nate Begeman307e7442005-03-26 01:28:53 +0000338 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Nate Begemana9795f82005-03-24 04:41:43 +0000339 }
340
341 std::vector<MVT::ValueType> RetVals;
342 MVT::ValueType RetTyVT = getValueType(RetTy);
343 if (RetTyVT != MVT::isVoid)
344 RetVals.push_back(RetTyVT);
345 RetVals.push_back(MVT::Other);
346
347 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
348 Chain, Callee, args_to_use), 0);
349 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
350 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
351 DAG.getConstant(NumBytes, getPointerTy()));
352 return std::make_pair(TheCall, Chain);
353}
354
355std::pair<SDOperand, SDOperand>
356PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
357 //vastart just returns the address of the VarArgsFrameIndex slot.
358 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
359}
360
361std::pair<SDOperand,SDOperand> PPC32TargetLowering::
362LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
363 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000364 MVT::ValueType ArgVT = getValueType(ArgTy);
365 SDOperand Result;
366 if (!isVANext) {
367 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
368 } else {
369 unsigned Amt;
370 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
371 Amt = 4;
372 else {
373 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
374 "Other types should have been promoted for varargs!");
375 Amt = 8;
376 }
377 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
378 DAG.getConstant(Amt, VAList.getValueType()));
379 }
380 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000381}
382
383
384std::pair<SDOperand, SDOperand> PPC32TargetLowering::
385LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
386 SelectionDAG &DAG) {
387 abort();
388}
389
390namespace {
391
392//===--------------------------------------------------------------------===//
393/// ISel - PPC32 specific code to select PPC32 machine instructions for
394/// SelectionDAG operations.
395//===--------------------------------------------------------------------===//
396class ISel : public SelectionDAGISel {
397
398 /// Comment Here.
399 PPC32TargetLowering PPC32Lowering;
400
401 /// ExprMap - As shared expressions are codegen'd, we keep track of which
402 /// vreg the value is produced in, so we only emit one copy of each compiled
403 /// tree.
404 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000405
406 unsigned GlobalBaseReg;
407 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000408
409public:
410 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
411 {}
412
Nate Begemanc7b09f12005-03-25 08:34:25 +0000413 /// runOnFunction - Override this function in order to reset our per-function
414 /// variables.
415 virtual bool runOnFunction(Function &Fn) {
416 // Make sure we re-emit a set of the global base reg if necessary
417 GlobalBaseInitialized = false;
418 return SelectionDAGISel::runOnFunction(Fn);
419 }
420
Nate Begemana9795f82005-03-24 04:41:43 +0000421 /// InstructionSelectBasicBlock - This callback is invoked by
422 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
423 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
424 DEBUG(BB->dump());
425 // Codegen the basic block.
426 Select(DAG.getRoot());
427
428 // Clear state used for selection.
429 ExprMap.clear();
430 }
431
Nate Begemanc7b09f12005-03-25 08:34:25 +0000432 unsigned ISel::getGlobalBaseReg();
Nate Begemana9795f82005-03-24 04:41:43 +0000433 unsigned SelectExpr(SDOperand N);
434 unsigned SelectExprFP(SDOperand N, unsigned Result);
435 void Select(SDOperand N);
436
437 void SelectAddr(SDOperand N, unsigned& Reg, int& offset);
438 void SelectBranchCC(SDOperand N);
439};
440
441/// canUseAsImmediateForOpcode - This method returns a value indicating whether
442/// the ConstantSDNode N can be used as an immediate to Opcode. The return
443/// values are either 0, 1 or 2. 0 indicates that either N is not a
444/// ConstantSDNode, or is not suitable for use by that opcode. A return value
445/// of 1 indicates that the constant may be used in normal immediate form. A
446/// return value of 2 indicates that the constant may be used in shifted
447/// immediate form. If the return value is nonzero, the constant value is
448/// placed in Imm.
449///
450static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
451 unsigned& Imm) {
452 if (N.getOpcode() != ISD::Constant) return 0;
453
454 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
455
456 switch(Opcode) {
457 default: return 0;
458 case ISD::ADD:
459 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
460 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
461 break;
462 case ISD::AND:
463 case ISD::XOR:
464 case ISD::OR:
465 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
466 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
467 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000468 case ISD::MUL:
469 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
470 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000471 }
472 return 0;
473}
474}
475
Nate Begemanc7b09f12005-03-25 08:34:25 +0000476/// getGlobalBaseReg - Output the instructions required to put the
477/// base address to use for accessing globals into a register.
478///
479unsigned ISel::getGlobalBaseReg() {
480 if (!GlobalBaseInitialized) {
481 // Insert the set of GlobalBaseReg into the first MBB of the function
482 MachineBasicBlock &FirstMBB = BB->getParent()->front();
483 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
484 GlobalBaseReg = MakeReg(MVT::i32);
485 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
486 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
487 GlobalBaseInitialized = true;
488 }
489 return GlobalBaseReg;
490}
491
Nate Begemana9795f82005-03-24 04:41:43 +0000492//Check to see if the load is a constant offset from a base register
493void ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
494{
495 Reg = SelectExpr(N);
496 offset = 0;
497 return;
498}
499
500void ISel::SelectBranchCC(SDOperand N)
501{
502 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
503 MachineBasicBlock *Dest =
504 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
505 unsigned Opc;
506
507 Select(N.getOperand(0)); //chain
508 SDOperand CC = N.getOperand(1);
509
510 //Giveup and do the stupid thing
511 unsigned Tmp1 = SelectExpr(CC);
512 BuildMI(BB, PPC::BNE, 2).addReg(Tmp1).addMBB(Dest);
513 return;
514}
515
516unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
517{
518 unsigned Tmp1, Tmp2, Tmp3;
519 unsigned Opc = 0;
520 SDNode *Node = N.Val;
521 MVT::ValueType DestType = N.getValueType();
522 unsigned opcode = N.getOpcode();
523
524 switch (opcode) {
525 default:
526 Node->dump();
527 assert(0 && "Node not handled!\n");
528
529 case ISD::SELECT:
530 abort();
531
532 case ISD::FP_ROUND:
533 assert (DestType == MVT::f32 &&
534 N.getOperand(0).getValueType() == MVT::f64 &&
535 "only f64 to f32 conversion supported here");
536 Tmp1 = SelectExpr(N.getOperand(0));
537 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
538 return Result;
539
540 case ISD::FP_EXTEND:
541 assert (DestType == MVT::f64 &&
542 N.getOperand(0).getValueType() == MVT::f32 &&
543 "only f32 to f64 conversion supported here");
544 Tmp1 = SelectExpr(N.getOperand(0));
545 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
546 return Result;
547
548 case ISD::CopyFromReg:
549 // FIXME: Handle copy from physregs!
550 // Just use the specified register as our input.
551 return dyn_cast<RegSDNode>(Node)->getReg();
552
553 case ISD::LOAD:
Nate Begeman5e966612005-03-24 06:28:42 +0000554 case ISD::EXTLOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000555 abort();
556
557 case ISD::ConstantFP:
558 abort();
559
560 case ISD::MUL:
561 case ISD::ADD:
562 case ISD::SUB:
563 case ISD::SDIV:
564 switch( opcode ) {
565 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
566 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
567 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
568 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
569 };
570
571 Tmp1 = SelectExpr(N.getOperand(0));
572 Tmp2 = SelectExpr(N.getOperand(1));
573 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
574 return Result;
575
Nate Begemana9795f82005-03-24 04:41:43 +0000576 case ISD::UINT_TO_FP:
577 case ISD::SINT_TO_FP:
578 abort();
579 }
580 assert(0 && "should not get here");
581 return 0;
582}
583
584unsigned ISel::SelectExpr(SDOperand N) {
585 unsigned Result;
586 unsigned Tmp1, Tmp2, Tmp3;
587 unsigned Opc = 0;
588 unsigned opcode = N.getOpcode();
589
590 SDNode *Node = N.Val;
591 MVT::ValueType DestType = N.getValueType();
592
593 unsigned &Reg = ExprMap[N];
594 if (Reg) return Reg;
595
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000596 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
597 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000598 Reg = Result = (N.getValueType() != MVT::Other) ?
599 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000600 else {
601 // If this is a call instruction, make sure to prepare ALL of the result
602 // values as well as the chain.
603 if (N.getOpcode() == ISD::CALL) {
604 if (Node->getNumValues() == 1)
605 Reg = Result = 1; // Void call, just a chain.
606 else {
607 Result = MakeReg(Node->getValueType(0));
608 ExprMap[N.getValue(0)] = Result;
609 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
610 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
611 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
612 }
613 } else {
614 Result = MakeReg(Node->getValueType(0));
615 ExprMap[N.getValue(0)] = Result;
616 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
617 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
618 }
619 }
620
621 if (DestType == MVT::f64 || DestType == MVT::f32)
622 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000623
624 switch (opcode) {
625 default:
626 Node->dump();
627 assert(0 && "Node not handled!\n");
628
629 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000630 // Generate both result values. FIXME: Need a better commment here?
631 if (Result != 1)
632 ExprMap[N.getValue(1)] = 1;
633 else
634 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
635
636 // FIXME: We are currently ignoring the requested alignment for handling
637 // greater than the stack alignment. This will need to be revisited at some
638 // point. Align = N.getOperand(2);
639 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
640 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
641 std::cerr << "Cannot allocate stack object with greater alignment than"
642 << " the stack alignment yet!";
643 abort();
644 }
645 Select(N.getOperand(0));
646 Tmp1 = SelectExpr(N.getOperand(1));
647 // Subtract size from stack pointer, thereby allocating some space.
648 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
649 // Put a pointer to the space into the result register by copying the SP
650 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
651 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000652
653 case ISD::ConstantPool:
654 abort();
655
656 case ISD::FrameIndex:
657 abort();
658
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000659 case ISD::GlobalAddress: {
660 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
661 unsigned Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000662 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
663 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000664 if (GV->hasWeakLinkage() || GV->isExternal()) {
665 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
666 } else {
667 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
668 }
669 return Result;
670 }
671
Nate Begeman5e966612005-03-24 06:28:42 +0000672 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000673 case ISD::EXTLOAD:
674 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000675 case ISD::SEXTLOAD: {
Nate Begeman5e966612005-03-24 06:28:42 +0000676 // Make sure we generate both values.
677 if (Result != 1)
678 ExprMap[N.getValue(1)] = 1; // Generate the token
679 else
680 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
681
682 SDOperand Chain = N.getOperand(0);
683 SDOperand Address = N.getOperand(1);
684 Select(Chain);
685
686 switch (Node->getValueType(0)) {
687 default: assert(0 && "Cannot load this type!");
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000688 case MVT::i1: Opc = PPC::LBZ; Tmp3 = 0; break;
689 case MVT::i8: Opc = PPC::LBZ; Tmp3 = 1; break;
690 case MVT::i16: Opc = PPC::LHZ; Tmp3 = 0; break;
691 case MVT::i32: Opc = PPC::LWZ; Tmp3 = 0; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000692 }
693
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000694 if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman5e966612005-03-24 06:28:42 +0000695 BuildMI(BB, Opc, 2, Result)
696 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
697 .addReg(PPC::R1);
698 } else {
699 int offset;
700 SelectAddr(Address, Tmp1, offset);
701 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
702 }
703 return Result;
704 }
705
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000706 case ISD::CALL: {
707 // Lower the chain for this call.
708 Select(N.getOperand(0));
709 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
710
711 // get the virtual reg for each argument
712 std::vector<unsigned> VRegs;
713 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
714 VRegs.push_back(SelectExpr(N.getOperand(i)));
715
716 // The ABI specifies that the first 32 bytes of args may be passed in GPRs,
717 // and that 13 FPRs may also be used for passing any floating point args.
718 int GPR_remaining = 8, FPR_remaining = 13;
719 unsigned GPR_idx = 0, FPR_idx = 0;
720 static const unsigned GPR[] = {
721 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
722 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
723 };
724 static const unsigned FPR[] = {
725 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6,
726 PPC::F7, PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12,
727 PPC::F13
728 };
729
730 // move the vregs into the appropriate architected register or stack slot
731 for(int i = 0, e = VRegs.size(); i < e; ++i) {
732 unsigned OperandType = N.getOperand(i+2).getValueType();
733 switch(OperandType) {
734 default:
735 Node->dump();
736 N.getOperand(i).Val->dump();
737 std::cerr << "Type for " << i << " is: " <<
738 N.getOperand(i+2).getValueType() << "\n";
739 assert(0 && "Unknown value type for call");
740 case MVT::i1:
741 case MVT::i8:
742 case MVT::i16:
743 case MVT::i32:
744 if (GPR_remaining > 0)
745 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(VRegs[i])
746 .addReg(VRegs[i]);
747 break;
748 case MVT::f32:
749 case MVT::f64:
750 if (FPR_remaining > 0) {
751 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(VRegs[i]);
752 --FPR_remaining;
753 }
754 break;
755 }
756 // All arguments consume GPRs available for argument passing
757 if (GPR_remaining > 0) --GPR_remaining;
758 if (MVT::f64 == OperandType && GPR_remaining > 0) --GPR_remaining;
759 }
760
761 // Emit the correct call instruction based on the type of symbol called.
762 if (GlobalAddressSDNode *GASD =
763 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
764 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
765 } else if (ExternalSymbolSDNode *ESSDN =
766 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
767 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
768 } else {
769 Tmp1 = SelectExpr(N.getOperand(1));
770 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
771 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
772 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
773 }
774
775 switch (Node->getValueType(0)) {
776 default: assert(0 && "Unknown value type for call result!");
777 case MVT::Other: return 1;
778 case MVT::i1:
779 case MVT::i8:
780 case MVT::i16:
781 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +0000782 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000783 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +0000784 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000785 break;
786 case MVT::f32:
787 case MVT::f64:
788 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
789 break;
790 }
791 return Result+N.ResNo;
792 }
Nate Begemana9795f82005-03-24 04:41:43 +0000793
794 case ISD::SIGN_EXTEND:
795 case ISD::SIGN_EXTEND_INREG:
796 Tmp1 = SelectExpr(N.getOperand(0));
797 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
798 return Result;
799
800 case ISD::ZERO_EXTEND_INREG:
801 Tmp1 = SelectExpr(N.getOperand(0));
802 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
803 default:
804 Node->dump();
805 assert(0 && "Zero Extend InReg not there yet");
806 break;
807 case MVT::i16: Tmp2 = 16; break;
808 case MVT::i8: Tmp2 = 24; break;
809 case MVT::i1: Tmp2 = 31; break;
810 }
811 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(0).addImm(0)
812 .addImm(Tmp2).addImm(31);
813 return Result;
814
Nate Begemana9795f82005-03-24 04:41:43 +0000815 case ISD::CopyFromReg:
816 if (Result == 1)
817 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
818 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
819 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
820 return Result;
821
822 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +0000823 Tmp1 = SelectExpr(N.getOperand(0));
824 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
825 Tmp2 = CN->getValue() & 0x1F;
826 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
827 .addImm(31-Tmp2);
828 } else {
829 Tmp2 = SelectExpr(N.getOperand(1));
830 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
831 }
832 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000833
Nate Begeman5e966612005-03-24 06:28:42 +0000834 case ISD::SRL:
835 Tmp1 = SelectExpr(N.getOperand(0));
836 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
837 Tmp2 = CN->getValue() & 0x1F;
838 BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(32-Tmp2)
839 .addImm(Tmp2).addImm(31);
840 } else {
841 Tmp2 = SelectExpr(N.getOperand(1));
842 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
843 }
844 return Result;
845
846 case ISD::SRA:
847 Tmp1 = SelectExpr(N.getOperand(0));
848 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
849 Tmp2 = CN->getValue() & 0x1F;
850 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
851 } else {
852 Tmp2 = SelectExpr(N.getOperand(1));
853 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
854 }
855 return Result;
856
Nate Begemana9795f82005-03-24 04:41:43 +0000857 case ISD::ADD:
858 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
859 Tmp1 = SelectExpr(N.getOperand(0));
860 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
861 default: assert(0 && "unhandled result code");
862 case 0: // No immediate
863 Tmp2 = SelectExpr(N.getOperand(1));
864 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
865 break;
866 case 1: // Low immediate
867 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
868 break;
869 case 2: // Shifted immediate
870 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
871 break;
872 }
873 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000874
Nate Begemana9795f82005-03-24 04:41:43 +0000875 case ISD::AND:
876 case ISD::OR:
877 case ISD::XOR:
878 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
879 Tmp1 = SelectExpr(N.getOperand(0));
880 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
881 default: assert(0 && "unhandled result code");
882 case 0: // No immediate
883 Tmp2 = SelectExpr(N.getOperand(1));
884 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000885 case ISD::AND: Opc = PPC::AND; break;
886 case ISD::OR: Opc = PPC::OR; break;
887 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000888 }
Nate Begeman5e966612005-03-24 06:28:42 +0000889 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000890 break;
891 case 1: // Low immediate
892 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000893 case ISD::AND: Opc = PPC::ANDIo; break;
894 case ISD::OR: Opc = PPC::ORI; break;
895 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000896 }
Nate Begeman5e966612005-03-24 06:28:42 +0000897 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000898 break;
899 case 2: // Shifted immediate
900 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +0000901 case ISD::AND: Opc = PPC::ANDISo; break;
902 case ISD::OR: Opc = PPC::ORIS; break;
903 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +0000904 }
Nate Begeman5e966612005-03-24 06:28:42 +0000905 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +0000906 break;
907 }
908 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000909
910 case ISD::SUB:
911 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
912 Tmp1 = SelectExpr(N.getOperand(0));
913 Tmp2 = SelectExpr(N.getOperand(1));
914 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
915 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000916
Nate Begeman5e966612005-03-24 06:28:42 +0000917 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000918 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
919 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +0000920 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
921 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
922 else {
923 Tmp2 = SelectExpr(N.getOperand(1));
924 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
925 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000926 return Result;
927
928 case ISD::ADD_PARTS:
929 case ISD::SUB_PARTS:
Nate Begemana9795f82005-03-24 04:41:43 +0000930 case ISD::UREM:
931 case ISD::SREM:
932 case ISD::SDIV:
933 case ISD::UDIV:
934 abort();
935
936 case ISD::FP_TO_UINT:
937 case ISD::FP_TO_SINT:
938 abort();
939
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000940 case ISD::SETCC:
941 abort();
942
Nate Begemana9795f82005-03-24 04:41:43 +0000943 case ISD::SELECT:
944 abort();
945
946 case ISD::Constant:
947 switch (N.getValueType()) {
948 default: assert(0 && "Cannot use constants of this type!");
949 case MVT::i1:
950 BuildMI(BB, PPC::LI, 1, Result)
951 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
952 break;
953 case MVT::i32:
954 {
955 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
956 if (v < 32768 && v >= -32768) {
957 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
958 } else {
Nate Begeman5e966612005-03-24 06:28:42 +0000959 Tmp1 = MakeReg(MVT::i32);
960 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
961 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +0000962 }
963 }
964 }
965 return Result;
966 }
967
968 return 0;
969}
970
971void ISel::Select(SDOperand N) {
972 unsigned Tmp1, Tmp2, Opc;
973 unsigned opcode = N.getOpcode();
974
975 if (!ExprMap.insert(std::make_pair(N, 1)).second)
976 return; // Already selected.
977
978 SDNode *Node = N.Val;
979
980 switch (Node->getOpcode()) {
981 default:
982 Node->dump(); std::cerr << "\n";
983 assert(0 && "Node not handled yet!");
984 case ISD::EntryToken: return; // Noop
985 case ISD::TokenFactor:
986 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
987 Select(Node->getOperand(i));
988 return;
989 case ISD::ADJCALLSTACKDOWN:
990 case ISD::ADJCALLSTACKUP:
991 Select(N.getOperand(0));
992 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
993 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
994 PPC::ADJCALLSTACKUP;
995 BuildMI(BB, Opc, 1).addImm(Tmp1);
996 return;
997 case ISD::BR: {
998 MachineBasicBlock *Dest =
999 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001000 Select(N.getOperand(0));
1001 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1002 return;
1003 }
1004 case ISD::BRCOND:
1005 SelectBranchCC(N);
1006 return;
1007 case ISD::CopyToReg:
1008 Select(N.getOperand(0));
1009 Tmp1 = SelectExpr(N.getOperand(1));
1010 Tmp2 = cast<RegSDNode>(N)->getReg();
1011
1012 if (Tmp1 != Tmp2) {
1013 if (N.getOperand(1).getValueType() == MVT::f64 ||
1014 N.getOperand(1).getValueType() == MVT::f32)
1015 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1016 else
1017 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1018 }
1019 return;
1020 case ISD::ImplicitDef:
1021 Select(N.getOperand(0));
1022 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1023 return;
1024 case ISD::RET:
1025 switch (N.getNumOperands()) {
1026 default:
1027 assert(0 && "Unknown return instruction!");
1028 case 3:
1029 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1030 N.getOperand(2).getValueType() == MVT::i32 &&
1031 "Unknown two-register value!");
1032 Select(N.getOperand(0));
1033 Tmp1 = SelectExpr(N.getOperand(1));
1034 Tmp2 = SelectExpr(N.getOperand(2));
1035 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1036 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1037 break;
1038 case 2:
1039 Select(N.getOperand(0));
1040 Tmp1 = SelectExpr(N.getOperand(1));
1041 switch (N.getOperand(1).getValueType()) {
1042 default:
1043 assert(0 && "Unknown return type!");
1044 case MVT::f64:
1045 case MVT::f32:
1046 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1047 break;
1048 case MVT::i32:
1049 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1050 break;
1051 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001052 case 1:
1053 Select(N.getOperand(0));
1054 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001055 }
1056 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1057 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001058 case ISD::TRUNCSTORE:
1059 case ISD::STORE:
1060 {
1061 SDOperand Chain = N.getOperand(0);
1062 SDOperand Value = N.getOperand(1);
1063 SDOperand Address = N.getOperand(2);
1064 Select(Chain);
1065
1066 Tmp1 = SelectExpr(Value); //value
1067
1068 if (opcode == ISD::STORE) {
1069 switch(Value.getValueType()) {
1070 default: assert(0 && "unknown Type in store");
1071 case MVT::i32: Opc = PPC::STW; break;
1072 case MVT::f64: Opc = PPC::STFD; break;
1073 case MVT::f32: Opc = PPC::STFS; break;
1074 }
1075 } else { //ISD::TRUNCSTORE
1076 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1077 default: assert(0 && "unknown Type in store");
1078 case MVT::i1: //FIXME: DAG does not promote this load
1079 case MVT::i8: Opc = PPC::STB; break;
1080 case MVT::i16: Opc = PPC::STH; break;
1081 }
1082 }
1083
1084 if (Address.getOpcode() == ISD::GlobalAddress)
1085 {
1086 BuildMI(BB, Opc, 2).addReg(Tmp1)
1087 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1088 }
1089 else if(Address.getOpcode() == ISD::FrameIndex)
1090 {
1091 BuildMI(BB, Opc, 2).addReg(Tmp1)
1092 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex());
1093 }
1094 else
1095 {
1096 int offset;
1097 SelectAddr(Address, Tmp2, offset);
1098 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1099 }
1100 return;
1101 }
1102 case ISD::EXTLOAD:
1103 case ISD::SEXTLOAD:
1104 case ISD::ZEXTLOAD:
1105 case ISD::LOAD:
1106 case ISD::CopyFromReg:
1107 case ISD::CALL:
1108 case ISD::DYNAMIC_STACKALLOC:
1109 ExprMap.erase(N);
1110 SelectExpr(N);
1111 return;
1112 }
1113 assert(0 && "Should not be reached!");
1114}
1115
1116
1117/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1118/// into a machine code representation using pattern matching and a machine
1119/// description file.
1120///
1121FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1122 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001123}
1124