Minor cleanup patch, no functionality changes. Written by Jim Laskey.
[oota-llvm.git] / lib / Target / PowerPC / PPCISelPattern.cpp
1 //===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===//
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 32 bit PowerPC.
11 // Magic number generation for integer divide from the PowerPC Compiler Writer's
12 // Guide, section 3.2.3.5
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "PowerPC.h"
17 #include "PowerPCInstrBuilder.h"
18 #include "PowerPCInstrInfo.h"
19 #include "PPC32TargetMachine.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Function.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/SelectionDAG.h"
26 #include "llvm/CodeGen/SelectionDAGISel.h"
27 #include "llvm/CodeGen/SSARegMap.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetLowering.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/ADT/Statistic.h"
34 #include <set>
35 #include <algorithm>
36 using namespace llvm;
37
38
39 //===----------------------------------------------------------------------===//
40 //  PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
41 namespace {
42   class PPC32TargetLowering : public TargetLowering {
43     int VarArgsFrameIndex;            // FrameIndex for start of varargs area.
44     int ReturnAddrIndex;              // FrameIndex for return slot.
45   public:
46     PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
47       // Fold away setcc operations if possible.
48       setSetCCIsExpensive();
49
50       // Set up the register classes.
51       addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
52       addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
53       addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
54
55       // PowerPC has no intrinsics for these particular operations
56       setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
57       setOperationAction(ISD::MEMSET, MVT::Other, Expand);
58       setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
59
60       // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
61       setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
62       setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
63
64       // PowerPC has no SREM/UREM instructions
65       setOperationAction(ISD::SREM, MVT::i32, Expand);
66       setOperationAction(ISD::UREM, MVT::i32, Expand);
67
68       // We don't support sin/cos/sqrt/fmod
69       setOperationAction(ISD::FSIN , MVT::f64, Expand);
70       setOperationAction(ISD::FCOS , MVT::f64, Expand);
71       setOperationAction(ISD::SREM , MVT::f64, Expand);
72       setOperationAction(ISD::FSIN , MVT::f32, Expand);
73       setOperationAction(ISD::FCOS , MVT::f32, Expand);
74       setOperationAction(ISD::SREM , MVT::f32, Expand);
75
76       // If we're enabling GP optimizations, use hardware square root
77       if (!TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) {
78         setOperationAction(ISD::FSQRT, MVT::f64, Expand);
79         setOperationAction(ISD::FSQRT, MVT::f32, Expand);
80       }
81
82       //PowerPC does not have CTPOP or CTTZ
83       setOperationAction(ISD::CTPOP, MVT::i32  , Expand);
84       setOperationAction(ISD::CTTZ , MVT::i32  , Expand);
85
86       setSetCCResultContents(ZeroOrOneSetCCResult);
87       addLegalFPImmediate(+0.0); // Necessary for FSEL
88       addLegalFPImmediate(-0.0); //
89
90       computeRegisterProperties();
91     }
92
93     /// LowerArguments - This hook must be implemented to indicate how we should
94     /// lower the arguments for the specified function, into the specified DAG.
95     virtual std::vector<SDOperand>
96     LowerArguments(Function &F, SelectionDAG &DAG);
97
98     /// LowerCallTo - This hook lowers an abstract call to a function into an
99     /// actual call.
100     virtual std::pair<SDOperand, SDOperand>
101     LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
102                 bool isTailCall, SDOperand Callee, ArgListTy &Args,
103                 SelectionDAG &DAG);
104
105     virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
106                                    Value *VAListV, SelectionDAG &DAG);
107
108     virtual std::pair<SDOperand,SDOperand>
109       LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
110                  const Type *ArgTy, SelectionDAG &DAG);
111
112     virtual std::pair<SDOperand, SDOperand>
113     LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
114                             SelectionDAG &DAG);
115   };
116 }
117
118
119 std::vector<SDOperand>
120 PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
121   //
122   // add beautiful description of PPC stack frame format, or at least some docs
123   //
124   MachineFunction &MF = DAG.getMachineFunction();
125   MachineFrameInfo *MFI = MF.getFrameInfo();
126   MachineBasicBlock& BB = MF.front();
127   std::vector<SDOperand> ArgValues;
128
129   // Due to the rather complicated nature of the PowerPC ABI, rather than a
130   // fixed size array of physical args, for the sake of simplicity let the STL
131   // handle tracking them for us.
132   std::vector<unsigned> argVR, argPR, argOp;
133   unsigned ArgOffset = 24;
134   unsigned GPR_remaining = 8;
135   unsigned FPR_remaining = 13;
136   unsigned GPR_idx = 0, FPR_idx = 0;
137   static const unsigned GPR[] = {
138     PPC::R3, PPC::R4, PPC::R5, PPC::R6,
139     PPC::R7, PPC::R8, PPC::R9, PPC::R10,
140   };
141   static const unsigned FPR[] = {
142     PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
143     PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
144   };
145
146   // Add DAG nodes to load the arguments...  On entry to a function on PPC,
147   // the arguments start at offset 24, although they are likely to be passed
148   // in registers.
149   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
150     SDOperand newroot, argt;
151     unsigned ObjSize;
152     bool needsLoad = false;
153     bool ArgLive = !I->use_empty();
154     MVT::ValueType ObjectVT = getValueType(I->getType());
155
156     switch (ObjectVT) {
157     default: assert(0 && "Unhandled argument type!");
158     case MVT::i1:
159     case MVT::i8:
160     case MVT::i16:
161     case MVT::i32:
162       ObjSize = 4;
163       if (!ArgLive) break;
164       if (GPR_remaining > 0) {
165         MF.addLiveIn(GPR[GPR_idx]);
166         argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
167                                             DAG.getRoot());
168         if (ObjectVT != MVT::i32)
169           argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
170       } else {
171         needsLoad = true;
172       }
173       break;
174       case MVT::i64: ObjSize = 8;
175       if (!ArgLive) break;
176       if (GPR_remaining > 0) {
177         SDOperand argHi, argLo;
178         MF.addLiveIn(GPR[GPR_idx]);
179         argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
180         // If we have two or more remaining argument registers, then both halves
181         // of the i64 can be sourced from there.  Otherwise, the lower half will
182         // have to come off the stack.  This can happen when an i64 is preceded
183         // by 28 bytes of arguments.
184         if (GPR_remaining > 1) {
185           MF.addLiveIn(GPR[GPR_idx+1]);
186           argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
187         } else {
188           int FI = MFI->CreateFixedObject(4, ArgOffset+4);
189           SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
190           argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
191                               DAG.getSrcValue(NULL));
192         }
193         // Build the outgoing arg thingy
194         argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
195         newroot = argLo;
196       } else {
197         needsLoad = true;
198       }
199       break;
200       case MVT::f32:
201       case MVT::f64:
202       ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
203       if (!ArgLive) break;
204       if (FPR_remaining > 0) {
205         MF.addLiveIn(FPR[FPR_idx]);
206         argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
207                                             DAG.getRoot());
208         --FPR_remaining;
209         ++FPR_idx;
210       } else {
211         needsLoad = true;
212       }
213       break;
214     }
215
216     // We need to load the argument to a virtual register if we determined above
217     // that we ran out of physical registers of the appropriate type
218     if (needsLoad) {
219       unsigned SubregOffset = 0;
220       if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
221       if (ObjectVT == MVT::i16) SubregOffset = 2;
222       int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
223       SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
224       FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
225                         DAG.getConstant(SubregOffset, MVT::i32));
226       argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
227                                    DAG.getSrcValue(NULL));
228     }
229
230     // Every 4 bytes of argument space consumes one of the GPRs available for
231     // argument passing.
232     if (GPR_remaining > 0) {
233       unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
234       GPR_remaining -= delta;
235       GPR_idx += delta;
236     }
237     ArgOffset += ObjSize;
238     if (newroot.Val)
239       DAG.setRoot(newroot.getValue(1));
240
241     ArgValues.push_back(argt);
242   }
243
244   // If the function takes variable number of arguments, make a frame index for
245   // the start of the first vararg value... for expansion of llvm.va_start.
246   if (F.isVarArg()) {
247     VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
248     SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
249     // If this function is vararg, store any remaining integer argument regs
250     // to their spots on the stack so that they may be loaded by deferencing the
251     // result of va_next.
252     std::vector<SDOperand> MemOps;
253     for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
254       MF.addLiveIn(GPR[GPR_idx]);
255       SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
256       SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
257                                     Val, FIN, DAG.getSrcValue(NULL));
258       MemOps.push_back(Store);
259       // Increment the address by four for the next argument to store
260       SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
261       FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
262     }
263     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
264   }
265
266   // Finally, inform the code generator which regs we return values in.
267   switch (getValueType(F.getReturnType())) {
268   default: assert(0 && "Unknown type!");
269   case MVT::isVoid: break;
270   case MVT::i1:
271   case MVT::i8:
272   case MVT::i16:
273   case MVT::i32:
274     MF.addLiveOut(PPC::R3);
275     break;
276   case MVT::i64:
277     MF.addLiveOut(PPC::R3);
278     MF.addLiveOut(PPC::R4);
279     break;
280   case MVT::f32:
281   case MVT::f64:
282     MF.addLiveOut(PPC::F1);
283     break;
284   }
285
286   return ArgValues;
287 }
288
289 std::pair<SDOperand, SDOperand>
290 PPC32TargetLowering::LowerCallTo(SDOperand Chain,
291                                  const Type *RetTy, bool isVarArg,
292                                  unsigned CallingConv, bool isTailCall,
293                                  SDOperand Callee, ArgListTy &Args,
294                                  SelectionDAG &DAG) {
295   // args_to_use will accumulate outgoing args for the ISD::CALL case in
296   // SelectExpr to use to put the arguments in the appropriate registers.
297   std::vector<SDOperand> args_to_use;
298
299   // Count how many bytes are to be pushed on the stack, including the linkage
300   // area, and parameter passing area.
301   unsigned NumBytes = 24;
302
303   if (Args.empty()) {
304     Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
305                         DAG.getConstant(NumBytes, getPointerTy()));
306   } else {
307     for (unsigned i = 0, e = Args.size(); i != e; ++i)
308       switch (getValueType(Args[i].second)) {
309       default: assert(0 && "Unknown value type!");
310       case MVT::i1:
311       case MVT::i8:
312       case MVT::i16:
313       case MVT::i32:
314       case MVT::f32:
315         NumBytes += 4;
316         break;
317       case MVT::i64:
318       case MVT::f64:
319         NumBytes += 8;
320         break;
321       }
322
323     // Just to be safe, we'll always reserve the full 24 bytes of linkage area
324     // plus 32 bytes of argument space in case any called code gets funky on us.
325     // (Required by ABI to support var arg)
326     if (NumBytes < 56) NumBytes = 56;
327
328     // Adjust the stack pointer for the new arguments...
329     // These operations are automatically eliminated by the prolog/epilog pass
330     Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
331                         DAG.getConstant(NumBytes, getPointerTy()));
332
333     // Set up a copy of the stack pointer for use loading and storing any
334     // arguments that may not fit in the registers available for argument
335     // passing.
336     SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
337                                             DAG.getEntryNode());
338
339     // Figure out which arguments are going to go in registers, and which in
340     // memory.  Also, if this is a vararg function, floating point operations
341     // must be stored to our stack, and loaded into integer regs as well, if
342     // any integer regs are available for argument passing.
343     unsigned ArgOffset = 24;
344     unsigned GPR_remaining = 8;
345     unsigned FPR_remaining = 13;
346
347     std::vector<SDOperand> MemOps;
348     for (unsigned i = 0, e = Args.size(); i != e; ++i) {
349       // PtrOff will be used to store the current argument to the stack if a
350       // register cannot be found for it.
351       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
352       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
353       MVT::ValueType ArgVT = getValueType(Args[i].second);
354
355       switch (ArgVT) {
356       default: assert(0 && "Unexpected ValueType for argument!");
357       case MVT::i1:
358       case MVT::i8:
359       case MVT::i16:
360         // Promote the integer to 32 bits.  If the input type is signed use a
361         // sign extend, otherwise use a zero extend.
362         if (Args[i].second->isSigned())
363           Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
364         else
365           Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
366         // FALL THROUGH
367       case MVT::i32:
368         if (GPR_remaining > 0) {
369           args_to_use.push_back(Args[i].first);
370           --GPR_remaining;
371         } else {
372           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
373                                        Args[i].first, PtrOff,
374                                        DAG.getSrcValue(NULL)));
375         }
376         ArgOffset += 4;
377         break;
378       case MVT::i64:
379         // If we have one free GPR left, we can place the upper half of the i64
380         // in it, and store the other half to the stack.  If we have two or more
381         // free GPRs, then we can pass both halves of the i64 in registers.
382         if (GPR_remaining > 0) {
383           SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
384             Args[i].first, DAG.getConstant(1, MVT::i32));
385           SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
386             Args[i].first, DAG.getConstant(0, MVT::i32));
387           args_to_use.push_back(Hi);
388           --GPR_remaining;
389           if (GPR_remaining > 0) {
390             args_to_use.push_back(Lo);
391             --GPR_remaining;
392           } else {
393             SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
394             PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
395             MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
396                                          Lo, PtrOff, DAG.getSrcValue(NULL)));
397           }
398         } else {
399           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
400                                        Args[i].first, PtrOff,
401                                        DAG.getSrcValue(NULL)));
402         }
403         ArgOffset += 8;
404         break;
405       case MVT::f32:
406       case MVT::f64:
407         if (FPR_remaining > 0) {
408           args_to_use.push_back(Args[i].first);
409           --FPR_remaining;
410           if (isVarArg) {
411             SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
412                                           Args[i].first, PtrOff,
413                                           DAG.getSrcValue(NULL));
414             MemOps.push_back(Store);
415             // Float varargs are always shadowed in available integer registers
416             if (GPR_remaining > 0) {
417               SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
418                                            DAG.getSrcValue(NULL));
419               MemOps.push_back(Load);
420               args_to_use.push_back(Load);
421               --GPR_remaining;
422             }
423             if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
424               SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
425               PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
426               SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
427                                            DAG.getSrcValue(NULL));
428               MemOps.push_back(Load);
429               args_to_use.push_back(Load);
430               --GPR_remaining;
431             }
432           } else {
433             // If we have any FPRs remaining, we may also have GPRs remaining.
434             // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
435             // GPRs.
436             if (GPR_remaining > 0) {
437               args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
438               --GPR_remaining;
439             }
440             if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
441               args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
442               --GPR_remaining;
443             }
444           }
445         } else {
446           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
447                                        Args[i].first, PtrOff,
448                                        DAG.getSrcValue(NULL)));
449         }
450         ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
451         break;
452       }
453     }
454     if (!MemOps.empty())
455       Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
456   }
457
458   std::vector<MVT::ValueType> RetVals;
459   MVT::ValueType RetTyVT = getValueType(RetTy);
460   if (RetTyVT != MVT::isVoid)
461     RetVals.push_back(RetTyVT);
462   RetVals.push_back(MVT::Other);
463
464   SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
465                                             Chain, Callee, args_to_use), 0);
466   Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
467   Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
468                       DAG.getConstant(NumBytes, getPointerTy()));
469   return std::make_pair(TheCall, Chain);
470 }
471
472 SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
473                                             Value *VAListV, SelectionDAG &DAG) {
474   // vastart just stores the address of the VarArgsFrameIndex slot into the
475   // memory location argument.
476   SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
477   return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
478                      DAG.getSrcValue(VAListV));
479 }
480
481 std::pair<SDOperand,SDOperand>
482 PPC32TargetLowering::LowerVAArg(SDOperand Chain,
483                                 SDOperand VAListP, Value *VAListV,
484                                 const Type *ArgTy, SelectionDAG &DAG) {
485   MVT::ValueType ArgVT = getValueType(ArgTy);
486
487   SDOperand VAList =
488     DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
489   SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
490   unsigned Amt;
491   if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
492     Amt = 4;
493   else {
494     assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
495            "Other types should have been promoted for varargs!");
496     Amt = 8;
497   }
498   VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
499                       DAG.getConstant(Amt, VAList.getValueType()));
500   Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
501                       VAList, VAListP, DAG.getSrcValue(VAListV));
502   return std::make_pair(Result, Chain);
503 }
504
505
506 std::pair<SDOperand, SDOperand> PPC32TargetLowering::
507 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
508                         SelectionDAG &DAG) {
509   assert(0 && "LowerFrameReturnAddress unimplemented");
510   abort();
511 }
512
513 namespace {
514 Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
515 Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
516 Statistic<>FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
517
518 //===--------------------------------------------------------------------===//
519 /// ISel - PPC32 specific code to select PPC32 machine instructions for
520 /// SelectionDAG operations.
521 //===--------------------------------------------------------------------===//
522 class ISel : public SelectionDAGISel {
523   PPC32TargetLowering PPC32Lowering;
524   SelectionDAG *ISelDAG;  // Hack to support us having a dag->dag transform
525                           // for sdiv and udiv until it is put into the future
526                           // dag combiner.
527
528   /// ExprMap - As shared expressions are codegen'd, we keep track of which
529   /// vreg the value is produced in, so we only emit one copy of each compiled
530   /// tree.
531   std::map<SDOperand, unsigned> ExprMap;
532
533   unsigned GlobalBaseReg;
534   bool GlobalBaseInitialized;
535   bool RecordSuccess;
536 public:
537   ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
538                             ISelDAG(0) {}
539
540   /// runOnFunction - Override this function in order to reset our per-function
541   /// variables.
542   virtual bool runOnFunction(Function &Fn) {
543     // Make sure we re-emit a set of the global base reg if necessary
544     GlobalBaseInitialized = false;
545     return SelectionDAGISel::runOnFunction(Fn);
546   }
547
548   /// InstructionSelectBasicBlock - This callback is invoked by
549   /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
550   virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
551     DEBUG(BB->dump());
552     // Codegen the basic block.
553     ISelDAG = &DAG;
554     Select(DAG.getRoot());
555
556     // Clear state used for selection.
557     ExprMap.clear();
558     ISelDAG = 0;
559   }
560
561   // dag -> dag expanders for integer divide by constant
562   SDOperand BuildSDIVSequence(SDOperand N);
563   SDOperand BuildUDIVSequence(SDOperand N);
564
565   unsigned getGlobalBaseReg();
566   unsigned getConstDouble(double floatVal, unsigned Result);
567   void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
568   bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
569   unsigned FoldIfWideZeroExtend(SDOperand N);
570   unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
571   unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
572   unsigned SelectExpr(SDOperand N, bool Recording=false);
573   void Select(SDOperand N);
574
575   unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset);
576   void SelectBranchCC(SDOperand N);
577   
578   virtual const char *getPassName() const {
579     return "PowerPC Pattern Instruction Selection";
580   } 
581 };
582
583 // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
584 // any number of 0s on either side.  The 1s are allowed to wrap from LSB to
585 // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.  0x0F0F0000 is
586 // not, since all 1s are not contiguous.
587 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
588   if (isShiftedMask_32(Val)) {
589     // look for the first non-zero bit
590     MB = CountLeadingZeros_32(Val);
591     // look for the first zero bit after the run of ones
592     ME = CountLeadingZeros_32((Val - 1) ^ Val);
593     return true;
594   } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
595     // effectively look for the first zero bit
596     ME = CountLeadingZeros_32(Val) - 1;
597     // effectively look for the first one bit after the run of zeros
598     MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
599     return true;
600   }
601   // no run present
602   return false;
603 }
604
605 // isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
606 // and mask opcode and mask operation.
607 static bool isRotateAndMask(unsigned Opcode, unsigned Shift, unsigned Mask,
608                             bool IsShiftMask,
609                             unsigned &SH, unsigned &MB, unsigned &ME) {
610   if (Shift > 31) return false;
611   unsigned Indeterminant = ~0;       // bit mask marking indeterminant results
612   
613   if (Opcode == ISD::SHL) { // shift left
614     // apply shift to mask if it comes first
615     if (IsShiftMask) Mask = Mask << Shift;
616     // determine which bits are made indeterminant by shift
617     Indeterminant = ~(0xFFFFFFFFu << Shift);
618   } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { // shift rights
619     // apply shift to mask if it comes first
620     if (IsShiftMask) Mask = Mask >> Shift;
621     // determine which bits are made indeterminant by shift
622     Indeterminant = ~(0xFFFFFFFFu >> Shift);
623     // adjust for the left rotate
624     Shift = 32 - Shift;
625   }
626   
627   // if the mask doesn't intersect any Indeterminant bits
628   if (!(Mask & Indeterminant)) {
629     SH = Shift;
630     // make sure the mask is still a mask (wrap arounds may not be)
631     return isRunOfOnes(Mask, MB, ME);
632   }
633   
634   // can't do it
635   return false;
636 }
637
638 // isIntImmediate - This method tests to see if a constant operand.
639 // If so Imm will receive the 32 bit value.
640 static bool isIntImmediate(SDOperand N, unsigned& Imm) {
641   // test for constant
642   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
643     // retrieve value
644     Imm = (unsigned)CN->getSignExtended();
645     // passes muster
646     return true;
647   }
648   // not a constant
649   return false;
650 }
651
652 // isOprShiftImm - Returns true if the specified operand is a shift opcode with
653 // a immediate shift count less than 32.
654 static bool isOprShiftImm(SDOperand N, unsigned& Opc, unsigned& SH) {
655   Opc = N.getOpcode();
656   return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
657          isIntImmediate(N.getOperand(1), SH) && SH < 32;
658 }
659
660 // isOprNot - Returns true if the specified operand is an xor with immediate -1.
661 static bool isOprNot(SDOperand N) {
662   unsigned Imm;
663   return N.getOpcode() == ISD::XOR &&
664          isIntImmediate(N.getOperand(1), Imm) && (signed)Imm == -1;
665 }
666
667 // Immediate constant composers.
668 // Lo16 - grabs the lo 16 bits from a 32 bit constant.
669 // Hi16 - grabs the hi 16 bits from a 32 bit constant.
670 // HA16 - computes the hi bits required if the lo bits are add/subtracted in
671 // arithmethically.
672 static unsigned Lo16(unsigned x)  { return x & 0x0000FFFF; }
673 static unsigned Hi16(unsigned x)  { return Lo16(x >> 16); }
674 static unsigned HA16(unsigned x)  { return Hi16((signed)x - (signed short)x); }
675
676 /// NodeHasRecordingVariant - If SelectExpr can always produce code for
677 /// NodeOpcode that also sets CR0 as a side effect, return true.  Otherwise,
678 /// return false.
679 static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
680   switch(NodeOpcode) {
681   default: return false;
682   case ISD::AND:
683   case ISD::OR:
684     return true;
685   }
686 }
687
688 /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
689 /// to Condition.  If the Condition is unordered or unsigned, the bool argument
690 /// U is set to true, otherwise it is set to false.
691 static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
692   U = false;
693   switch (Condition) {
694   default: assert(0 && "Unknown condition!"); abort();
695   case ISD::SETEQ:  return PPC::BEQ;
696   case ISD::SETNE:  return PPC::BNE;
697   case ISD::SETULT: U = true;
698   case ISD::SETLT:  return PPC::BLT;
699   case ISD::SETULE: U = true;
700   case ISD::SETLE:  return PPC::BLE;
701   case ISD::SETUGT: U = true;
702   case ISD::SETGT:  return PPC::BGT;
703   case ISD::SETUGE: U = true;
704   case ISD::SETGE:  return PPC::BGE;
705   }
706   return 0;
707 }
708
709 /// getCROpForOp - Return the condition register opcode (or inverted opcode)
710 /// associated with the SelectionDAG opcode.
711 static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
712   switch (Opcode) {
713   default: assert(0 && "Unknown opcode!"); abort();
714   case ISD::AND:
715     if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
716     if (!Inv1 && !Inv2) return PPC::CRAND;
717     if (Inv1 ^ Inv2) return PPC::CRANDC;
718   case ISD::OR:
719     if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
720     if (!Inv1 && !Inv2) return PPC::CROR;
721     if (Inv1 ^ Inv2) return PPC::CRORC;
722   }
723   return 0;
724 }
725
726 /// getCRIdxForSetCC - Return the index of the condition register field
727 /// associated with the SetCC condition, and whether or not the field is
728 /// treated as inverted.  That is, lt = 0; ge = 0 inverted.
729 static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
730   switch (Condition) {
731   default: assert(0 && "Unknown condition!"); abort();
732   case ISD::SETULT:
733   case ISD::SETLT:  Inv = false;  return 0;
734   case ISD::SETUGE:
735   case ISD::SETGE:  Inv = true;   return 0;
736   case ISD::SETUGT:
737   case ISD::SETGT:  Inv = false;  return 1;
738   case ISD::SETULE:
739   case ISD::SETLE:  Inv = true;   return 1;
740   case ISD::SETEQ:  Inv = false;  return 2;
741   case ISD::SETNE:  Inv = true;   return 2;
742   }
743   return 0;
744 }
745
746 /// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
747 /// and store immediate instructions.
748 static unsigned IndexedOpForOp(unsigned Opcode) {
749   switch(Opcode) {
750   default: assert(0 && "Unknown opcode!"); abort();
751   case PPC::LBZ: return PPC::LBZX;  case PPC::STB: return PPC::STBX;
752   case PPC::LHZ: return PPC::LHZX;  case PPC::STH: return PPC::STHX;
753   case PPC::LHA: return PPC::LHAX;  case PPC::STW: return PPC::STWX;
754   case PPC::LWZ: return PPC::LWZX;  case PPC::STFS: return PPC::STFSX;
755   case PPC::LFS: return PPC::LFSX;  case PPC::STFD: return PPC::STFDX;
756   case PPC::LFD: return PPC::LFDX;
757   }
758   return 0;
759 }
760
761 // Structure used to return the necessary information to codegen an SDIV as
762 // a multiply.
763 struct ms {
764   int m; // magic number
765   int s; // shift amount
766 };
767
768 struct mu {
769   unsigned int m; // magic number
770   int a;          // add indicator
771   int s;          // shift amount
772 };
773
774 /// magic - calculate the magic numbers required to codegen an integer sdiv as
775 /// a sequence of multiply and shifts.  Requires that the divisor not be 0, 1,
776 /// or -1.
777 static struct ms magic(int d) {
778   int p;
779   unsigned int ad, anc, delta, q1, r1, q2, r2, t;
780   const unsigned int two31 = 0x80000000U;
781   struct ms mag;
782
783   ad = abs(d);
784   t = two31 + ((unsigned int)d >> 31);
785   anc = t - 1 - t%ad;   // absolute value of nc
786   p = 31;               // initialize p
787   q1 = two31/anc;       // initialize q1 = 2p/abs(nc)
788   r1 = two31 - q1*anc;  // initialize r1 = rem(2p,abs(nc))
789   q2 = two31/ad;        // initialize q2 = 2p/abs(d)
790   r2 = two31 - q2*ad;   // initialize r2 = rem(2p,abs(d))
791   do {
792     p = p + 1;
793     q1 = 2*q1;        // update q1 = 2p/abs(nc)
794     r1 = 2*r1;        // update r1 = rem(2p/abs(nc))
795     if (r1 >= anc) {  // must be unsigned comparison
796       q1 = q1 + 1;
797       r1 = r1 - anc;
798     }
799     q2 = 2*q2;        // update q2 = 2p/abs(d)
800     r2 = 2*r2;        // update r2 = rem(2p/abs(d))
801     if (r2 >= ad) {   // must be unsigned comparison
802       q2 = q2 + 1;
803       r2 = r2 - ad;
804     }
805     delta = ad - r2;
806   } while (q1 < delta || (q1 == delta && r1 == 0));
807
808   mag.m = q2 + 1;
809   if (d < 0) mag.m = -mag.m; // resulting magic number
810   mag.s = p - 32;            // resulting shift
811   return mag;
812 }
813
814 /// magicu - calculate the magic numbers required to codegen an integer udiv as
815 /// a sequence of multiply, add and shifts.  Requires that the divisor not be 0.
816 static struct mu magicu(unsigned d)
817 {
818   int p;
819   unsigned int nc, delta, q1, r1, q2, r2;
820   struct mu magu;
821   magu.a = 0;               // initialize "add" indicator
822   nc = - 1 - (-d)%d;
823   p = 31;                   // initialize p
824   q1 = 0x80000000/nc;       // initialize q1 = 2p/nc
825   r1 = 0x80000000 - q1*nc;  // initialize r1 = rem(2p,nc)
826   q2 = 0x7FFFFFFF/d;        // initialize q2 = (2p-1)/d
827   r2 = 0x7FFFFFFF - q2*d;   // initialize r2 = rem((2p-1),d)
828   do {
829     p = p + 1;
830     if (r1 >= nc - r1 ) {
831       q1 = 2*q1 + 1;  // update q1
832       r1 = 2*r1 - nc; // update r1
833     }
834     else {
835       q1 = 2*q1; // update q1
836       r1 = 2*r1; // update r1
837     }
838     if (r2 + 1 >= d - r2) {
839       if (q2 >= 0x7FFFFFFF) magu.a = 1;
840       q2 = 2*q2 + 1;     // update q2
841       r2 = 2*r2 + 1 - d; // update r2
842     }
843     else {
844       if (q2 >= 0x80000000) magu.a = 1;
845       q2 = 2*q2;     // update q2
846       r2 = 2*r2 + 1; // update r2
847     }
848     delta = d - 1 - r2;
849   } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
850   magu.m = q2 + 1; // resulting magic number
851   magu.s = p - 32;  // resulting shift
852   return magu;
853 }
854 }
855
856 /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
857 /// return a DAG expression to select that will generate the same value by
858 /// multiplying by a magic number.  See:
859 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
860 SDOperand ISel::BuildSDIVSequence(SDOperand N) {
861   int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
862   ms magics = magic(d);
863   // Multiply the numerator (operand 0) by the magic value
864   SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
865                                  ISelDAG->getConstant(magics.m, MVT::i32));
866   // If d > 0 and m < 0, add the numerator
867   if (d > 0 && magics.m < 0)
868     Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
869   // If d < 0 and m > 0, subtract the numerator.
870   if (d < 0 && magics.m > 0)
871     Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
872   // Shift right algebraic if shift value is nonzero
873   if (magics.s > 0)
874     Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
875                          ISelDAG->getConstant(magics.s, MVT::i32));
876   // Extract the sign bit and add it to the quotient
877   SDOperand T =
878     ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
879   return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
880 }
881
882 /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
883 /// return a DAG expression to select that will generate the same value by
884 /// multiplying by a magic number.  See:
885 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
886 SDOperand ISel::BuildUDIVSequence(SDOperand N) {
887   unsigned d =
888     (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
889   mu magics = magicu(d);
890   // Multiply the numerator (operand 0) by the magic value
891   SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
892                                  ISelDAG->getConstant(magics.m, MVT::i32));
893   if (magics.a == 0) {
894     Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
895                          ISelDAG->getConstant(magics.s, MVT::i32));
896   } else {
897     SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
898     NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
899                            ISelDAG->getConstant(1, MVT::i32));
900     NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
901     Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
902                            ISelDAG->getConstant(magics.s-1, MVT::i32));
903   }
904   return Q;
905 }
906
907 /// getGlobalBaseReg - Output the instructions required to put the
908 /// base address to use for accessing globals into a register.
909 ///
910 unsigned ISel::getGlobalBaseReg() {
911   if (!GlobalBaseInitialized) {
912     // Insert the set of GlobalBaseReg into the first MBB of the function
913     MachineBasicBlock &FirstMBB = BB->getParent()->front();
914     MachineBasicBlock::iterator MBBI = FirstMBB.begin();
915     GlobalBaseReg = MakeReg(MVT::i32);
916     BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
917     BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
918     GlobalBaseInitialized = true;
919   }
920   return GlobalBaseReg;
921 }
922
923 /// getConstDouble - Loads a floating point value into a register, via the
924 /// Constant Pool.  Optionally takes a register in which to load the value.
925 unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
926   unsigned Tmp1 = MakeReg(MVT::i32);
927   if (0 == Result) Result = MakeReg(MVT::f64);
928   MachineConstantPool *CP = BB->getParent()->getConstantPool();
929   ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
930   unsigned CPI = CP->getConstantPoolIndex(CFP);
931   if (PICEnabled)
932     BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
933       .addConstantPoolIndex(CPI);
934   else
935     BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
936   BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
937   return Result;
938 }
939
940 /// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result.  If
941 /// Inv is true, then invert the result.
942 void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
943   unsigned IntCR = MakeReg(MVT::i32);
944   BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
945   bool GPOpt =
946     TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor();
947   BuildMI(BB, GPOpt ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
948   if (Inv) {
949     unsigned Tmp1 = MakeReg(MVT::i32);
950     BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
951       .addImm(31).addImm(31);
952     BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
953   } else {
954     BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
955       .addImm(31).addImm(31);
956   }
957 }
958
959 /// SelectBitfieldInsert - turn an or of two masked values into
960 /// the rotate left word immediate then mask insert (rlwimi) instruction.
961 /// Returns true on success, false if the caller still needs to select OR.
962 ///
963 /// Patterns matched:
964 /// 1. or shl, and   5. or and, and
965 /// 2. or and, shl   6. or shl, shr
966 /// 3. or shr, and   7. or shr, shl
967 /// 4. or and, shr
968 bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
969   bool IsRotate = false;
970   unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
971
972   SDOperand Op0 = OR.getOperand(0);
973   SDOperand Op1 = OR.getOperand(1);
974
975   unsigned Op0Opc = Op0.getOpcode();
976   unsigned Op1Opc = Op1.getOpcode();
977
978   // Verify that we have the correct opcodes
979   if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
980     return false;
981   if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
982     return false;
983
984   // Generate Mask value for Target
985   if (ConstantSDNode *CN =
986       dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) {
987     switch(Op0Opc) {
988     case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
989     case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
990     case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
991     }
992   } else {
993     return false;
994   }
995
996   // Generate Mask value for Insert
997   if (ConstantSDNode *CN =
998       dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) {
999     switch(Op1Opc) {
1000     case ISD::SHL:
1001       Amount = CN->getValue();
1002       InsMask <<= Amount;
1003       if (Op0Opc == ISD::SRL) IsRotate = true;
1004       break;
1005     case ISD::SRL:
1006       Amount = CN->getValue();
1007       InsMask >>= Amount;
1008       Amount = 32-Amount;
1009       if (Op0Opc == ISD::SHL) IsRotate = true;
1010       break;
1011     case ISD::AND:
1012       InsMask &= (unsigned)CN->getValue();
1013       break;
1014     }
1015   } else {
1016     return false;
1017   }
1018
1019   unsigned Tmp3 = 0;
1020
1021   // If both of the inputs are ANDs and one of them has a logical shift by
1022   // constant as its input, make that the inserted value so that we can combine
1023   // the shift into the rotate part of the rlwimi instruction
1024   if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
1025     if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
1026         Op1.getOperand(0).getOpcode() == ISD::SRL) {
1027       if (ConstantSDNode *CN =
1028           dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) {
1029         Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1030           CN->getValue() : 32 - CN->getValue();
1031         Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1032       }
1033     } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
1034                Op0.getOperand(0).getOpcode() == ISD::SRL) {
1035       if (ConstantSDNode *CN =
1036           dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) {
1037         std::swap(Op0, Op1);
1038         std::swap(TgtMask, InsMask);
1039         Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1040           CN->getValue() : 32 - CN->getValue();
1041         Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1042       }
1043     }
1044   }
1045
1046   // Verify that the Target mask and Insert mask together form a full word mask
1047   // and that the Insert mask is a run of set bits (which implies both are runs
1048   // of set bits).  Given that, Select the arguments and generate the rlwimi
1049   // instruction.
1050   unsigned MB, ME;
1051   if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
1052     unsigned Tmp1, Tmp2;
1053     bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
1054     // Check for rotlwi / rotrwi here, a special case of bitfield insert
1055     // where both bitfield halves are sourced from the same value.
1056     if (IsRotate && fullMask &&
1057         OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
1058       Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1059       BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1060         .addImm(0).addImm(31);
1061       return true;
1062     }
1063     if (Op0Opc == ISD::AND && fullMask)
1064       Tmp1 = SelectExpr(Op0.getOperand(0));
1065     else
1066       Tmp1 = SelectExpr(Op0);
1067     Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
1068     BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1069       .addImm(Amount).addImm(MB).addImm(ME);
1070     return true;
1071   }
1072   return false;
1073 }
1074
1075 /// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1076 /// low six bits.  If the shift amount is an ISD::AND node with a mask that is
1077 /// wider than the implicit mask, then we can get rid of the AND and let the
1078 /// shift do the mask.
1079 unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1080   unsigned C, MB, ME;
1081   if (N.getOpcode() == ISD::AND &&
1082       isIntImmediate(N.getOperand(1), C) && isRunOfOnes(C, MB, ME) &&
1083       MB <= 26 && ME == 31)
1084     return SelectExpr(N.getOperand(0));
1085   else
1086     return SelectExpr(N);
1087 }
1088
1089 unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
1090   unsigned Result, Tmp1, Tmp2;
1091   bool AlreadySelected = false;
1092   static const unsigned CompareOpcodes[] =
1093     { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
1094
1095   // Allocate a condition register for this expression
1096   Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1097
1098   // If the first operand to the select is a SETCC node, then we can fold it
1099   // into the branch that selects which value to return.
1100   if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
1101     bool U;
1102     Opc = getBCCForSetCC(SetCC->getCondition(), U);
1103     Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
1104
1105     // Use U to determine whether the SETCC immediate range is signed or not.
1106     if (isIntImmediate(SetCC->getOperand(1), Tmp2) &&
1107         ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) {
1108       Tmp2 = Lo16(Tmp2);
1109       // For comparisons against zero, we can implicity set CR0 if a recording
1110       // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1111       // operand zero of the SetCC node is available.
1112       if (0 == Tmp2 &&
1113           NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1114           SetCC->getOperand(0).Val->hasOneUse()) {
1115         RecordSuccess = false;
1116         Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1117         if (RecordSuccess) {
1118           ++Recorded;
1119           BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1120           return Result;
1121         }
1122         AlreadySelected = true;
1123       }
1124       // If we could not implicitly set CR0, then emit a compare immediate
1125       // instead.
1126       if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
1127       if (U)
1128         BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1129       else
1130         BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1131     } else {
1132       bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1133       unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
1134       Tmp1 = SelectExpr(SetCC->getOperand(0));
1135       Tmp2 = SelectExpr(SetCC->getOperand(1));
1136       BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1137     }
1138   } else {
1139     // If this isn't a SetCC, then select the value and compare it against zero,
1140     // treating it as if it were a boolean.
1141     Opc = PPC::BNE;
1142     Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1143     Tmp1 = SelectExpr(CC);
1144     BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1145   }
1146   return Result;
1147 }
1148
1149 unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
1150                             unsigned &Idx) {
1151   bool Inv0, Inv1;
1152   unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1153
1154   // Allocate a condition register for this expression
1155   unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1156
1157   // Check for the operations we support:
1158   switch(N.getOpcode()) {
1159   default:
1160     Opc = PPC::BNE;
1161     Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1162     Tmp1 = SelectExpr(N);
1163     BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1164     break;
1165   case ISD::OR:
1166   case ISD::AND:
1167     Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1168     Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1169     CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1170     if (Inv0 && !Inv1) {
1171       std::swap(Tmp1, Tmp2);
1172       std::swap(Idx0, Idx1);
1173       Opc = Opc1;
1174     }
1175     if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1176     BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1177       .addReg(Tmp2).addImm(Idx1);
1178     Inv = false;
1179     Idx = Idx0;
1180     break;
1181   case ISD::SETCC:
1182     Tmp1 = SelectCC(N, Opc, Inv, Idx);
1183     Result = Tmp1;
1184     break;
1185   }
1186   return Result;
1187 }
1188
1189 /// Check to see if the load is a constant offset from a base register.
1190 unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
1191 {
1192   unsigned imm = 0, opcode = N.getOpcode();
1193   if (N.getOpcode() == ISD::ADD) {
1194     bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
1195     if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
1196       offset = Lo16(imm);
1197       if (isFrame) {
1198         ++FrameOff;
1199         Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
1200         return 1;
1201       } else {
1202         Reg = SelectExpr(N.getOperand(0));
1203         return 0;
1204       }
1205     } else {
1206       Reg = SelectExpr(N.getOperand(0));
1207       offset = SelectExpr(N.getOperand(1));
1208       return 2;
1209     }
1210   }
1211   // Now check if we're dealing with a global, and whether or not we should emit
1212   // an optimized load or store for statics.
1213   if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) {
1214     GlobalValue *GV = GN->getGlobal();
1215     if (!GV->hasWeakLinkage() && !GV->isExternal()) {
1216       unsigned GlobalHi = MakeReg(MVT::i32);
1217       if (PICEnabled)
1218         BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg())
1219           .addGlobalAddress(GV);
1220       else
1221         BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV);
1222       Reg = GlobalHi;
1223       offset = 0;
1224       return 3;
1225     }
1226   }
1227   Reg = SelectExpr(N);
1228   offset = 0;
1229   return 0;
1230 }
1231
1232 void ISel::SelectBranchCC(SDOperand N)
1233 {
1234   MachineBasicBlock *Dest =
1235     cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1236
1237   bool Inv;
1238   unsigned Opc, CCReg, Idx;
1239   Select(N.getOperand(0));  //chain
1240   CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
1241
1242   // Iterate to the next basic block
1243   ilist<MachineBasicBlock>::iterator It = BB;
1244   ++It;
1245
1246   // If this is a two way branch, then grab the fallthrough basic block argument
1247   // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1248   // if necessary by the branch selection pass.  Otherwise, emit a standard
1249   // conditional branch.
1250   if (N.getOpcode() == ISD::BRCONDTWOWAY) {
1251     MachineBasicBlock *Fallthrough =
1252       cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1253     if (Dest != It) {
1254       BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
1255         .addMBB(Dest).addMBB(Fallthrough);
1256       if (Fallthrough != It)
1257         BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1258     } else {
1259       if (Fallthrough != It) {
1260         Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1261         BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
1262           .addMBB(Fallthrough).addMBB(Dest);
1263       }
1264     }
1265   } else {
1266     // If the fallthrough path is off the end of the function, which would be
1267     // undefined behavior, set it to be the same as the current block because
1268     // we have nothing better to set it to, and leaving it alone will cause the
1269     // PowerPC Branch Selection pass to crash.
1270     if (It == BB->getParent()->end()) It = Dest;
1271     BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
1272       .addMBB(Dest).addMBB(It);
1273   }
1274   return;
1275 }
1276
1277 unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
1278   unsigned Result;
1279   unsigned Tmp1, Tmp2, Tmp3;
1280   unsigned Opc = 0;
1281   unsigned opcode = N.getOpcode();
1282
1283   SDNode *Node = N.Val;
1284   MVT::ValueType DestType = N.getValueType();
1285
1286   if (Node->getOpcode() == ISD::CopyFromReg &&
1287       (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
1288        cast<RegSDNode>(Node)->getReg() == PPC::R1))
1289     // Just use the specified register as our input.
1290     return cast<RegSDNode>(Node)->getReg();
1291
1292   unsigned &Reg = ExprMap[N];
1293   if (Reg) return Reg;
1294
1295   switch (N.getOpcode()) {
1296   default:
1297     Reg = Result = (N.getValueType() != MVT::Other) ?
1298                             MakeReg(N.getValueType()) : 1;
1299     break;
1300   case ISD::TAILCALL:
1301   case ISD::CALL:
1302     // If this is a call instruction, make sure to prepare ALL of the result
1303     // values as well as the chain.
1304     if (Node->getNumValues() == 1)
1305       Reg = Result = 1;  // Void call, just a chain.
1306     else {
1307       Result = MakeReg(Node->getValueType(0));
1308       ExprMap[N.getValue(0)] = Result;
1309       for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1310         ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1311       ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
1312     }
1313     break;
1314   case ISD::ADD_PARTS:
1315   case ISD::SUB_PARTS:
1316   case ISD::SHL_PARTS:
1317   case ISD::SRL_PARTS:
1318   case ISD::SRA_PARTS:
1319     Result = MakeReg(Node->getValueType(0));
1320     ExprMap[N.getValue(0)] = Result;
1321     for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1322       ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1323     break;
1324   }
1325
1326   switch (opcode) {
1327   default:
1328     Node->dump();
1329     assert(0 && "Node not handled!\n");
1330   case ISD::UNDEF:
1331     BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1332     return Result;
1333   case ISD::DYNAMIC_STACKALLOC:
1334     // Generate both result values.  FIXME: Need a better commment here?
1335     if (Result != 1)
1336       ExprMap[N.getValue(1)] = 1;
1337     else
1338       Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1339
1340     // FIXME: We are currently ignoring the requested alignment for handling
1341     // greater than the stack alignment.  This will need to be revisited at some
1342     // point.  Align = N.getOperand(2);
1343     if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1344         cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1345       std::cerr << "Cannot allocate stack object with greater alignment than"
1346                 << " the stack alignment yet!";
1347       abort();
1348     }
1349     Select(N.getOperand(0));
1350     Tmp1 = SelectExpr(N.getOperand(1));
1351     // Subtract size from stack pointer, thereby allocating some space.
1352     BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1353     // Put a pointer to the space into the result register by copying the SP
1354     BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1355     return Result;
1356
1357   case ISD::ConstantPool:
1358     Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1359     Tmp2 = MakeReg(MVT::i32);
1360     if (PICEnabled)
1361       BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
1362         .addConstantPoolIndex(Tmp1);
1363     else
1364       BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
1365     BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1366     return Result;
1367
1368   case ISD::FrameIndex:
1369     Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1370     addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
1371     return Result;
1372
1373   case ISD::GlobalAddress: {
1374     GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1375     Tmp1 = MakeReg(MVT::i32);
1376     if (PICEnabled)
1377       BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1378         .addGlobalAddress(GV);
1379     else
1380       BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
1381     if (GV->hasWeakLinkage() || GV->isExternal()) {
1382       BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1383     } else {
1384       BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1385     }
1386     return Result;
1387   }
1388
1389   case ISD::LOAD:
1390   case ISD::EXTLOAD:
1391   case ISD::ZEXTLOAD:
1392   case ISD::SEXTLOAD: {
1393     MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1394       Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
1395     bool sext = (ISD::SEXTLOAD == opcode);
1396
1397     // Make sure we generate both values.
1398     if (Result != 1)
1399       ExprMap[N.getValue(1)] = 1;   // Generate the token
1400     else
1401       Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1402
1403     SDOperand Chain   = N.getOperand(0);
1404     SDOperand Address = N.getOperand(1);
1405     Select(Chain);
1406
1407     switch (TypeBeingLoaded) {
1408     default: Node->dump(); assert(0 && "Cannot load this type!");
1409     case MVT::i1:  Opc = PPC::LBZ; break;
1410     case MVT::i8:  Opc = PPC::LBZ; break;
1411     case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1412     case MVT::i32: Opc = PPC::LWZ; break;
1413     case MVT::f32: Opc = PPC::LFS; break;
1414     case MVT::f64: Opc = PPC::LFD; break;
1415     }
1416
1417     if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1418       Tmp1 = MakeReg(MVT::i32);
1419       int CPI = CP->getIndex();
1420       if (PICEnabled)
1421         BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1422           .addConstantPoolIndex(CPI);
1423       else
1424         BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
1425       BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
1426     } else if (Address.getOpcode() == ISD::FrameIndex) {
1427       Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1428       addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
1429     } else {
1430       int offset;
1431       switch(SelectAddr(Address, Tmp1, offset)) {
1432       default: assert(0 && "Unhandled return value from SelectAddr");
1433       case 0:   // imm offset, no frame, no index
1434         BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1435         break;
1436       case 1:   // imm offset + frame index
1437         addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
1438         break;
1439       case 2:   // base+index addressing
1440         Opc = IndexedOpForOp(Opc);
1441         BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1442         break;
1443       case 3: {
1444         GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
1445         GlobalValue *GV = GN->getGlobal();
1446         BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1447       }
1448       }
1449     }
1450     return Result;
1451   }
1452
1453   case ISD::TAILCALL:
1454   case ISD::CALL: {
1455     unsigned GPR_idx = 0, FPR_idx = 0;
1456     static const unsigned GPR[] = {
1457       PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1458       PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1459     };
1460     static const unsigned FPR[] = {
1461       PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1462       PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1463     };
1464
1465     // Lower the chain for this call.
1466     Select(N.getOperand(0));
1467     ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1468
1469     MachineInstr *CallMI;
1470     // Emit the correct call instruction based on the type of symbol called.
1471     if (GlobalAddressSDNode *GASD =
1472         dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1473       CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1474                                                            true);
1475     } else if (ExternalSymbolSDNode *ESSDN =
1476                dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1477       CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1478                                                             true);
1479     } else {
1480       Tmp1 = SelectExpr(N.getOperand(1));
1481       BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1482       BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1483       CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1484         .addReg(PPC::R12);
1485     }
1486
1487     // Load the register args to virtual regs
1488     std::vector<unsigned> ArgVR;
1489     for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1490       ArgVR.push_back(SelectExpr(N.getOperand(i)));
1491
1492     // Copy the virtual registers into the appropriate argument register
1493     for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1494       switch(N.getOperand(i+2).getValueType()) {
1495       default: Node->dump(); assert(0 && "Unknown value type for call");
1496       case MVT::i1:
1497       case MVT::i8:
1498       case MVT::i16:
1499       case MVT::i32:
1500         assert(GPR_idx < 8 && "Too many int args");
1501         if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1502           BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1503           CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1504         }
1505         ++GPR_idx;
1506         break;
1507       case MVT::f64:
1508       case MVT::f32:
1509         assert(FPR_idx < 13 && "Too many fp args");
1510         BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1511         CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1512         ++FPR_idx;
1513         break;
1514       }
1515     }
1516
1517     // Put the call instruction in the correct place in the MachineBasicBlock
1518     BB->push_back(CallMI);
1519
1520     switch (Node->getValueType(0)) {
1521     default: assert(0 && "Unknown value type for call result!");
1522     case MVT::Other: return 1;
1523     case MVT::i1:
1524     case MVT::i8:
1525     case MVT::i16:
1526     case MVT::i32:
1527       if (Node->getValueType(1) == MVT::i32) {
1528         BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1529         BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1530       } else {
1531         BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1532       }
1533       break;
1534     case MVT::f32:
1535     case MVT::f64:
1536       BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1537       break;
1538     }
1539     return Result+N.ResNo;
1540   }
1541
1542   case ISD::SIGN_EXTEND:
1543   case ISD::SIGN_EXTEND_INREG:
1544     Tmp1 = SelectExpr(N.getOperand(0));
1545     switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
1546     default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1547     case MVT::i16:
1548       BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1549       break;
1550     case MVT::i8:
1551       BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1552       break;
1553     case MVT::i1:
1554       BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1555       break;
1556     }
1557     return Result;
1558
1559   case ISD::CopyFromReg:
1560     DestType = N.getValue(0).getValueType();
1561     if (Result == 1)
1562       Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
1563     Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1564     if (MVT::isInteger(DestType))
1565       BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1566     else
1567       BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1568     return Result;
1569
1570   case ISD::SHL:
1571     Tmp1 = SelectExpr(N.getOperand(0));
1572     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1573       Tmp2 = CN->getValue() & 0x1F;
1574       BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
1575         .addImm(31-Tmp2);
1576     } else {
1577       Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
1578       BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1579     }
1580     return Result;
1581
1582   case ISD::SRL:
1583     Tmp1 = SelectExpr(N.getOperand(0));
1584     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1585       Tmp2 = CN->getValue() & 0x1F;
1586       BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
1587         .addImm(Tmp2).addImm(31);
1588     } else {
1589       Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
1590       BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1591     }
1592     return Result;
1593
1594   case ISD::SRA:
1595     Tmp1 = SelectExpr(N.getOperand(0));
1596     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1597       Tmp2 = CN->getValue() & 0x1F;
1598       BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1599     } else {
1600       Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
1601       BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1602     }
1603     return Result;
1604
1605   case ISD::CTLZ:
1606     Tmp1 = SelectExpr(N.getOperand(0));
1607     BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1608     return Result;
1609
1610   case ISD::ADD:
1611     if (!MVT::isInteger(DestType)) {
1612       if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1613           N.getOperand(0).Val->hasOneUse()) {
1614         ++FusedFP; // Statistic
1615         Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1616         Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1617         Tmp3 = SelectExpr(N.getOperand(1));
1618         Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1619         BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1620         return Result;
1621       }
1622       if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1623           N.getOperand(1).Val->hasOneUse()) {
1624         ++FusedFP; // Statistic
1625         Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1626         Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1627         Tmp3 = SelectExpr(N.getOperand(0));
1628         Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1629         BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1630         return Result;
1631       }
1632       Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1633       Tmp1 = SelectExpr(N.getOperand(0));
1634       Tmp2 = SelectExpr(N.getOperand(1));
1635       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1636       return Result;
1637     }
1638     Tmp1 = SelectExpr(N.getOperand(0));
1639     if (isIntImmediate(N.getOperand(1), Tmp2)) {
1640       Tmp3 = HA16(Tmp2);
1641       Tmp2 = Lo16(Tmp2);
1642       if (Tmp2 && Tmp3) {
1643         unsigned Reg = MakeReg(MVT::i32);
1644         BuildMI(BB, PPC::ADDI, 2, Reg).addReg(Tmp1).addSImm(Tmp2);
1645         BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Reg).addSImm(Tmp3);
1646       } else if (Tmp2) {
1647         BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1648       } else {
1649         BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1650       }
1651       return Result;
1652     }
1653     
1654     Tmp2 = SelectExpr(N.getOperand(1));
1655     BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1656     return Result;
1657
1658   case ISD::AND:
1659     if (isIntImmediate(N.getOperand(1), Tmp2)) {
1660       if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) {
1661         unsigned SH, MB, ME;
1662         Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1663         unsigned OprOpc;
1664         if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) &&
1665             isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) {
1666           Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1667         } else {
1668           Tmp1 = SelectExpr(N.getOperand(0));
1669           isRunOfOnes(Tmp2, MB, ME);
1670           SH = 0;
1671         }
1672         BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH)
1673           .addImm(MB).addImm(ME);
1674         RecordSuccess = true;
1675         return Result;
1676       } else if (isUInt16(Tmp2)) {
1677         Tmp2 = Lo16(Tmp2);
1678         Tmp1 = SelectExpr(N.getOperand(0));
1679         BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1680         RecordSuccess = true;
1681         return Result;
1682       } else if (isUInt16(Tmp2)) {
1683         Tmp2 = Hi16(Tmp2);
1684         Tmp1 = SelectExpr(N.getOperand(0));
1685         BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1686         RecordSuccess = true;
1687        return Result;
1688       }
1689     }
1690     if (isOprNot(N.getOperand(0))) {
1691       Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1692       Tmp2 = SelectExpr(N.getOperand(1));
1693       BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1694       RecordSuccess = false;
1695       return Result;
1696     }
1697     // emit a regular and
1698     Tmp1 = SelectExpr(N.getOperand(0));
1699     Tmp2 = SelectExpr(N.getOperand(1));
1700     Opc = Recording ? PPC::ANDo : PPC::AND;
1701     BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1702     RecordSuccess = true;
1703     return Result;
1704
1705   case ISD::OR:
1706     if (SelectBitfieldInsert(N, Result))
1707       return Result;
1708       
1709     Tmp1 = SelectExpr(N.getOperand(0));
1710     if (isIntImmediate(N.getOperand(1), Tmp2)) {
1711       Tmp3 = Hi16(Tmp2);
1712       Tmp2 = Lo16(Tmp2);
1713       if (Tmp2 && Tmp3) {
1714         unsigned Reg = MakeReg(MVT::i32);
1715         BuildMI(BB, PPC::ORI, 2, Reg).addReg(Tmp1).addImm(Tmp2);
1716         BuildMI(BB, PPC::ORIS, 2, Result).addReg(Reg).addImm(Tmp3);
1717       } else if (Tmp2) {
1718         BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1719       } else {
1720         BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp3);
1721       }
1722     } else {
1723       Tmp2 = SelectExpr(N.getOperand(1));
1724       Opc = Recording ? PPC::ORo : PPC::OR;
1725       RecordSuccess = true;
1726       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1727     }
1728     return Result;
1729
1730   case ISD::XOR: {
1731     // Check for EQV: xor, (xor a, -1), b
1732     if (N.getOperand(0).getOpcode() == ISD::XOR &&
1733         isIntImmediate(N.getOperand(0).getOperand(1), Tmp2) &&
1734         (signed)Tmp2 == -1) {
1735       Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1736       Tmp2 = SelectExpr(N.getOperand(1));
1737       BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1738       return Result;
1739     }
1740     // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
1741     if (isOprNot(N)) {
1742       switch(N.getOperand(0).getOpcode()) {
1743       case ISD::OR:
1744         Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1745         Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1746         BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1747         break;
1748       case ISD::AND:
1749         Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1750         Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1751         BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1752         break;
1753       case ISD::XOR:
1754         Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1755         Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1756         BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1757         break;
1758       default:
1759         Tmp1 = SelectExpr(N.getOperand(0));
1760         BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1761         break;
1762       }
1763       return Result;
1764     }
1765     Tmp1 = SelectExpr(N.getOperand(0));
1766     if (isIntImmediate(N.getOperand(1), Tmp2)) {
1767       Tmp3 = Hi16(Tmp2);
1768       Tmp2 = Lo16(Tmp2);
1769       if (Tmp2 && Tmp3) {
1770         unsigned Reg = MakeReg(MVT::i32);
1771         BuildMI(BB, PPC::XORI, 2, Reg).addReg(Tmp1).addImm(Tmp2);
1772         BuildMI(BB, PPC::XORIS, 2, Result).addReg(Reg).addImm(Tmp3);
1773       } else if (Tmp2) {
1774         BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1775       } else {
1776         BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp3);
1777       }
1778     } else {
1779       Tmp2 = SelectExpr(N.getOperand(1));
1780       BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1781     }
1782     return Result;
1783   }
1784
1785    case ISD::SUB:
1786     if (!MVT::isInteger(DestType)) {
1787       if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1788           N.getOperand(0).Val->hasOneUse()) {
1789         ++FusedFP; // Statistic
1790         Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1791         Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1792         Tmp3 = SelectExpr(N.getOperand(1));
1793         Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1794         BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1795         return Result;
1796       }
1797       if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1798           N.getOperand(1).Val->hasOneUse()) {
1799         ++FusedFP; // Statistic
1800         Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1801         Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1802         Tmp3 = SelectExpr(N.getOperand(0));
1803         Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1804         BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1805         return Result;
1806       }
1807       Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1808       Tmp1 = SelectExpr(N.getOperand(0));
1809       Tmp2 = SelectExpr(N.getOperand(1));
1810       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1811       return Result;
1812     }
1813     if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) {
1814       Tmp2 = SelectExpr(N.getOperand(1));
1815       BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1816       return Result;
1817     } else if (isIntImmediate(N.getOperand(1), Tmp2)) {
1818       Tmp1 = SelectExpr(N.getOperand(0));
1819       Tmp2 = -Tmp2;
1820       Tmp3 = HA16(Tmp2);
1821       Tmp2 = Lo16(Tmp2);
1822       if (Tmp2 && Tmp3) {
1823         unsigned Reg = MakeReg(MVT::i32);
1824         BuildMI(BB, PPC::ADDI, 2, Reg).addReg(Tmp1).addSImm(Tmp2);
1825         BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Reg).addSImm(Tmp3);
1826       } else if (Tmp2) {
1827         BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1828       } else {
1829         BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1830       }
1831       return Result;
1832     }    
1833     Tmp1 = SelectExpr(N.getOperand(0));
1834     Tmp2 = SelectExpr(N.getOperand(1));
1835     BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1836     return Result;
1837
1838   case ISD::MUL:
1839     Tmp1 = SelectExpr(N.getOperand(0));
1840     if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) {
1841       Tmp2 = Lo16(Tmp2);
1842       BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1843     } else {
1844       Tmp2 = SelectExpr(N.getOperand(1));
1845       switch (DestType) {
1846       default: assert(0 && "Unknown type to ISD::MUL"); break;
1847       case MVT::i32: Opc = PPC::MULLW; break;
1848       case MVT::f32: Opc = PPC::FMULS; break;
1849       case MVT::f64: Opc = PPC::FMUL; break;
1850       }
1851       BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1852     }
1853     return Result;
1854
1855   case ISD::MULHS:
1856   case ISD::MULHU:
1857     Tmp1 = SelectExpr(N.getOperand(0));
1858     Tmp2 = SelectExpr(N.getOperand(1));
1859     Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1860     BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1861     return Result;
1862
1863   case ISD::SDIV:
1864     if (isIntImmediate(N.getOperand(1), Tmp3)) {
1865       if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) {
1866         Tmp3 = Log2_32(Tmp3);
1867         Tmp1 = MakeReg(MVT::i32);
1868         Tmp2 = SelectExpr(N.getOperand(0));
1869         BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1870         BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1871         return Result;
1872       } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) {
1873         Tmp3 = Log2_32(-Tmp3);
1874         Tmp2 = SelectExpr(N.getOperand(0));
1875         Tmp1 = MakeReg(MVT::i32);
1876         unsigned Tmp4 = MakeReg(MVT::i32);
1877         BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1878         BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1879         BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1880         return Result;
1881       }
1882     }
1883     // fall thru
1884   case ISD::UDIV:
1885     // If this is a divide by constant, we can emit code using some magic
1886     // constants to implement it as a multiply instead.
1887     if (isIntImmediate(N.getOperand(1), Tmp3)) {
1888       if (opcode == ISD::SDIV) {
1889         if ((signed)Tmp3 < -1 || (signed)Tmp3 > 1) {
1890           ExprMap.erase(N);
1891           return SelectExpr(BuildSDIVSequence(N));
1892         }
1893       } else {
1894         if ((signed)Tmp3 > 1) {
1895           ExprMap.erase(N);
1896           return SelectExpr(BuildUDIVSequence(N));
1897         }
1898       }
1899     }
1900     Tmp1 = SelectExpr(N.getOperand(0));
1901     Tmp2 = SelectExpr(N.getOperand(1));
1902     switch (DestType) {
1903     default: assert(0 && "Unknown type to ISD::SDIV"); break;
1904     case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1905     case MVT::f32: Opc = PPC::FDIVS; break;
1906     case MVT::f64: Opc = PPC::FDIV; break;
1907     }
1908     BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1909     return Result;
1910
1911   case ISD::ADD_PARTS:
1912   case ISD::SUB_PARTS: {
1913     assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1914            "Not an i64 add/sub!");
1915     // Emit all of the operands.
1916     std::vector<unsigned> InVals;
1917     for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1918       InVals.push_back(SelectExpr(N.getOperand(i)));
1919     if (N.getOpcode() == ISD::ADD_PARTS) {
1920       BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1921       BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
1922     } else {
1923       BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1924       BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1925     }
1926     return Result+N.ResNo;
1927   }
1928
1929   case ISD::SHL_PARTS:
1930   case ISD::SRA_PARTS:
1931   case ISD::SRL_PARTS: {
1932     assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1933            "Not an i64 shift!");
1934     unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1935     unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1936     unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
1937     Tmp1 = MakeReg(MVT::i32);
1938     Tmp2 = MakeReg(MVT::i32);
1939     Tmp3 = MakeReg(MVT::i32);
1940     unsigned Tmp4 = MakeReg(MVT::i32);
1941     unsigned Tmp5 = MakeReg(MVT::i32);
1942     unsigned Tmp6 = MakeReg(MVT::i32);
1943     BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1944     if (ISD::SHL_PARTS == opcode) {
1945       BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1946       BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1947       BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1948       BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1949       BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
1950       BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1951       BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1952     } else if (ISD::SRL_PARTS == opcode) {
1953       BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1954       BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1955       BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1956       BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1957       BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1958       BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1959       BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1960     } else {
1961       MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1962       MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1963       MachineBasicBlock *OldMBB = BB;
1964       MachineFunction *F = BB->getParent();
1965       ilist<MachineBasicBlock>::iterator It = BB; ++It;
1966       F->getBasicBlockList().insert(It, TmpMBB);
1967       F->getBasicBlockList().insert(It, PhiMBB);
1968       BB->addSuccessor(TmpMBB);
1969       BB->addSuccessor(PhiMBB);
1970       BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1971       BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1972       BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1973       BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1974       BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1975       BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1976       BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1977       // Select correct least significant half if the shift amount > 32
1978       BB = TmpMBB;
1979       unsigned Tmp7 = MakeReg(MVT::i32);
1980       BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1981       TmpMBB->addSuccessor(PhiMBB);
1982       BB = PhiMBB;
1983       BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1984         .addReg(Tmp7).addMBB(TmpMBB);
1985     }
1986     return Result+N.ResNo;
1987   }
1988
1989   case ISD::FP_TO_UINT:
1990   case ISD::FP_TO_SINT: {
1991     bool U = (ISD::FP_TO_UINT == opcode);
1992     Tmp1 = SelectExpr(N.getOperand(0));
1993     if (!U) {
1994       Tmp2 = MakeReg(MVT::f64);
1995       BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1996       int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1997       addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1998       addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1999       return Result;
2000     } else {
2001       unsigned Zero = getConstDouble(0.0);
2002       unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2003       unsigned Border = getConstDouble(1LL << 31);
2004       unsigned UseZero = MakeReg(MVT::f64);
2005       unsigned UseMaxInt = MakeReg(MVT::f64);
2006       unsigned UseChoice = MakeReg(MVT::f64);
2007       unsigned TmpReg = MakeReg(MVT::f64);
2008       unsigned TmpReg2 = MakeReg(MVT::f64);
2009       unsigned ConvReg = MakeReg(MVT::f64);
2010       unsigned IntTmp = MakeReg(MVT::i32);
2011       unsigned XorReg = MakeReg(MVT::i32);
2012       MachineFunction *F = BB->getParent();
2013       int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2014       // Update machine-CFG edges
2015       MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2016       MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2017       MachineBasicBlock *OldMBB = BB;
2018       ilist<MachineBasicBlock>::iterator It = BB; ++It;
2019       F->getBasicBlockList().insert(It, XorMBB);
2020       F->getBasicBlockList().insert(It, PhiMBB);
2021       BB->addSuccessor(XorMBB);
2022       BB->addSuccessor(PhiMBB);
2023       // Convert from floating point to unsigned 32-bit value
2024       // Use 0 if incoming value is < 0.0
2025       BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2026       // Use 2**32 - 1 if incoming value is >= 2**32
2027       BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2028       BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2029         .addReg(MaxInt);
2030       // Subtract 2**31
2031       BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2032       // Use difference if >= 2**31
2033       BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2034       BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2035         .addReg(UseChoice);
2036       // Convert to integer
2037       BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2038       addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2039       addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2040       BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2041       BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2042
2043       // XorMBB:
2044       //   add 2**31 if input was >= 2**31
2045       BB = XorMBB;
2046       BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2047       XorMBB->addSuccessor(PhiMBB);
2048
2049       // PhiMBB:
2050       //   DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2051       BB = PhiMBB;
2052       BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2053         .addReg(XorReg).addMBB(XorMBB);
2054       return Result;
2055     }
2056     assert(0 && "Should never get here");
2057     return 0;
2058   }
2059
2060   case ISD::SETCC:
2061     if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
2062       if (ConstantSDNode *CN =
2063           dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
2064         // We can codegen setcc op, imm very efficiently compared to a brcond.
2065         // Check for those cases here.
2066         // setcc op, 0
2067         if (CN->getValue() == 0) {
2068           Tmp1 = SelectExpr(SetCC->getOperand(0));
2069           switch (SetCC->getCondition()) {
2070           default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
2071           case ISD::SETEQ:
2072             Tmp2 = MakeReg(MVT::i32);
2073             BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2074             BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2075               .addImm(5).addImm(31);
2076             break;
2077           case ISD::SETNE:
2078             Tmp2 = MakeReg(MVT::i32);
2079             BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2080             BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2081             break;
2082           case ISD::SETLT:
2083             BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2084               .addImm(31).addImm(31);
2085             break;
2086           case ISD::SETGT:
2087             Tmp2 = MakeReg(MVT::i32);
2088             Tmp3 = MakeReg(MVT::i32);
2089             BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2090             BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2091             BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2092               .addImm(31).addImm(31);
2093             break;
2094           }
2095           return Result;
2096         }
2097         // setcc op, -1
2098         if (CN->isAllOnesValue()) {
2099           Tmp1 = SelectExpr(SetCC->getOperand(0));
2100           switch (SetCC->getCondition()) {
2101           default: assert(0 && "Unhandled SetCC condition"); abort();
2102           case ISD::SETEQ:
2103             Tmp2 = MakeReg(MVT::i32);
2104             Tmp3 = MakeReg(MVT::i32);
2105             BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2106             BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2107             BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
2108             break;
2109           case ISD::SETNE:
2110             Tmp2 = MakeReg(MVT::i32);
2111             Tmp3 = MakeReg(MVT::i32);
2112             BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2113             BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2114             BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2115             break;
2116           case ISD::SETLT:
2117             Tmp2 = MakeReg(MVT::i32);
2118             Tmp3 = MakeReg(MVT::i32);
2119             BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2120             BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2121             BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2122               .addImm(31).addImm(31);
2123             break;
2124           case ISD::SETGT:
2125             Tmp2 = MakeReg(MVT::i32);
2126             BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2127               .addImm(31).addImm(31);
2128             BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2129             break;
2130           }
2131           return Result;
2132         }
2133       }
2134
2135       bool Inv;
2136       unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2137       MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
2138       return Result;
2139     }
2140     assert(0 && "Is this legal?");
2141     return 0;
2142
2143   case ISD::SELECT: {
2144     SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
2145     if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
2146         !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
2147         !MVT::isInteger(N.getOperand(1).getValueType()) &&
2148         !MVT::isInteger(N.getOperand(2).getValueType()) &&
2149         SetCC->getCondition() != ISD::SETEQ &&
2150         SetCC->getCondition() != ISD::SETNE) {
2151       MVT::ValueType VT = SetCC->getOperand(0).getValueType();
2152       unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
2153       unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
2154
2155       ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
2156       if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
2157         switch(SetCC->getCondition()) {
2158         default: assert(0 && "Invalid FSEL condition"); abort();
2159         case ISD::SETULT:
2160         case ISD::SETLT:
2161           std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
2162         case ISD::SETUGE:
2163         case ISD::SETGE:
2164           Tmp1 = SelectExpr(SetCC->getOperand(0));   // Val to compare against
2165           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
2166           return Result;
2167         case ISD::SETUGT:
2168         case ISD::SETGT:
2169           std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
2170         case ISD::SETULE:
2171         case ISD::SETLE: {
2172           if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
2173             Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
2174           } else {
2175             Tmp2 = MakeReg(VT);
2176             Tmp1 = SelectExpr(SetCC->getOperand(0));   // Val to compare against
2177             BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
2178           }
2179           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
2180           return Result;
2181         }
2182         }
2183       } else {
2184         Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
2185         Tmp1 = SelectExpr(SetCC->getOperand(0));   // Val to compare against
2186         Tmp2 = SelectExpr(SetCC->getOperand(1));
2187         Tmp3 =  MakeReg(VT);
2188         switch(SetCC->getCondition()) {
2189         default: assert(0 && "Invalid FSEL condition"); abort();
2190         case ISD::SETULT:
2191         case ISD::SETLT:
2192           BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2193           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2194           return Result;
2195         case ISD::SETUGE:
2196         case ISD::SETGE:
2197           BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2198           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2199           return Result;
2200         case ISD::SETUGT:
2201         case ISD::SETGT:
2202           BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2203           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2204           return Result;
2205         case ISD::SETULE:
2206         case ISD::SETLE:
2207           BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2208           BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2209           return Result;
2210         }
2211       }
2212       assert(0 && "Should never get here");
2213       return 0;
2214     }
2215
2216     bool Inv;
2217     unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2218     unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
2219     unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
2220
2221     // Create an iterator with which to insert the MBB for copying the false
2222     // value and the MBB to hold the PHI instruction for this SetCC.
2223     MachineBasicBlock *thisMBB = BB;
2224     const BasicBlock *LLVM_BB = BB->getBasicBlock();
2225     ilist<MachineBasicBlock>::iterator It = BB;
2226     ++It;
2227
2228     //  thisMBB:
2229     //  ...
2230     //   TrueVal = ...
2231     //   cmpTY ccX, r1, r2
2232     //   bCC copy1MBB
2233     //   fallthrough --> copy0MBB
2234     MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2235     MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
2236     BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
2237     MachineFunction *F = BB->getParent();
2238     F->getBasicBlockList().insert(It, copy0MBB);
2239     F->getBasicBlockList().insert(It, sinkMBB);
2240     // Update machine-CFG edges
2241     BB->addSuccessor(copy0MBB);
2242     BB->addSuccessor(sinkMBB);
2243
2244     //  copy0MBB:
2245     //   %FalseValue = ...
2246     //   # fallthrough to sinkMBB
2247     BB = copy0MBB;
2248     // Update machine-CFG edges
2249     BB->addSuccessor(sinkMBB);
2250
2251     //  sinkMBB:
2252     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2253     //  ...
2254     BB = sinkMBB;
2255     BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2256       .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2257     return Result;
2258   }
2259
2260   case ISD::Constant:
2261     switch (N.getValueType()) {
2262     default: assert(0 && "Cannot use constants of this type!");
2263     case MVT::i1:
2264       BuildMI(BB, PPC::LI, 1, Result)
2265         .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2266       break;
2267     case MVT::i32:
2268       {
2269         int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2270         if (v < 32768 && v >= -32768) {
2271           BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2272         } else {
2273           Tmp1 = MakeReg(MVT::i32);
2274           BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2275           BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
2276         }
2277       }
2278     }
2279     return Result;
2280
2281   case ISD::ConstantFP: {
2282     ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
2283     Result = getConstDouble(CN->getValue(), Result);
2284     return Result;
2285   }
2286
2287   case ISD::FNEG:
2288     if (!NoExcessFPPrecision &&
2289         ISD::ADD == N.getOperand(0).getOpcode() &&
2290         N.getOperand(0).Val->hasOneUse() &&
2291         ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
2292         N.getOperand(0).getOperand(0).Val->hasOneUse()) {
2293       ++FusedFP; // Statistic
2294       Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
2295       Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
2296       Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
2297       Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2298       BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2299     } else if (!NoExcessFPPrecision &&
2300         ISD::ADD == N.getOperand(0).getOpcode() &&
2301         N.getOperand(0).Val->hasOneUse() &&
2302         ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
2303         N.getOperand(0).getOperand(1).Val->hasOneUse()) {
2304       ++FusedFP; // Statistic
2305       Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
2306       Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
2307       Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
2308       Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2309       BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2310     } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
2311       Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2312       BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
2313     } else {
2314       Tmp1 = SelectExpr(N.getOperand(0));
2315       BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
2316     }
2317     return Result;
2318
2319   case ISD::FABS:
2320     Tmp1 = SelectExpr(N.getOperand(0));
2321     BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
2322     return Result;
2323
2324   case ISD::FSQRT:
2325     Tmp1 = SelectExpr(N.getOperand(0));
2326     Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
2327     BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2328     return Result;
2329
2330   case ISD::FP_ROUND:
2331     assert (DestType == MVT::f32 &&
2332             N.getOperand(0).getValueType() == MVT::f64 &&
2333             "only f64 to f32 conversion supported here");
2334     Tmp1 = SelectExpr(N.getOperand(0));
2335     BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
2336     return Result;
2337
2338   case ISD::FP_EXTEND:
2339     assert (DestType == MVT::f64 &&
2340             N.getOperand(0).getValueType() == MVT::f32 &&
2341             "only f32 to f64 conversion supported here");
2342     Tmp1 = SelectExpr(N.getOperand(0));
2343     BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
2344     return Result;
2345
2346   case ISD::UINT_TO_FP:
2347   case ISD::SINT_TO_FP: {
2348     assert (N.getOperand(0).getValueType() == MVT::i32
2349             && "int to float must operate on i32");
2350     bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
2351     Tmp1 = SelectExpr(N.getOperand(0));  // Get the operand register
2352     Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
2353     Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
2354
2355     int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2356     MachineConstantPool *CP = BB->getParent()->getConstantPool();
2357
2358     if (IsUnsigned) {
2359       unsigned ConstF = getConstDouble(0x1.000000p52);
2360       // Store the hi & low halves of the fp value, currently in int regs
2361       BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2362       addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2363       addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
2364       addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2365       // Generate the return value with a subtract
2366       BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2367     } else {
2368       unsigned ConstF = getConstDouble(0x1.000008p52);
2369       unsigned TmpL = MakeReg(MVT::i32);
2370       // Store the hi & low halves of the fp value, currently in int regs
2371       BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2372       addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2373       BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
2374       addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
2375       addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2376       // Generate the return value with a subtract
2377       BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2378     }
2379     return Result;
2380   }
2381   }
2382   return 0;
2383 }
2384
2385 void ISel::Select(SDOperand N) {
2386   unsigned Tmp1, Tmp2, Tmp3, Opc;
2387   unsigned opcode = N.getOpcode();
2388
2389   if (!ExprMap.insert(std::make_pair(N, 1)).second)
2390     return;  // Already selected.
2391
2392   SDNode *Node = N.Val;
2393
2394   switch (Node->getOpcode()) {
2395   default:
2396     Node->dump(); std::cerr << "\n";
2397     assert(0 && "Node not handled yet!");
2398   case ISD::EntryToken: return;  // Noop
2399   case ISD::TokenFactor:
2400     for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2401       Select(Node->getOperand(i));
2402     return;
2403   case ISD::CALLSEQ_START:
2404   case ISD::CALLSEQ_END:
2405     Select(N.getOperand(0));
2406     Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2407     Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
2408       PPC::ADJCALLSTACKUP;
2409     BuildMI(BB, Opc, 1).addImm(Tmp1);
2410     return;
2411   case ISD::BR: {
2412     MachineBasicBlock *Dest =
2413       cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2414     Select(N.getOperand(0));
2415     BuildMI(BB, PPC::B, 1).addMBB(Dest);
2416     return;
2417   }
2418   case ISD::BRCOND:
2419   case ISD::BRCONDTWOWAY:
2420     SelectBranchCC(N);
2421     return;
2422   case ISD::CopyToReg:
2423     Select(N.getOperand(0));
2424     Tmp1 = SelectExpr(N.getOperand(1));
2425     Tmp2 = cast<RegSDNode>(N)->getReg();
2426
2427     if (Tmp1 != Tmp2) {
2428       if (N.getOperand(1).getValueType() == MVT::f64 ||
2429           N.getOperand(1).getValueType() == MVT::f32)
2430         BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2431       else
2432         BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2433     }
2434     return;
2435   case ISD::ImplicitDef:
2436     Select(N.getOperand(0));
2437     BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2438     return;
2439   case ISD::RET:
2440     switch (N.getNumOperands()) {
2441     default:
2442       assert(0 && "Unknown return instruction!");
2443     case 3:
2444       assert(N.getOperand(1).getValueType() == MVT::i32 &&
2445              N.getOperand(2).getValueType() == MVT::i32 &&
2446              "Unknown two-register value!");
2447       Select(N.getOperand(0));
2448       Tmp1 = SelectExpr(N.getOperand(1));
2449       Tmp2 = SelectExpr(N.getOperand(2));
2450       BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2451       BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
2452       break;
2453     case 2:
2454       Select(N.getOperand(0));
2455       Tmp1 = SelectExpr(N.getOperand(1));
2456       switch (N.getOperand(1).getValueType()) {
2457         default:
2458           assert(0 && "Unknown return type!");
2459         case MVT::f64:
2460         case MVT::f32:
2461           BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2462           break;
2463         case MVT::i32:
2464           BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2465           break;
2466       }
2467     case 1:
2468       Select(N.getOperand(0));
2469       break;
2470     }
2471     BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2472     return;
2473   case ISD::TRUNCSTORE:
2474   case ISD::STORE: {
2475     SDOperand Chain   = N.getOperand(0);
2476     SDOperand Value   = N.getOperand(1);
2477     SDOperand Address = N.getOperand(2);
2478     Select(Chain);
2479
2480     Tmp1 = SelectExpr(Value); //value
2481
2482     if (opcode == ISD::STORE) {
2483       switch(Value.getValueType()) {
2484       default: assert(0 && "unknown Type in store");
2485       case MVT::i32: Opc = PPC::STW; break;
2486       case MVT::f64: Opc = PPC::STFD; break;
2487       case MVT::f32: Opc = PPC::STFS; break;
2488       }
2489     } else { //ISD::TRUNCSTORE
2490       switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
2491       default: assert(0 && "unknown Type in store");
2492       case MVT::i1:
2493       case MVT::i8: Opc  = PPC::STB; break;
2494       case MVT::i16: Opc = PPC::STH; break;
2495       }
2496     }
2497
2498     if(Address.getOpcode() == ISD::FrameIndex) {
2499       Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2500       addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
2501     } else {
2502       int offset;
2503       switch(SelectAddr(Address, Tmp2, offset)) {
2504       default: assert(0 && "Unhandled return value from SelectAddr");
2505       case 0:   // imm offset, no frame, no index
2506         BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
2507         break;
2508       case 1:   // imm offset + frame index
2509         addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
2510         break;
2511       case 2:   // base+index addressing
2512         Opc = IndexedOpForOp(Opc);
2513         BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2514         break;
2515       case 3: {
2516         GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
2517         GlobalValue *GV = GN->getGlobal();
2518         BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
2519       }
2520       }
2521     }
2522     return;
2523   }
2524   case ISD::EXTLOAD:
2525   case ISD::SEXTLOAD:
2526   case ISD::ZEXTLOAD:
2527   case ISD::LOAD:
2528   case ISD::CopyFromReg:
2529   case ISD::TAILCALL:
2530   case ISD::CALL:
2531   case ISD::DYNAMIC_STACKALLOC:
2532     ExprMap.erase(N);
2533     SelectExpr(N);
2534     return;
2535   }
2536   assert(0 && "Should not be reached!");
2537 }
2538
2539
2540 /// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2541 /// into a machine code representation using pattern matching and a machine
2542 /// description file.
2543 ///
2544 FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2545   return new ISel(TM);
2546 }
2547