fix typo
[oota-llvm.git] / lib / Target / PowerPC / PPC64ISelPattern.cpp
1 //===-- PPC64ISelPattern.cpp - A pattern matching inst selector for PPC64 -===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Nate Begeman and is distributed under
6 // 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 64 bit PowerPC.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "PowerPC.h"
15 #include "PowerPCInstrBuilder.h"
16 #include "PowerPCInstrInfo.h"
17 #include "PPC64RegisterInfo.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/Target/TargetOptions.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/ADT/Statistic.h"
32 #include <set>
33 #include <algorithm>
34 using namespace llvm;
35
36 //===----------------------------------------------------------------------===//
37 //  PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
38 namespace {
39   class PPC64TargetLowering : public TargetLowering {
40     int VarArgsFrameIndex;            // FrameIndex for start of varargs area.
41     int ReturnAddrIndex;              // FrameIndex for return slot.
42   public:
43     PPC64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
44       // Set up the register classes.
45       addRegisterClass(MVT::i64, PPC64::GPRCRegisterClass);
46       addRegisterClass(MVT::f32, PPC64::FPRCRegisterClass);
47       addRegisterClass(MVT::f64, PPC64::FPRCRegisterClass);
48
49       // PowerPC has no intrinsics for these particular operations
50       setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
51       setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
52       setOperationAction(ISD::MEMSET, MVT::Other, Expand);
53       setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
54
55       // We don't support sin/cos/sqrt
56       setOperationAction(ISD::FSIN , MVT::f64, Expand);
57       setOperationAction(ISD::FCOS , MVT::f64, Expand);
58       setOperationAction(ISD::FSQRT, MVT::f64, Expand);
59       setOperationAction(ISD::FSIN , MVT::f32, Expand);
60       setOperationAction(ISD::FCOS , MVT::f32, Expand);
61       setOperationAction(ISD::FSQRT, MVT::f32, Expand);
62
63       // PPC 64 has i16 and i32 but no i8 (or i1) SEXTLOAD
64       setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
65       setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
66
67       // PowerPC has no SREM/UREM instructions
68       setOperationAction(ISD::SREM, MVT::i64, Expand);
69       setOperationAction(ISD::UREM, MVT::i64, Expand);
70
71       // PowerPC has these, but they are not implemented
72       setOperationAction(ISD::CTPOP, MVT::i64, Expand);
73       setOperationAction(ISD::CTTZ , MVT::i64, Expand);
74       setOperationAction(ISD::CTLZ , MVT::i64, Expand);
75
76       setShiftAmountFlavor(Extend);   // shl X, 32 == 0
77       addLegalFPImmediate(+0.0); // Necessary for FSEL
78       addLegalFPImmediate(-0.0); //
79
80       computeRegisterProperties();
81     }
82
83     /// LowerArguments - This hook must be implemented to indicate how we should
84     /// lower the arguments for the specified function, into the specified DAG.
85     virtual std::vector<SDOperand>
86     LowerArguments(Function &F, SelectionDAG &DAG);
87
88     /// LowerCallTo - This hook lowers an abstract call to a function into an
89     /// actual call.
90     virtual std::pair<SDOperand, SDOperand>
91     LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
92                 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
93
94     virtual std::pair<SDOperand, SDOperand>
95     LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
96
97     virtual std::pair<SDOperand,SDOperand>
98     LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
99                    const Type *ArgTy, SelectionDAG &DAG);
100
101     virtual std::pair<SDOperand, SDOperand>
102     LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
103                             SelectionDAG &DAG);
104   };
105 }
106
107
108 std::vector<SDOperand>
109 PPC64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
110   //
111   // add beautiful description of PPC stack frame format, or at least some docs
112   //
113   MachineFunction &MF = DAG.getMachineFunction();
114   MachineFrameInfo *MFI = MF.getFrameInfo();
115   MachineBasicBlock& BB = MF.front();
116   std::vector<SDOperand> ArgValues;
117
118   // Due to the rather complicated nature of the PowerPC ABI, rather than a
119   // fixed size array of physical args, for the sake of simplicity let the STL
120   // handle tracking them for us.
121   std::vector<unsigned> argVR, argPR, argOp;
122   unsigned ArgOffset = 48;
123   unsigned GPR_remaining = 8;
124   unsigned FPR_remaining = 13;
125   unsigned GPR_idx = 0, FPR_idx = 0;
126   static const unsigned GPR[] = {
127     PPC::R3, PPC::R4, PPC::R5, PPC::R6,
128     PPC::R7, PPC::R8, PPC::R9, PPC::R10,
129   };
130   static const unsigned FPR[] = {
131     PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
132     PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
133   };
134
135   // Add DAG nodes to load the arguments...  On entry to a function on PPC,
136   // the arguments start at offset 48, although they are likely to be passed
137   // in registers.
138   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
139     SDOperand newroot, argt;
140     bool needsLoad = false;
141     MVT::ValueType ObjectVT = getValueType(I->getType());
142
143     switch (ObjectVT) {
144     default: assert(0 && "Unhandled argument type!");
145     case MVT::i1:
146     case MVT::i8:
147     case MVT::i16:
148     case MVT::i32:
149     case MVT::i64:
150       if (GPR_remaining > 0) {
151         BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
152         argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
153                                             DAG.getRoot());
154         if (ObjectVT != MVT::i64)
155           argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
156       } else {
157         needsLoad = true;
158       }
159       break;
160     case MVT::f32:
161     case MVT::f64:
162       if (FPR_remaining > 0) {
163         BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
164         argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
165                                             DAG.getRoot());
166         --FPR_remaining;
167         ++FPR_idx;
168       } else {
169         needsLoad = true;
170       }
171       break;
172     }
173
174     // We need to load the argument to a virtual register if we determined above
175     // that we ran out of physical registers of the appropriate type
176     if (needsLoad) {
177       unsigned SubregOffset = 0;
178       switch (ObjectVT) {
179       default: assert(0 && "Unhandled argument type!");
180       case MVT::i1:
181       case MVT::i8:   SubregOffset = 7; break;
182       case MVT::i16:  SubregOffset = 6; break;
183       case MVT::i32:
184       case MVT::f32:  SubregOffset = 4; break;
185       case MVT::i64:
186       case MVT::f64:  SubregOffset = 0; break;
187       }
188       int FI = MFI->CreateFixedObject(8, ArgOffset);
189       SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
190       FIN = DAG.getNode(ISD::ADD, MVT::i64, FIN,
191                         DAG.getConstant(SubregOffset, MVT::i64));
192       argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
193     }
194
195     // Every 4 bytes of argument space consumes one of the GPRs available for
196     // argument passing.
197     if (GPR_remaining > 0) {
198       --GPR_remaining;
199       ++GPR_idx;
200     }
201     ArgOffset += 8;
202
203     DAG.setRoot(newroot.getValue(1));
204     ArgValues.push_back(argt);
205   }
206
207   // If the function takes variable number of arguments, make a frame index for
208   // the start of the first vararg value... for expansion of llvm.va_start.
209   if (F.isVarArg()) {
210     VarArgsFrameIndex = MFI->CreateFixedObject(8, ArgOffset);
211     SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
212     // If this function is vararg, store any remaining integer argument regs
213     // to their spots on the stack so that they may be loaded by deferencing the
214     // result of va_next.
215     std::vector<SDOperand> MemOps;
216     for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
217       BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
218       SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i64, DAG.getRoot());
219       SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
220                                     Val, FIN, DAG.getSrcValue(NULL));
221       MemOps.push_back(Store);
222       // Increment the address by eight for the next argument to store
223       SDOperand PtrOff = DAG.getConstant(8, getPointerTy());
224       FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
225     }
226     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
227   }
228
229   return ArgValues;
230 }
231
232 std::pair<SDOperand, SDOperand>
233 PPC64TargetLowering::LowerCallTo(SDOperand Chain,
234                                  const Type *RetTy, bool isVarArg,
235                                  SDOperand Callee, ArgListTy &Args,
236                                  SelectionDAG &DAG) {
237   // args_to_use will accumulate outgoing args for the ISD::CALL case in
238   // SelectExpr to use to put the arguments in the appropriate registers.
239   std::vector<SDOperand> args_to_use;
240
241   // Count how many bytes are to be pushed on the stack, including the linkage
242   // area, and parameter passing area.
243   unsigned NumBytes = 48;
244
245   if (Args.empty()) {
246     Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
247                         DAG.getConstant(NumBytes, getPointerTy()));
248   } else {
249     NumBytes = 8 * Args.size(); // All arguments are rounded up to 8 bytes
250
251     // Just to be safe, we'll always reserve the full 48 bytes of linkage area
252     // plus 64 bytes of argument space in case any called code gets funky on us.
253     if (NumBytes < 112) NumBytes = 112;
254
255     // Adjust the stack pointer for the new arguments...
256     // These operations are automatically eliminated by the prolog/epilog pass
257     Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
258                         DAG.getConstant(NumBytes, getPointerTy()));
259
260     // Set up a copy of the stack pointer for use loading and storing any
261     // arguments that may not fit in the registers available for argument
262     // passing.
263     SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
264                                             DAG.getEntryNode());
265
266     // Figure out which arguments are going to go in registers, and which in
267     // memory.  Also, if this is a vararg function, floating point operations
268     // must be stored to our stack, and loaded into integer regs as well, if
269     // any integer regs are available for argument passing.
270     unsigned ArgOffset = 48;
271     unsigned GPR_remaining = 8;
272     unsigned FPR_remaining = 13;
273
274     std::vector<SDOperand> MemOps;
275     for (unsigned i = 0, e = Args.size(); i != e; ++i) {
276       // PtrOff will be used to store the current argument to the stack if a
277       // register cannot be found for it.
278       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
279       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
280       MVT::ValueType ArgVT = getValueType(Args[i].second);
281
282       switch (ArgVT) {
283       default: assert(0 && "Unexpected ValueType for argument!");
284       case MVT::i1:
285       case MVT::i8:
286       case MVT::i16:
287       case MVT::i32:
288         // Promote the integer to 64 bits.  If the input type is signed use a
289         // sign extend, otherwise use a zero extend.
290         if (Args[i].second->isSigned())
291           Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
292         else
293           Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
294         // FALL THROUGH
295       case MVT::i64:
296         if (GPR_remaining > 0) {
297           args_to_use.push_back(Args[i].first);
298           --GPR_remaining;
299         } else {
300           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
301                                           Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
302         }
303         ArgOffset += 8;
304         break;
305       case MVT::f32:
306       case MVT::f64:
307         if (FPR_remaining > 0) {
308           args_to_use.push_back(Args[i].first);
309           --FPR_remaining;
310           if (isVarArg) {
311             SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
312                                           Args[i].first, PtrOff, DAG.getSrcValue(NULL));
313             MemOps.push_back(Store);
314             // Float varargs are always shadowed in available integer registers
315             if (GPR_remaining > 0) {
316               SDOperand Load = DAG.getLoad(MVT::i64, Store, PtrOff, DAG.getSrcValue(NULL));
317               MemOps.push_back(Load);
318               args_to_use.push_back(Load);
319               --GPR_remaining;
320             }
321           } else {
322             // If we have any FPRs remaining, we may also have GPRs remaining.
323             // Args passed in FPRs also consume an available GPR.
324             if (GPR_remaining > 0) {
325               args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i64));
326               --GPR_remaining;
327             }
328           }
329         } else {
330           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
331                                           Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
332         }
333         ArgOffset += 8;
334         break;
335       }
336     }
337     if (!MemOps.empty())
338       Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
339   }
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
355 std::pair<SDOperand, SDOperand>
356 PPC64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
357   //vastart just returns the address of the VarArgsFrameIndex slot.
358   return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
359 }
360
361 std::pair<SDOperand,SDOperand> PPC64TargetLowering::
362 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
363                const Type *ArgTy, SelectionDAG &DAG) {
364   MVT::ValueType ArgVT = getValueType(ArgTy);
365   SDOperand Result;
366   if (!isVANext) {
367     Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
368   } else {
369     Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
370                          DAG.getConstant(8, VAList.getValueType()));
371   }
372   return std::make_pair(Result, Chain);
373 }
374
375
376 std::pair<SDOperand, SDOperand> PPC64TargetLowering::
377 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
378                         SelectionDAG &DAG) {
379   assert(0 && "LowerFrameReturnAddress unimplemented");
380   abort();
381 }
382
383 namespace {
384 Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
385 Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
386 //===--------------------------------------------------------------------===//
387 /// ISel - PPC32 specific code to select PPC32 machine instructions for
388 /// SelectionDAG operations.
389 //===--------------------------------------------------------------------===//
390 class ISel : public SelectionDAGISel {
391
392   /// Comment Here.
393   PPC64TargetLowering PPC64Lowering;
394
395   /// ExprMap - As shared expressions are codegen'd, we keep track of which
396   /// vreg the value is produced in, so we only emit one copy of each compiled
397   /// tree.
398   std::map<SDOperand, unsigned> ExprMap;
399
400   unsigned GlobalBaseReg;
401   bool GlobalBaseInitialized;
402
403 public:
404   ISel(TargetMachine &TM) : SelectionDAGISel(PPC64Lowering), PPC64Lowering(TM)
405   {}
406
407   /// runOnFunction - Override this function in order to reset our per-function
408   /// variables.
409   virtual bool runOnFunction(Function &Fn) {
410     // Make sure we re-emit a set of the global base reg if necessary
411     GlobalBaseInitialized = false;
412     return SelectionDAGISel::runOnFunction(Fn);
413   }
414
415   /// InstructionSelectBasicBlock - This callback is invoked by
416   /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
417   virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
418     DEBUG(BB->dump());
419     // Codegen the basic block.
420     Select(DAG.getRoot());
421
422     // Clear state used for selection.
423     ExprMap.clear();
424   }
425
426   unsigned getGlobalBaseReg();
427   unsigned getConstDouble(double floatVal, unsigned Result);
428   unsigned SelectSetCR0(SDOperand CC);
429   unsigned SelectExpr(SDOperand N);
430   unsigned SelectExprFP(SDOperand N, unsigned Result);
431   void Select(SDOperand N);
432
433   bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
434   void SelectBranchCC(SDOperand N);
435 };
436
437 /// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N.  It
438 /// returns zero when the input is not exactly a power of two.
439 static unsigned ExactLog2(unsigned Val) {
440   if (Val == 0 || (Val & (Val-1))) return 0;
441   unsigned Count = 0;
442   while (Val != 1) {
443     Val >>= 1;
444     ++Count;
445   }
446   return Count;
447 }
448
449 /// getImmediateForOpcode - This method returns a value indicating whether
450 /// the ConstantSDNode N can be used as an immediate to Opcode.  The return
451 /// values are either 0, 1 or 2.  0 indicates that either N is not a
452 /// ConstantSDNode, or is not suitable for use by that opcode.  A return value
453 /// of 1 indicates that the constant may be used in normal immediate form.  A
454 /// return value of 2 indicates that the constant may be used in shifted
455 /// immediate form.  A return value of 3 indicates that log base 2 of the
456 /// constant may be used.
457 ///
458 static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
459                                       unsigned& Imm, bool U = false) {
460   if (N.getOpcode() != ISD::Constant) return 0;
461
462   int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
463
464   switch(Opcode) {
465   default: return 0;
466   case ISD::ADD:
467     if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
468     if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
469     break;
470   case ISD::AND:
471   case ISD::XOR:
472   case ISD::OR:
473     if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
474     if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
475     break;
476   case ISD::MUL:
477   case ISD::SUB:
478     if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
479     break;
480   case ISD::SETCC:
481     if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
482     if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
483     break;
484   case ISD::SDIV:
485     if ((Imm = ExactLog2(v))) { return 3; }
486     break;
487   }
488   return 0;
489 }
490
491 /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
492 /// to Condition.  If the Condition is unordered or unsigned, the bool argument
493 /// U is set to true, otherwise it is set to false.
494 static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
495   U = false;
496   switch (Condition) {
497   default: assert(0 && "Unknown condition!"); abort();
498   case ISD::SETEQ:  return PPC::BEQ;
499   case ISD::SETNE:  return PPC::BNE;
500   case ISD::SETULT: U = true;
501   case ISD::SETLT:  return PPC::BLT;
502   case ISD::SETULE: U = true;
503   case ISD::SETLE:  return PPC::BLE;
504   case ISD::SETUGT: U = true;
505   case ISD::SETGT:  return PPC::BGT;
506   case ISD::SETUGE: U = true;
507   case ISD::SETGE:  return PPC::BGE;
508   }
509   return 0;
510 }
511
512 /// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
513 /// and store immediate instructions.
514 static unsigned IndexedOpForOp(unsigned Opcode) {
515   switch(Opcode) {
516   default: assert(0 && "Unknown opcode!"); abort();
517   case PPC::LBZ: return PPC::LBZX;  case PPC::STB: return PPC::STBX;
518   case PPC::LHZ: return PPC::LHZX;  case PPC::STH: return PPC::STHX;
519   case PPC::LHA: return PPC::LHAX;  case PPC::STW: return PPC::STWX;
520   case PPC::LWZ: return PPC::LWZX;  case PPC::STD: return PPC::STDX;
521   case PPC::LD:  return PPC::LDX;   case PPC::STFS: return PPC::STFSX;
522   case PPC::LFS: return PPC::LFSX;  case PPC::STFD: return PPC::STFDX;
523   case PPC::LFD: return PPC::LFDX;
524   }
525   return 0;
526 }
527 }
528
529 /// getGlobalBaseReg - Output the instructions required to put the
530 /// base address to use for accessing globals into a register.
531 ///
532 unsigned ISel::getGlobalBaseReg() {
533   if (!GlobalBaseInitialized) {
534     // Insert the set of GlobalBaseReg into the first MBB of the function
535     MachineBasicBlock &FirstMBB = BB->getParent()->front();
536     MachineBasicBlock::iterator MBBI = FirstMBB.begin();
537     GlobalBaseReg = MakeReg(MVT::i64);
538     BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
539     BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
540     GlobalBaseInitialized = true;
541   }
542   return GlobalBaseReg;
543 }
544
545 /// getConstDouble - Loads a floating point value into a register, via the
546 /// Constant Pool.  Optionally takes a register in which to load the value.
547 unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
548   unsigned Tmp1 = MakeReg(MVT::i64);
549   if (0 == Result) Result = MakeReg(MVT::f64);
550   MachineConstantPool *CP = BB->getParent()->getConstantPool();
551   ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
552   unsigned CPI = CP->getConstantPoolIndex(CFP);
553   BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
554     .addConstantPoolIndex(CPI);
555   BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
556   return Result;
557 }
558
559 unsigned ISel::SelectSetCR0(SDOperand CC) {
560   unsigned Opc, Tmp1, Tmp2;
561   static const unsigned CompareOpcodes[] =
562     { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
563
564   // If the first operand to the select is a SETCC node, then we can fold it
565   // into the branch that selects which value to return.
566   SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
567   if (SetCC && CC.getOpcode() == ISD::SETCC) {
568     bool U;
569     Opc = getBCCForSetCC(SetCC->getCondition(), U);
570     Tmp1 = SelectExpr(SetCC->getOperand(0));
571
572     // Pass the optional argument U to getImmediateForOpcode for SETCC,
573     // so that it knows whether the SETCC immediate range is signed or not.
574     if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
575                                    Tmp2, U)) {
576       if (U)
577         BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
578       else
579         BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
580     } else {
581       bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
582       unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
583       Tmp2 = SelectExpr(SetCC->getOperand(1));
584       BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
585     }
586   } else {
587     Tmp1 = SelectExpr(CC);
588     BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
589     Opc = PPC::BNE;
590   }
591   return Opc;
592 }
593
594 /// Check to see if the load is a constant offset from a base register
595 bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
596 {
597   unsigned imm = 0, opcode = N.getOpcode();
598   if (N.getOpcode() == ISD::ADD) {
599     Reg = SelectExpr(N.getOperand(0));
600     if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
601       offset = imm;
602       return false;
603     }
604     offset = SelectExpr(N.getOperand(1));
605     return true;
606   }
607   Reg = SelectExpr(N);
608   offset = 0;
609   return false;
610 }
611
612 void ISel::SelectBranchCC(SDOperand N)
613 {
614   assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
615   MachineBasicBlock *Dest =
616     cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
617
618   // Get the MBB we will fall through to so that we can hand it off to the
619   // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
620   //ilist<MachineBasicBlock>::iterator It = BB;
621   //MachineBasicBlock *Fallthrough = ++It;
622
623   Select(N.getOperand(0));  //chain
624   unsigned Opc = SelectSetCR0(N.getOperand(1));
625   // FIXME: Use this once we have something approximating two-way branches
626   // We cannot currently use this in case the ISel hands us something like
627   // BRcc MBBx
628   // BR MBBy
629   // since the fallthrough basic block for the conditional branch does not start
630   // with the unconditional branch (it is skipped over).
631   //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
632   //  .addMBB(Dest).addMBB(Fallthrough);
633   BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
634   return;
635 }
636
637 unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
638 {
639   unsigned Tmp1, Tmp2, Tmp3;
640   unsigned Opc = 0;
641   SDNode *Node = N.Val;
642   MVT::ValueType DestType = N.getValueType();
643   unsigned opcode = N.getOpcode();
644
645   switch (opcode) {
646   default:
647     Node->dump();
648     assert(0 && "Node not handled!\n");
649
650   case ISD::SELECT: {
651     // Attempt to generate FSEL.  We can do this whenever we have an FP result,
652     // and an FP comparison in the SetCC node.
653     SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
654     if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
655         !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
656         SetCC->getCondition() != ISD::SETEQ &&
657         SetCC->getCondition() != ISD::SETNE) {
658       MVT::ValueType VT = SetCC->getOperand(0).getValueType();
659       Tmp1 = SelectExpr(SetCC->getOperand(0));   // Val to compare against
660       unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
661       unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
662
663       ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
664       if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
665         switch(SetCC->getCondition()) {
666         default: assert(0 && "Invalid FSEL condition"); abort();
667         case ISD::SETULT:
668         case ISD::SETLT:
669           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
670           return Result;
671         case ISD::SETUGE:
672         case ISD::SETGE:
673           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
674           return Result;
675         case ISD::SETUGT:
676         case ISD::SETGT: {
677           Tmp2 = MakeReg(VT);
678           BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
679           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
680           return Result;
681         }
682         case ISD::SETULE:
683         case ISD::SETLE: {
684           Tmp2 = MakeReg(VT);
685           BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
686           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
687           return Result;
688         }
689         }
690       } else {
691         Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
692         Tmp2 = SelectExpr(SetCC->getOperand(1));
693         Tmp3 =  MakeReg(VT);
694         switch(SetCC->getCondition()) {
695         default: assert(0 && "Invalid FSEL condition"); abort();
696         case ISD::SETULT:
697         case ISD::SETLT:
698           BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
699           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
700           return Result;
701         case ISD::SETUGE:
702         case ISD::SETGE:
703           BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
704           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
705           return Result;
706         case ISD::SETUGT:
707         case ISD::SETGT:
708           BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
709           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
710           return Result;
711         case ISD::SETULE:
712         case ISD::SETLE:
713           BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
714           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
715           return Result;
716         }
717       }
718       assert(0 && "Should never get here");
719       return 0;
720     }
721
722     unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
723     unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
724     Opc = SelectSetCR0(N.getOperand(0));
725
726     // Create an iterator with which to insert the MBB for copying the false
727     // value and the MBB to hold the PHI instruction for this SetCC.
728     MachineBasicBlock *thisMBB = BB;
729     const BasicBlock *LLVM_BB = BB->getBasicBlock();
730     ilist<MachineBasicBlock>::iterator It = BB;
731     ++It;
732
733     //  thisMBB:
734     //  ...
735     //   TrueVal = ...
736     //   cmpTY cr0, r1, r2
737     //   bCC copy1MBB
738     //   fallthrough --> copy0MBB
739     MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
740     MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
741     BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
742     MachineFunction *F = BB->getParent();
743     F->getBasicBlockList().insert(It, copy0MBB);
744     F->getBasicBlockList().insert(It, sinkMBB);
745     // Update machine-CFG edges
746     BB->addSuccessor(copy0MBB);
747     BB->addSuccessor(sinkMBB);
748
749     //  copy0MBB:
750     //   %FalseValue = ...
751     //   # fallthrough to sinkMBB
752     BB = copy0MBB;
753     // Update machine-CFG edges
754     BB->addSuccessor(sinkMBB);
755
756     //  sinkMBB:
757     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
758     //  ...
759     BB = sinkMBB;
760     BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
761       .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
762     return Result;
763   }
764
765   case ISD::FNEG:
766     if (!NoExcessFPPrecision &&
767         ISD::ADD == N.getOperand(0).getOpcode() &&
768         N.getOperand(0).Val->hasOneUse() &&
769         ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
770         N.getOperand(0).getOperand(0).Val->hasOneUse()) {
771       ++FusedFP; // Statistic
772       Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
773       Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
774       Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
775       Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
776       BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
777     } else if (!NoExcessFPPrecision &&
778         ISD::SUB == N.getOperand(0).getOpcode() &&
779         N.getOperand(0).Val->hasOneUse() &&
780         ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
781         N.getOperand(0).getOperand(0).Val->hasOneUse()) {
782       ++FusedFP; // Statistic
783       Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
784       Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
785       Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
786       Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
787       BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
788     } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
789       Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
790       BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
791     } else {
792       Tmp1 = SelectExpr(N.getOperand(0));
793       BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
794     }
795     return Result;
796
797   case ISD::FABS:
798     Tmp1 = SelectExpr(N.getOperand(0));
799     BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
800     return Result;
801
802   case ISD::FP_ROUND:
803     assert (DestType == MVT::f32 &&
804             N.getOperand(0).getValueType() == MVT::f64 &&
805             "only f64 to f32 conversion supported here");
806     Tmp1 = SelectExpr(N.getOperand(0));
807     BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
808     return Result;
809
810   case ISD::FP_EXTEND:
811     assert (DestType == MVT::f64 &&
812             N.getOperand(0).getValueType() == MVT::f32 &&
813             "only f32 to f64 conversion supported here");
814     Tmp1 = SelectExpr(N.getOperand(0));
815     BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
816     return Result;
817
818   case ISD::CopyFromReg:
819     if (Result == 1)
820       Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
821     Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
822     BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
823     return Result;
824
825   case ISD::ConstantFP: {
826     ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
827     Result = getConstDouble(CN->getValue(), Result);
828     return Result;
829   }
830
831   case ISD::ADD:
832     if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
833         N.getOperand(0).Val->hasOneUse()) {
834       ++FusedFP; // Statistic
835       Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
836       Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
837       Tmp3 = SelectExpr(N.getOperand(1));
838       Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
839       BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
840       return Result;
841     }
842     Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
843     Tmp1 = SelectExpr(N.getOperand(0));
844     Tmp2 = SelectExpr(N.getOperand(1));
845     BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
846     return Result;
847
848   case ISD::SUB:
849     if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
850         N.getOperand(0).Val->hasOneUse()) {
851       ++FusedFP; // Statistic
852       Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
853       Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
854       Tmp3 = SelectExpr(N.getOperand(1));
855       Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
856       BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
857       return Result;
858     }
859     Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
860     Tmp1 = SelectExpr(N.getOperand(0));
861     Tmp2 = SelectExpr(N.getOperand(1));
862     BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
863     return Result;
864
865   case ISD::MUL:
866   case ISD::SDIV:
867     switch( opcode ) {
868     case ISD::MUL:  Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
869     case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
870     };
871     Tmp1 = SelectExpr(N.getOperand(0));
872     Tmp2 = SelectExpr(N.getOperand(1));
873     BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
874     return Result;
875
876   case ISD::UINT_TO_FP:
877   case ISD::SINT_TO_FP: {
878     bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
879     Tmp1 = SelectExpr(N.getOperand(0));  // Get the operand register
880     Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
881     Tmp3 = MakeReg(MVT::i64); // temp reg to hold the conversion constant
882     unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
883
884     int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
885     MachineConstantPool *CP = BB->getParent()->getConstantPool();
886
887     // FIXME: pull this FP constant generation stuff out into something like
888     // the simple ISel's getReg.
889     if (IsUnsigned) {
890       addFrameReference(BuildMI(BB, PPC::STD, 3).addReg(Tmp1), FrameIdx);
891       addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
892       BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp2);
893     } else {
894       ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
895       unsigned CPI = CP->getConstantPoolIndex(CFP);
896       // Load constant fp value
897       unsigned Tmp4 = MakeReg(MVT::i32);
898       unsigned TmpL = MakeReg(MVT::i32);
899       BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
900         .addConstantPoolIndex(CPI);
901       BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
902       // Store the hi & low halves of the fp value, currently in int regs
903       BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
904       addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
905       BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
906       addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
907       addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
908       // Generate the return value with a subtract
909       BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
910     }
911     return Result;
912   }
913   }
914   assert(0 && "Should never get here");
915   return 0;
916 }
917
918 unsigned ISel::SelectExpr(SDOperand N) {
919   unsigned Result;
920   unsigned Tmp1, Tmp2, Tmp3;
921   unsigned Opc = 0;
922   unsigned opcode = N.getOpcode();
923
924   SDNode *Node = N.Val;
925   MVT::ValueType DestType = N.getValueType();
926
927   unsigned &Reg = ExprMap[N];
928   if (Reg) return Reg;
929
930   switch (N.getOpcode()) {
931   default:
932     Reg = Result = (N.getValueType() != MVT::Other) ?
933                             MakeReg(N.getValueType()) : 1;
934     break;
935   case ISD::CALL:
936     // If this is a call instruction, make sure to prepare ALL of the result
937     // values as well as the chain.
938     if (Node->getNumValues() == 1)
939       Reg = Result = 1;  // Void call, just a chain.
940     else {
941       Result = MakeReg(Node->getValueType(0));
942       ExprMap[N.getValue(0)] = Result;
943       for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
944         ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
945       ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
946     }
947     break;
948   }
949
950   if (ISD::CopyFromReg == opcode)
951     DestType = N.getValue(0).getValueType();
952
953   if (DestType == MVT::f64 || DestType == MVT::f32)
954     if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
955       return SelectExprFP(N, Result);
956
957   switch (opcode) {
958   default:
959     Node->dump();
960     assert(0 && "Node not handled!\n");
961   case ISD::UNDEF:
962     BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
963     return Result;
964   case ISD::DYNAMIC_STACKALLOC:
965     // Generate both result values.  FIXME: Need a better commment here?
966     if (Result != 1)
967       ExprMap[N.getValue(1)] = 1;
968     else
969       Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
970
971     // FIXME: We are currently ignoring the requested alignment for handling
972     // greater than the stack alignment.  This will need to be revisited at some
973     // point.  Align = N.getOperand(2);
974     if (!isa<ConstantSDNode>(N.getOperand(2)) ||
975         cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
976       std::cerr << "Cannot allocate stack object with greater alignment than"
977                 << " the stack alignment yet!";
978       abort();
979     }
980     Select(N.getOperand(0));
981     Tmp1 = SelectExpr(N.getOperand(1));
982     // Subtract size from stack pointer, thereby allocating some space.
983     BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
984     // Put a pointer to the space into the result register by copying the SP
985     BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
986     return Result;
987
988   case ISD::ConstantPool:
989     Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
990     Tmp2 = MakeReg(MVT::i64);
991     BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
992       .addConstantPoolIndex(Tmp1);
993     BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
994     return Result;
995
996   case ISD::FrameIndex:
997     Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
998     addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
999     return Result;
1000
1001   case ISD::GlobalAddress: {
1002     GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1003     Tmp1 = MakeReg(MVT::i64);
1004     BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1005       .addGlobalAddress(GV);
1006     if (GV->hasWeakLinkage() || GV->isExternal()) {
1007       BuildMI(BB, PPC::LD, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1008     } else {
1009       BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1010     }
1011     return Result;
1012   }
1013
1014   case ISD::LOAD:
1015   case ISD::EXTLOAD:
1016   case ISD::ZEXTLOAD:
1017   case ISD::SEXTLOAD: {
1018     MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1019       Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
1020     bool sext = (ISD::SEXTLOAD == opcode);
1021
1022     // Make sure we generate both values.
1023     if (Result != 1)
1024       ExprMap[N.getValue(1)] = 1;   // Generate the token
1025     else
1026       Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1027
1028     SDOperand Chain   = N.getOperand(0);
1029     SDOperand Address = N.getOperand(1);
1030     Select(Chain);
1031
1032     switch (TypeBeingLoaded) {
1033     default: Node->dump(); assert(0 && "Cannot load this type!");
1034     case MVT::i1:  Opc = PPC::LBZ; break;
1035     case MVT::i8:  Opc = PPC::LBZ; break;
1036     case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1037     case MVT::i32: Opc = sext ? PPC::LWA : PPC::LWZ; break;
1038     case MVT::i64: Opc = PPC::LD;  break;
1039     case MVT::f32: Opc = PPC::LFS; break;
1040     case MVT::f64: Opc = PPC::LFD; break;
1041     }
1042
1043     if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1044       Tmp1 = MakeReg(MVT::i64);
1045       int CPI = CP->getIndex();
1046       BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1047         .addConstantPoolIndex(CPI);
1048       BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
1049     }
1050     else if(Address.getOpcode() == ISD::FrameIndex) {
1051       Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1052       addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
1053     } else {
1054       int offset;
1055       bool idx = SelectAddr(Address, Tmp1, offset);
1056       if (idx) {
1057         Opc = IndexedOpForOp(Opc);
1058         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1059       } else {
1060         BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1061       }
1062     }
1063     return Result;
1064   }
1065
1066   case ISD::CALL: {
1067     unsigned GPR_idx = 0, FPR_idx = 0;
1068     static const unsigned GPR[] = {
1069       PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1070       PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1071     };
1072     static const unsigned FPR[] = {
1073       PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1074       PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1075     };
1076
1077     // Lower the chain for this call.
1078     Select(N.getOperand(0));
1079     ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1080
1081     MachineInstr *CallMI;
1082     // Emit the correct call instruction based on the type of symbol called.
1083     if (GlobalAddressSDNode *GASD =
1084         dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1085       CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1086                                                            true);
1087     } else if (ExternalSymbolSDNode *ESSDN =
1088                dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1089       CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1090                                                             true);
1091     } else {
1092       Tmp1 = SelectExpr(N.getOperand(1));
1093       BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1094       BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1095       CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1096         .addReg(PPC::R12);
1097     }
1098
1099     // Load the register args to virtual regs
1100     std::vector<unsigned> ArgVR;
1101     for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1102       ArgVR.push_back(SelectExpr(N.getOperand(i)));
1103
1104     // Copy the virtual registers into the appropriate argument register
1105     for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1106       switch(N.getOperand(i+2).getValueType()) {
1107       default: Node->dump(); assert(0 && "Unknown value type for call");
1108       case MVT::i1:
1109       case MVT::i8:
1110       case MVT::i16:
1111       case MVT::i32:
1112       case MVT::i64:
1113         assert(GPR_idx < 8 && "Too many int args");
1114         if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1115           BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1116           CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1117         }
1118         ++GPR_idx;
1119         break;
1120       case MVT::f64:
1121       case MVT::f32:
1122         assert(FPR_idx < 13 && "Too many fp args");
1123         BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1124         CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1125         ++FPR_idx;
1126         break;
1127       }
1128     }
1129
1130     // Put the call instruction in the correct place in the MachineBasicBlock
1131     BB->push_back(CallMI);
1132
1133     switch (Node->getValueType(0)) {
1134     default: assert(0 && "Unknown value type for call result!");
1135     case MVT::Other: return 1;
1136     case MVT::i1:
1137     case MVT::i8:
1138     case MVT::i16:
1139     case MVT::i32:
1140     case MVT::i64:
1141       BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1142       break;
1143     case MVT::f32:
1144     case MVT::f64:
1145       BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1146       break;
1147     }
1148     return Result+N.ResNo;
1149   }
1150
1151   case ISD::SIGN_EXTEND:
1152   case ISD::SIGN_EXTEND_INREG:
1153     Tmp1 = SelectExpr(N.getOperand(0));
1154     switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1155     default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1156     case MVT::i32:
1157       BuildMI(BB, PPC::EXTSW, 1, Result).addReg(Tmp1);
1158       break;
1159     case MVT::i16:
1160       BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1161       break;
1162     case MVT::i8:
1163       BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1164       break;
1165     case MVT::i1:
1166       BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1167       break;
1168     }
1169     return Result;
1170
1171   case ISD::CopyFromReg:
1172     if (Result == 1)
1173       Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1174     Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1175     BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1176     return Result;
1177
1178   case ISD::SHL:
1179     Tmp1 = SelectExpr(N.getOperand(0));
1180     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1181       Tmp2 = CN->getValue() & 0x3F;
1182       BuildMI(BB, PPC::RLDICR, 3, Result).addReg(Tmp1).addImm(Tmp2)
1183         .addImm(63-Tmp2);
1184     } else {
1185       Tmp2 = SelectExpr(N.getOperand(1));
1186       BuildMI(BB, PPC::SLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1187     }
1188     return Result;
1189
1190   case ISD::SRL:
1191     Tmp1 = SelectExpr(N.getOperand(0));
1192     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1193       Tmp2 = CN->getValue() & 0x3F;
1194       BuildMI(BB, PPC::RLDICL, 3, Result).addReg(Tmp1).addImm(64-Tmp2)
1195         .addImm(Tmp2);
1196     } else {
1197       Tmp2 = SelectExpr(N.getOperand(1));
1198       BuildMI(BB, PPC::SRD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1199     }
1200     return Result;
1201
1202   case ISD::SRA:
1203     Tmp1 = SelectExpr(N.getOperand(0));
1204     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1205       Tmp2 = CN->getValue() & 0x3F;
1206       BuildMI(BB, PPC::SRADI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1207     } else {
1208       Tmp2 = SelectExpr(N.getOperand(1));
1209       BuildMI(BB, PPC::SRAD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1210     }
1211     return Result;
1212
1213   case ISD::ADD:
1214     Tmp1 = SelectExpr(N.getOperand(0));
1215     switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1216       default: assert(0 && "unhandled result code");
1217       case 0: // No immediate
1218         Tmp2 = SelectExpr(N.getOperand(1));
1219         BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1220         break;
1221       case 1: // Low immediate
1222         BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1223         break;
1224       case 2: // Shifted immediate
1225         BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1226         break;
1227     }
1228     return Result;
1229
1230   case ISD::AND:
1231   case ISD::OR:
1232     Tmp1 = SelectExpr(N.getOperand(0));
1233     switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1234       default: assert(0 && "unhandled result code");
1235       case 0: // No immediate
1236         Tmp2 = SelectExpr(N.getOperand(1));
1237         switch (opcode) {
1238         case ISD::AND: Opc = PPC::AND; break;
1239         case ISD::OR:  Opc = PPC::OR;  break;
1240         }
1241         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1242         break;
1243       case 1: // Low immediate
1244         switch (opcode) {
1245         case ISD::AND: Opc = PPC::ANDIo; break;
1246         case ISD::OR:  Opc = PPC::ORI;   break;
1247         }
1248         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1249         break;
1250       case 2: // Shifted immediate
1251         switch (opcode) {
1252         case ISD::AND: Opc = PPC::ANDISo;  break;
1253         case ISD::OR:  Opc = PPC::ORIS;    break;
1254         }
1255         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1256         break;
1257     }
1258     return Result;
1259
1260   case ISD::XOR: {
1261     // Check for EQV: xor, (xor a, -1), b
1262     if (N.getOperand(0).getOpcode() == ISD::XOR &&
1263         N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1264         cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1265       ++NotLogic;
1266       Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1267       Tmp2 = SelectExpr(N.getOperand(1));
1268       BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1269       return Result;
1270     }
1271     // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1272     if (N.getOperand(1).getOpcode() == ISD::Constant &&
1273         cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1274       ++NotLogic;
1275       switch(N.getOperand(0).getOpcode()) {
1276       case ISD::OR:
1277         Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1278         Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1279         BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1280         break;
1281       case ISD::AND:
1282         Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1283         Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1284         BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1285         break;
1286       default:
1287         Tmp1 = SelectExpr(N.getOperand(0));
1288         BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1289         break;
1290       }
1291       return Result;
1292     }
1293     Tmp1 = SelectExpr(N.getOperand(0));
1294     switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1295       default: assert(0 && "unhandled result code");
1296       case 0: // No immediate
1297         Tmp2 = SelectExpr(N.getOperand(1));
1298         BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1299         break;
1300       case 1: // Low immediate
1301         BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1302         break;
1303       case 2: // Shifted immediate
1304         BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1305         break;
1306     }
1307     return Result;
1308   }
1309
1310   case ISD::SUB:
1311     Tmp2 = SelectExpr(N.getOperand(1));
1312     if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1313       BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1314     else {
1315       Tmp1 = SelectExpr(N.getOperand(0));
1316       BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1317     }
1318     return Result;
1319
1320   case ISD::MUL:
1321     Tmp1 = SelectExpr(N.getOperand(0));
1322     if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1323       BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1324     else {
1325       Tmp2 = SelectExpr(N.getOperand(1));
1326       BuildMI(BB, PPC::MULLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1327     }
1328     return Result;
1329
1330   case ISD::SDIV:
1331   case ISD::UDIV:
1332     if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1333       Tmp1 = MakeReg(MVT::i64);
1334       Tmp2 = SelectExpr(N.getOperand(0));
1335       BuildMI(BB, PPC::SRADI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1336       BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1337       return Result;
1338     }
1339     Tmp1 = SelectExpr(N.getOperand(0));
1340     Tmp2 = SelectExpr(N.getOperand(1));
1341     Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1342     BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1343     return Result;
1344
1345   case ISD::FP_TO_UINT:
1346   case ISD::FP_TO_SINT: {
1347     Tmp1 = SelectExpr(N.getOperand(0));
1348     Tmp2 = MakeReg(MVT::f64);
1349     BuildMI(BB, PPC::FCTIDZ, 1, Tmp2).addReg(Tmp1);
1350     int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1351     addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1352     addFrameReference(BuildMI(BB, PPC::LD, 2, Result), FrameIdx);
1353     return Result;
1354   }
1355
1356   case ISD::SETCC:
1357     if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1358       Opc = SelectSetCR0(N);
1359
1360       unsigned TrueValue = MakeReg(MVT::i32);
1361       BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1362       unsigned FalseValue = MakeReg(MVT::i32);
1363       BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1364
1365       // Create an iterator with which to insert the MBB for copying the false
1366       // value and the MBB to hold the PHI instruction for this SetCC.
1367       MachineBasicBlock *thisMBB = BB;
1368       const BasicBlock *LLVM_BB = BB->getBasicBlock();
1369       ilist<MachineBasicBlock>::iterator It = BB;
1370       ++It;
1371
1372       //  thisMBB:
1373       //  ...
1374       //   cmpTY cr0, r1, r2
1375       //   %TrueValue = li 1
1376       //   bCC sinkMBB
1377       MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1378       MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1379       BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1380       MachineFunction *F = BB->getParent();
1381       F->getBasicBlockList().insert(It, copy0MBB);
1382       F->getBasicBlockList().insert(It, sinkMBB);
1383       // Update machine-CFG edges
1384       BB->addSuccessor(copy0MBB);
1385       BB->addSuccessor(sinkMBB);
1386
1387       //  copy0MBB:
1388       //   %FalseValue = li 0
1389       //   fallthrough
1390       BB = copy0MBB;
1391       // Update machine-CFG edges
1392       BB->addSuccessor(sinkMBB);
1393
1394       //  sinkMBB:
1395       //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1396       //  ...
1397       BB = sinkMBB;
1398       BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1399         .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1400       return Result;
1401     }
1402     assert(0 && "Is this legal?");
1403     return 0;
1404
1405   case ISD::SELECT: {
1406     unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1407     unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1408     Opc = SelectSetCR0(N.getOperand(0));
1409
1410     // Create an iterator with which to insert the MBB for copying the false
1411     // value and the MBB to hold the PHI instruction for this SetCC.
1412     MachineBasicBlock *thisMBB = BB;
1413     const BasicBlock *LLVM_BB = BB->getBasicBlock();
1414     ilist<MachineBasicBlock>::iterator It = BB;
1415     ++It;
1416
1417     //  thisMBB:
1418     //  ...
1419     //   TrueVal = ...
1420     //   cmpTY cr0, r1, r2
1421     //   bCC copy1MBB
1422     //   fallthrough --> copy0MBB
1423     MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1424     MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1425     BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1426     MachineFunction *F = BB->getParent();
1427     F->getBasicBlockList().insert(It, copy0MBB);
1428     F->getBasicBlockList().insert(It, sinkMBB);
1429     // Update machine-CFG edges
1430     BB->addSuccessor(copy0MBB);
1431     BB->addSuccessor(sinkMBB);
1432
1433     //  copy0MBB:
1434     //   %FalseValue = ...
1435     //   # fallthrough to sinkMBB
1436     BB = copy0MBB;
1437     // Update machine-CFG edges
1438     BB->addSuccessor(sinkMBB);
1439
1440     //  sinkMBB:
1441     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1442     //  ...
1443     BB = sinkMBB;
1444     BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1445       .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1446
1447     // FIXME: Select i64?
1448     return Result;
1449   }
1450
1451   case ISD::Constant:
1452     switch (N.getValueType()) {
1453     default: assert(0 && "Cannot use constants of this type!");
1454     case MVT::i1:
1455       BuildMI(BB, PPC::LI, 1, Result)
1456         .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1457       break;
1458     case MVT::i32:
1459       {
1460         int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1461         if (v < 32768 && v >= -32768) {
1462           BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1463         } else {
1464           Tmp1 = MakeReg(MVT::i32);
1465           BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1466           BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
1467         }
1468       }
1469     }
1470     return Result;
1471   }
1472
1473   return 0;
1474 }
1475
1476 void ISel::Select(SDOperand N) {
1477   unsigned Tmp1, Tmp2, Opc;
1478   unsigned opcode = N.getOpcode();
1479
1480   if (!ExprMap.insert(std::make_pair(N, 1)).second)
1481     return;  // Already selected.
1482
1483   SDNode *Node = N.Val;
1484
1485   switch (Node->getOpcode()) {
1486   default:
1487     Node->dump(); std::cerr << "\n";
1488     assert(0 && "Node not handled yet!");
1489   case ISD::EntryToken: return;  // Noop
1490   case ISD::TokenFactor:
1491     for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1492       Select(Node->getOperand(i));
1493     return;
1494   case ISD::ADJCALLSTACKDOWN:
1495   case ISD::ADJCALLSTACKUP:
1496     Select(N.getOperand(0));
1497     Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1498     Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1499       PPC::ADJCALLSTACKUP;
1500     BuildMI(BB, Opc, 1).addImm(Tmp1);
1501     return;
1502   case ISD::BR: {
1503     MachineBasicBlock *Dest =
1504       cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1505     Select(N.getOperand(0));
1506     BuildMI(BB, PPC::B, 1).addMBB(Dest);
1507     return;
1508   }
1509   case ISD::BRCOND:
1510     SelectBranchCC(N);
1511     return;
1512   case ISD::CopyToReg:
1513     Select(N.getOperand(0));
1514     Tmp1 = SelectExpr(N.getOperand(1));
1515     Tmp2 = cast<RegSDNode>(N)->getReg();
1516
1517     if (Tmp1 != Tmp2) {
1518       if (N.getOperand(1).getValueType() == MVT::f64 ||
1519           N.getOperand(1).getValueType() == MVT::f32)
1520         BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1521       else
1522         BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1523     }
1524     return;
1525   case ISD::ImplicitDef:
1526     Select(N.getOperand(0));
1527     BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1528     return;
1529   case ISD::RET:
1530     switch (N.getNumOperands()) {
1531     default:
1532       assert(0 && "Unknown return instruction!");
1533     case 3:
1534       assert(N.getOperand(1).getValueType() == MVT::i32 &&
1535              N.getOperand(2).getValueType() == MVT::i32 &&
1536              "Unknown two-register value!");
1537       Select(N.getOperand(0));
1538       Tmp1 = SelectExpr(N.getOperand(1));
1539       Tmp2 = SelectExpr(N.getOperand(2));
1540       BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1541       BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
1542       break;
1543     case 2:
1544       Select(N.getOperand(0));
1545       Tmp1 = SelectExpr(N.getOperand(1));
1546       switch (N.getOperand(1).getValueType()) {
1547         default:
1548           assert(0 && "Unknown return type!");
1549         case MVT::f64:
1550         case MVT::f32:
1551           BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1552           break;
1553         case MVT::i32:
1554           BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1555           break;
1556       }
1557     case 1:
1558       Select(N.getOperand(0));
1559       break;
1560     }
1561     BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1562     return;
1563   case ISD::TRUNCSTORE:
1564   case ISD::STORE:
1565     {
1566       SDOperand Chain   = N.getOperand(0);
1567       SDOperand Value   = N.getOperand(1);
1568       SDOperand Address = N.getOperand(2);
1569       Select(Chain);
1570
1571       Tmp1 = SelectExpr(Value); //value
1572
1573       if (opcode == ISD::STORE) {
1574         switch(Value.getValueType()) {
1575         default: assert(0 && "unknown Type in store");
1576         case MVT::i64: Opc = PPC::STD; break;
1577         case MVT::f64: Opc = PPC::STFD; break;
1578         case MVT::f32: Opc = PPC::STFS; break;
1579         }
1580       } else { //ISD::TRUNCSTORE
1581         switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1582         default: assert(0 && "unknown Type in store");
1583         case MVT::i1: //FIXME: DAG does not promote this load
1584         case MVT::i8:  Opc= PPC::STB; break;
1585         case MVT::i16: Opc = PPC::STH; break;
1586         case MVT::i32: Opc = PPC::STW; break;
1587         }
1588       }
1589
1590       if(Address.getOpcode() == ISD::FrameIndex)
1591       {
1592         Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1593         addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
1594       }
1595       else
1596       {
1597         int offset;
1598         bool idx = SelectAddr(Address, Tmp2, offset);
1599         if (idx) {
1600           Opc = IndexedOpForOp(Opc);
1601           BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1602         } else {
1603           BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1604         }
1605       }
1606       return;
1607     }
1608   case ISD::EXTLOAD:
1609   case ISD::SEXTLOAD:
1610   case ISD::ZEXTLOAD:
1611   case ISD::LOAD:
1612   case ISD::CopyFromReg:
1613   case ISD::CALL:
1614   case ISD::DYNAMIC_STACKALLOC:
1615     ExprMap.erase(N);
1616     SelectExpr(N);
1617     return;
1618   }
1619   assert(0 && "Should not be reached!");
1620 }
1621
1622
1623 /// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1624 /// into a machine code representation using pattern matching and a machine
1625 /// description file.
1626 ///
1627 FunctionPass *llvm::createPPC64ISelPattern(TargetMachine &TM) {
1628   return new ISel(TM);
1629 }
1630