PPCISelLowering.cpp revision 6de08f4377302cb73ca6a378410889be423af20f
1b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis//===-- PPC32ISelLowering.cpp - PPC32 DAG Lowering Implementation ---------===//
2b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis//
3b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis//                     The LLVM Compiler Infrastructure
4b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis//
5b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis// This file was developed by Chris Lattner and is distributed under
6b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis// the University of Illinois Open Source License. See LICENSE.TXT for details.
7b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis//
8b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
9b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis//
10b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis// This file implements the PPC32ISelLowering class.
11b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis//
12b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
13b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
14b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "PPC32ISelLowering.h"
15b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "PPC32TargetMachine.h"
16b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "llvm/CodeGen/MachineFrameInfo.h"
173c385c28cf1f27b193a620d2e51f814873362cebJohn McCall#include "llvm/CodeGen/MachineFunction.h"
18833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall#include "llvm/CodeGen/MachineInstrBuilder.h"
1930a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/CodeGen/SelectionDAG.h"
20ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor#include "llvm/Constants.h"
21aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar#include "llvm/Function.h"
22b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidisusing namespace llvm;
23b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
24c21c7e9c2cded68f91be15be6847c9649242dc17Douglas GregorPPC32TargetLowering::PPC32TargetLowering(TargetMachine &TM)
25b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  : TargetLowering(TM) {
26a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall
2734a0447b8072e0da14c0980597da9d03a1495662John McCall  // Fold away setcc operations if possible.
28b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  setSetCCIsExpensive();
2951bd803fbdade51d674598ed45da3d54190a656cJohn McCall
3051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  // Set up the register classes.
3151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
3251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
3351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
3451bd803fbdade51d674598ed45da3d54190a656cJohn McCall
35b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  // PowerPC has no intrinsics for these particular operations
36b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
37b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  setOperationAction(ISD::MEMSET, MVT::Other, Expand);
38b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
39b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
40b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
4134a0447b8072e0da14c0980597da9d03a1495662John McCall  setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
4234a0447b8072e0da14c0980597da9d03a1495662John McCall  setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
43f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall
44b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  // PowerPC has no SREM/UREM instructions
451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  setOperationAction(ISD::SREM, MVT::i32, Expand);
46b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  setOperationAction(ISD::UREM, MVT::i32, Expand);
47464011827c5f9047caaba7e245556d66a65a15b6David Blaikie
48464011827c5f9047caaba7e245556d66a65a15b6David Blaikie  // We don't support sin/cos/sqrt/fmod
4939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  setOperationAction(ISD::FSIN , MVT::f64, Expand);
5039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  setOperationAction(ISD::FCOS , MVT::f64, Expand);
5165124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie  setOperationAction(ISD::SREM , MVT::f64, Expand);
5239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  setOperationAction(ISD::FSIN , MVT::f32, Expand);
5339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  setOperationAction(ISD::FCOS , MVT::f32, Expand);
5439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  setOperationAction(ISD::SREM , MVT::f32, Expand);
5539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie
5639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  // If we're enabling GP optimizations, use hardware square root
57464011827c5f9047caaba7e245556d66a65a15b6David Blaikie  if (!TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) {
58464011827c5f9047caaba7e245556d66a65a15b6David Blaikie    setOperationAction(ISD::FSQRT, MVT::f64, Expand);
59464011827c5f9047caaba7e245556d66a65a15b6David Blaikie    setOperationAction(ISD::FSQRT, MVT::f32, Expand);
6039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  }
6139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie
6265124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie  // PowerPC does not have CTPOP or CTTZ
6339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  setOperationAction(ISD::CTPOP, MVT::i32  , Expand);
6439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  setOperationAction(ISD::CTTZ , MVT::i32  , Expand);
6539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie
6639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  // PowerPC does not have Select
6739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  setOperationAction(ISD::SELECT, MVT::i32, Expand);
6839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  setOperationAction(ISD::SELECT, MVT::f32, Expand);
6939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  setOperationAction(ISD::SELECT, MVT::f64, Expand);
7051bd803fbdade51d674598ed45da3d54190a656cJohn McCall
7151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  // PowerPC wants to turn select_cc of FP into fsel when possible.
7251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
7351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
7451bd803fbdade51d674598ed45da3d54190a656cJohn McCall
7551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  // PowerPC does not have BRCOND* which requires SetCC
7651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  setOperationAction(ISD::BRCOND,       MVT::Other, Expand);
7751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
7851bd803fbdade51d674598ed45da3d54190a656cJohn McCall
7951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  // PowerPC does not have FP_TO_UINT
8051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
8134a0447b8072e0da14c0980597da9d03a1495662John McCall
8234a0447b8072e0da14c0980597da9d03a1495662John McCall  // PowerPC does not have [U|S]INT_TO_FP
8334a0447b8072e0da14c0980597da9d03a1495662John McCall  setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
84f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
8534a0447b8072e0da14c0980597da9d03a1495662John McCall
86b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  setSetCCResultContents(ZeroOrOneSetCCResult);
8751bd803fbdade51d674598ed45da3d54190a656cJohn McCall
88a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  computeRegisterProperties();
8951bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
9051bd803fbdade51d674598ed45da3d54190a656cJohn McCall
9151bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
9234a0447b8072e0da14c0980597da9d03a1495662John McCallstatic bool isFloatingPointZero(SDOperand Op) {
937247c88d1e41514a41085f83ebf03dd5220e054aDavid Blaikie  if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
94b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis    return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
95b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
96b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis    // Maybe this has already been legalized into the constant pool?
97b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis    if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
9844ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
9944ee0a710c59d8e6793189f903bae21c16814324Eli Friedman        return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
10044ee0a710c59d8e6793189f903bae21c16814324Eli Friedman  }
10144ee0a710c59d8e6793189f903bae21c16814324Eli Friedman  return false;
102b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
103b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
10451bd803fbdade51d674598ed45da3d54190a656cJohn McCall/// LowerOperation - Provide custom lowering hooks for some operations.
10551bd803fbdade51d674598ed45da3d54190a656cJohn McCall///
10651bd803fbdade51d674598ed45da3d54190a656cJohn McCallSDOperand PPC32TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
10734a0447b8072e0da14c0980597da9d03a1495662John McCall  switch (Op.getOpcode()) {
108f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  default: assert(0 && "Wasn't expecting to be able to lower this!");
10934a0447b8072e0da14c0980597da9d03a1495662John McCall  case ISD::SELECT_CC:
11034a0447b8072e0da14c0980597da9d03a1495662John McCall    // Turn FP only select_cc's into fsel instructions.
111b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis    if (MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
112b735471f3848065120d7210e557b5f0d37ed4c43Argyrios Kyrtzidis        MVT::isFloatingPoint(Op.getOperand(2).getValueType())) {
11351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
11451bd803fbdade51d674598ed45da3d54190a656cJohn McCall
11551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // Cannot handle SETEQ/SETNE.
116b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis      if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
117e4aec0eae303e926643c7239cf7b0197ea9f63e2Abramo Bagnara
118e4aec0eae303e926643c7239cf7b0197ea9f63e2Abramo Bagnara      MVT::ValueType ResVT = Op.getValueType();
119e4aec0eae303e926643c7239cf7b0197ea9f63e2Abramo Bagnara      MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
120e4aec0eae303e926643c7239cf7b0197ea9f63e2Abramo Bagnara      SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
121e4aec0eae303e926643c7239cf7b0197ea9f63e2Abramo Bagnara      SDOperand TV  = Op.getOperand(2), FV  = Op.getOperand(3);
122e4aec0eae303e926643c7239cf7b0197ea9f63e2Abramo Bagnara
123833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      // If the RHS of the comparison is a 0.0, we don't need to do the
124aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar      // subtraction at all.
125e4aec0eae303e926643c7239cf7b0197ea9f63e2Abramo Bagnara      if (isFloatingPointZero(RHS))
126833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        switch (CC) {
127aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar        default: assert(0 && "Invalid FSEL condition"); abort();
128aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar        case ISD::SETULT:
129833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        case ISD::SETLT:
130833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall          std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
131bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara        case ISD::SETUGE:
132bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara        case ISD::SETGE:
13351bd803fbdade51d674598ed45da3d54190a656cJohn McCall          return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
134b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis        case ISD::SETUGT:
135b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis        case ISD::SETGT:
13634a0447b8072e0da14c0980597da9d03a1495662John McCall          std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
13751bd803fbdade51d674598ed45da3d54190a656cJohn McCall        case ISD::SETULE:
13834a0447b8072e0da14c0980597da9d03a1495662John McCall        case ISD::SETLE:
139b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis          return DAG.getNode(PPCISD::FSEL, ResVT,
140b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis                             DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
141b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis        }
14251bd803fbdade51d674598ed45da3d54190a656cJohn McCall
14351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      switch (CC) {
14451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      default: assert(0 && "Invalid FSEL condition"); abort();
145b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis      case ISD::SETULT:
14634a0447b8072e0da14c0980597da9d03a1495662John McCall      case ISD::SETLT:
14751bd803fbdade51d674598ed45da3d54190a656cJohn McCall        return DAG.getNode(PPCISD::FSEL, ResVT,
14834a0447b8072e0da14c0980597da9d03a1495662John McCall                           DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), FV, TV);
14939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      case ISD::SETUGE:
150140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      case ISD::SETGE:
1514ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall        return DAG.getNode(PPCISD::FSEL, ResVT,
1524ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall                           DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), TV, FV);
1534ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall      case ISD::SETUGT:
1544ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall      case ISD::SETGT:
1554ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall        return DAG.getNode(PPCISD::FSEL, ResVT,
156c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor                           DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), FV, TV);
157c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      case ISD::SETULE:
1584ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall      case ISD::SETLE:
1594ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall        return DAG.getNode(PPCISD::FSEL, ResVT,
16045ab4b5f8961dadcef6545ed6956da5daf95c6cbCraig Silverstein                           DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), TV, FV);
16145ab4b5f8961dadcef6545ed6956da5daf95c6cbCraig Silverstein      }
16245ab4b5f8961dadcef6545ed6956da5daf95c6cbCraig Silverstein    }
163711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    break;
164711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
165711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return SDOperand();
166711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
167711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
168711c52bb20d0c69063b52a99826fb7d2835501f1John McCallstd::vector<SDOperand>
169711c52bb20d0c69063b52a99826fb7d2835501f1John McCallPPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
170711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  //
171711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // add beautiful description of PPC stack frame format, or at least some docs
172711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  //
173711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  MachineFunction &MF = DAG.getMachineFunction();
174711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  MachineFrameInfo *MFI = MF.getFrameInfo();
17545ab4b5f8961dadcef6545ed6956da5daf95c6cbCraig Silverstein  MachineBasicBlock& BB = MF.front();
17645ab4b5f8961dadcef6545ed6956da5daf95c6cbCraig Silverstein  std::vector<SDOperand> ArgValues;
177b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
178b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  // Due to the rather complicated nature of the PowerPC ABI, rather than a
179b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  // fixed size array of physical args, for the sake of simplicity let the STL
180b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  // handle tracking them for us.
181b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  std::vector<unsigned> argVR, argPR, argOp;
182b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  unsigned ArgOffset = 24;
183b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  unsigned GPR_remaining = 8;
184b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  unsigned FPR_remaining = 13;
1854ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall  unsigned GPR_idx = 0, FPR_idx = 0;
18665124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie  static const unsigned GPR[] = {
18765124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie    PPC::R3, PPC::R4, PPC::R5, PPC::R6,
18865124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie    PPC::R7, PPC::R8, PPC::R9, PPC::R10,
18965124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie  };
190ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  static const unsigned FPR[] = {
191ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
19251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
193723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  };
194bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara
195b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  // Add DAG nodes to load the arguments...  On entry to a function on PPC,
196b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  // the arguments start at offset 24, although they are likely to be passed
1973c385c28cf1f27b193a620d2e51f814873362cebJohn McCall  // in registers.
1983c385c28cf1f27b193a620d2e51f814873362cebJohn McCall  for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
1994ab92891a53adda8c52c1947351371da58e33f64Gabor Greif    SDOperand newroot, argt;
2003c385c28cf1f27b193a620d2e51f814873362cebJohn McCall    unsigned ObjSize;
2013c385c28cf1f27b193a620d2e51f814873362cebJohn McCall    bool needsLoad = false;
20234a0447b8072e0da14c0980597da9d03a1495662John McCall    bool ArgLive = !I->use_empty();
203489f7131d0f7746525de3b26204c8eb523d84505Gabor Greif    MVT::ValueType ObjectVT = getValueType(I->getType());
20434a0447b8072e0da14c0980597da9d03a1495662John McCall
20534a0447b8072e0da14c0980597da9d03a1495662John McCall    switch (ObjectVT) {
20634a0447b8072e0da14c0980597da9d03a1495662John McCall    default: assert(0 && "Unhandled argument type!");
207f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    case MVT::i1:
20834a0447b8072e0da14c0980597da9d03a1495662John McCall    case MVT::i8:
209f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    case MVT::i16:
210f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    case MVT::i32:
21134a0447b8072e0da14c0980597da9d03a1495662John McCall      ObjSize = 4;
21234a0447b8072e0da14c0980597da9d03a1495662John McCall      if (!ArgLive) break;
21351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      if (GPR_remaining > 0) {
21451bd803fbdade51d674598ed45da3d54190a656cJohn McCall        MF.addLiveIn(GPR[GPR_idx]);
21551bd803fbdade51d674598ed45da3d54190a656cJohn McCall        argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
21651bd803fbdade51d674598ed45da3d54190a656cJohn McCall                                            GPR[GPR_idx], MVT::i32);
21739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        if (ObjectVT != MVT::i32)
21839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
21965124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie      } else {
22065124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie        needsLoad = true;
22134a0447b8072e0da14c0980597da9d03a1495662John McCall      }
22234a0447b8072e0da14c0980597da9d03a1495662John McCall      break;
22334a0447b8072e0da14c0980597da9d03a1495662John McCall    case MVT::i64: ObjSize = 8;
22434a0447b8072e0da14c0980597da9d03a1495662John McCall      if (!ArgLive) break;
22534a0447b8072e0da14c0980597da9d03a1495662John McCall      if (GPR_remaining > 0) {
22634a0447b8072e0da14c0980597da9d03a1495662John McCall        SDOperand argHi, argLo;
22734a0447b8072e0da14c0980597da9d03a1495662John McCall        MF.addLiveIn(GPR[GPR_idx]);
22834a0447b8072e0da14c0980597da9d03a1495662John McCall        argHi = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
22951bd803fbdade51d674598ed45da3d54190a656cJohn McCall        // If we have two or more remaining argument registers, then both halves
23034a0447b8072e0da14c0980597da9d03a1495662John McCall        // of the i64 can be sourced from there.  Otherwise, the lower half will
231bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara        // have to come off the stack.  This can happen when an i64 is preceded
23234a0447b8072e0da14c0980597da9d03a1495662John McCall        // by 28 bytes of arguments.
23334a0447b8072e0da14c0980597da9d03a1495662John McCall        if (GPR_remaining > 1) {
23434a0447b8072e0da14c0980597da9d03a1495662John McCall          MF.addLiveIn(GPR[GPR_idx+1]);
23534a0447b8072e0da14c0980597da9d03a1495662John McCall          argLo = DAG.getCopyFromReg(argHi, GPR[GPR_idx+1], MVT::i32);
23644ee0a710c59d8e6793189f903bae21c16814324Eli Friedman        } else {
23744ee0a710c59d8e6793189f903bae21c16814324Eli Friedman          int FI = MFI->CreateFixedObject(4, ArgOffset+4);
23844ee0a710c59d8e6793189f903bae21c16814324Eli Friedman          SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
23944ee0a710c59d8e6793189f903bae21c16814324Eli Friedman          argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
24044ee0a710c59d8e6793189f903bae21c16814324Eli Friedman                              DAG.getSrcValue(NULL));
24134a0447b8072e0da14c0980597da9d03a1495662John McCall        }
24234a0447b8072e0da14c0980597da9d03a1495662John McCall        // Build the outgoing arg thingy
2434ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall        argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
2444ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall        newroot = argLo;
245c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor      } else {
2464ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall        needsLoad = true;
2474ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall      }
2484ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall      break;
2494ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall    case MVT::f32:
2504ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall    case MVT::f64:
2514ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall      ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
2524ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall      if (!ArgLive) break;
25334a0447b8072e0da14c0980597da9d03a1495662John McCall      if (FPR_remaining > 0) {
25434a0447b8072e0da14c0980597da9d03a1495662John McCall        MF.addLiveIn(FPR[FPR_idx]);
25534a0447b8072e0da14c0980597da9d03a1495662John McCall        argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
25634a0447b8072e0da14c0980597da9d03a1495662John McCall                                            FPR[FPR_idx], ObjectVT);
25734a0447b8072e0da14c0980597da9d03a1495662John McCall        --FPR_remaining;
25834a0447b8072e0da14c0980597da9d03a1495662John McCall        ++FPR_idx;
25934a0447b8072e0da14c0980597da9d03a1495662John McCall      } else {
26034a0447b8072e0da14c0980597da9d03a1495662John McCall        needsLoad = true;
26144ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      }
26244ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      break;
26344ee0a710c59d8e6793189f903bae21c16814324Eli Friedman    }
26444ee0a710c59d8e6793189f903bae21c16814324Eli Friedman
26544ee0a710c59d8e6793189f903bae21c16814324Eli Friedman    // We need to load the argument to a virtual register if we determined above
26634a0447b8072e0da14c0980597da9d03a1495662John McCall    // that we ran out of physical registers of the appropriate type
26734a0447b8072e0da14c0980597da9d03a1495662John McCall    if (needsLoad) {
26839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      unsigned SubregOffset = 0;
26939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
27065124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie      if (ObjectVT == MVT::i16) SubregOffset = 2;
27165124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie      int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
27234a0447b8072e0da14c0980597da9d03a1495662John McCall      SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
27334a0447b8072e0da14c0980597da9d03a1495662John McCall      FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
27434a0447b8072e0da14c0980597da9d03a1495662John McCall                        DAG.getConstant(SubregOffset, MVT::i32));
27534a0447b8072e0da14c0980597da9d03a1495662John McCall      argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
27639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie                                   DAG.getSrcValue(NULL));
27739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    }
27839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie
27934a0447b8072e0da14c0980597da9d03a1495662John McCall    // Every 4 bytes of argument space consumes one of the GPRs available for
28034a0447b8072e0da14c0980597da9d03a1495662John McCall    // argument passing.
28134a0447b8072e0da14c0980597da9d03a1495662John McCall    if (GPR_remaining > 0) {
282f2130a6e3cef10b435feadf6ad8bebaaf9a81a88John McCall      unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
283f2130a6e3cef10b435feadf6ad8bebaaf9a81a88John McCall      GPR_remaining -= delta;
28434a0447b8072e0da14c0980597da9d03a1495662John McCall      GPR_idx += delta;
28570517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko    }
28670517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko    ArgOffset += ObjSize;
28770517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko    if (newroot.Val)
288f2130a6e3cef10b435feadf6ad8bebaaf9a81a88John McCall      DAG.setRoot(newroot.getValue(1));
28970517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko
29034a0447b8072e0da14c0980597da9d03a1495662John McCall    ArgValues.push_back(argt);
29134a0447b8072e0da14c0980597da9d03a1495662John McCall  }
29234a0447b8072e0da14c0980597da9d03a1495662John McCall
29334a0447b8072e0da14c0980597da9d03a1495662John McCall  // If the function takes variable number of arguments, make a frame index for
29434a0447b8072e0da14c0980597da9d03a1495662John McCall  // the start of the first vararg value... for expansion of llvm.va_start.
29534a0447b8072e0da14c0980597da9d03a1495662John McCall  if (F.isVarArg()) {
296a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall    VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
297a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall    SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
298a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall    // If this function is vararg, store any remaining integer argument regs
299a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall    // to their spots on the stack so that they may be loaded by deferencing the
30051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    // result of va_next.
30151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    std::vector<SDOperand> MemOps;
30251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
30351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      MF.addLiveIn(GPR[GPR_idx]);
30451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
30551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
30651bd803fbdade51d674598ed45da3d54190a656cJohn McCall                                    Val, FIN, DAG.getSrcValue(NULL));
30751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      MemOps.push_back(Store);
30851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      // Increment the address by four for the next argument to store
30934a0447b8072e0da14c0980597da9d03a1495662John McCall      SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
31034a0447b8072e0da14c0980597da9d03a1495662John McCall      FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
31134a0447b8072e0da14c0980597da9d03a1495662John McCall    }
31234a0447b8072e0da14c0980597da9d03a1495662John McCall    DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
31334a0447b8072e0da14c0980597da9d03a1495662John McCall  }
31434a0447b8072e0da14c0980597da9d03a1495662John McCall
31534a0447b8072e0da14c0980597da9d03a1495662John McCall  // Finally, inform the code generator which regs we return values in.
31639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  switch (getValueType(F.getReturnType())) {
31765124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie    default: assert(0 && "Unknown type!");
31844ee0a710c59d8e6793189f903bae21c16814324Eli Friedman    case MVT::isVoid: break;
31944ee0a710c59d8e6793189f903bae21c16814324Eli Friedman    case MVT::i1:
32034a0447b8072e0da14c0980597da9d03a1495662John McCall    case MVT::i8:
32134a0447b8072e0da14c0980597da9d03a1495662John McCall    case MVT::i16:
32234a0447b8072e0da14c0980597da9d03a1495662John McCall    case MVT::i32:
32334a0447b8072e0da14c0980597da9d03a1495662John McCall      MF.addLiveOut(PPC::R3);
32434a0447b8072e0da14c0980597da9d03a1495662John McCall      break;
32534a0447b8072e0da14c0980597da9d03a1495662John McCall    case MVT::i64:
32639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      MF.addLiveOut(PPC::R3);
32744ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      MF.addLiveOut(PPC::R4);
32844ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      break;
32944ee0a710c59d8e6793189f903bae21c16814324Eli Friedman    case MVT::f32:
33020387efff0870da2c8b30bb62ae661239a903021Manuel Klimek    case MVT::f64:
33144ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      MF.addLiveOut(PPC::F1);
33244ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      break;
33344ee0a710c59d8e6793189f903bae21c16814324Eli Friedman  }
33444ee0a710c59d8e6793189f903bae21c16814324Eli Friedman
33544ee0a710c59d8e6793189f903bae21c16814324Eli Friedman  return ArgValues;
33644ee0a710c59d8e6793189f903bae21c16814324Eli Friedman}
337e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara
338e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnarastd::pair<SDOperand, SDOperand>
3394ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCallPPC32TargetLowering::LowerCallTo(SDOperand Chain,
3404ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall                                 const Type *RetTy, bool isVarArg,
3414ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall                                 unsigned CallingConv, bool isTailCall,
3424ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall                                 SDOperand Callee, ArgListTy &Args,
343f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall                                 SelectionDAG &DAG) {
34451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  // args_to_use will accumulate outgoing args for the ISD::CALL case in
34534a0447b8072e0da14c0980597da9d03a1495662John McCall  // SelectExpr to use to put the arguments in the appropriate registers.
34634a0447b8072e0da14c0980597da9d03a1495662John McCall  std::vector<SDOperand> args_to_use;
34751bd803fbdade51d674598ed45da3d54190a656cJohn McCall
34834a0447b8072e0da14c0980597da9d03a1495662John McCall  // Count how many bytes are to be pushed on the stack, including the linkage
34934a0447b8072e0da14c0980597da9d03a1495662John McCall  // area, and parameter passing area.
35034a0447b8072e0da14c0980597da9d03a1495662John McCall  unsigned NumBytes = 24;
35134a0447b8072e0da14c0980597da9d03a1495662John McCall
35244ee0a710c59d8e6793189f903bae21c16814324Eli Friedman  if (Args.empty()) {
35344ee0a710c59d8e6793189f903bae21c16814324Eli Friedman    Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
35444ee0a710c59d8e6793189f903bae21c16814324Eli Friedman                        DAG.getConstant(NumBytes, getPointerTy()));
35544ee0a710c59d8e6793189f903bae21c16814324Eli Friedman  } else {
35634a0447b8072e0da14c0980597da9d03a1495662John McCall    for (unsigned i = 0, e = Args.size(); i != e; ++i) {
35734a0447b8072e0da14c0980597da9d03a1495662John McCall      switch (getValueType(Args[i].second)) {
35834a0447b8072e0da14c0980597da9d03a1495662John McCall      default: assert(0 && "Unknown value type!");
35934a0447b8072e0da14c0980597da9d03a1495662John McCall      case MVT::i1:
36034a0447b8072e0da14c0980597da9d03a1495662John McCall      case MVT::i8:
36134a0447b8072e0da14c0980597da9d03a1495662John McCall      case MVT::i16:
36234a0447b8072e0da14c0980597da9d03a1495662John McCall      case MVT::i32:
36334a0447b8072e0da14c0980597da9d03a1495662John McCall      case MVT::f32:
36444ee0a710c59d8e6793189f903bae21c16814324Eli Friedman        NumBytes += 4;
36544ee0a710c59d8e6793189f903bae21c16814324Eli Friedman        break;
36644ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      case MVT::i64:
36744ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      case MVT::f64:
36834a0447b8072e0da14c0980597da9d03a1495662John McCall        NumBytes += 8;
369ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie        break;
37034a0447b8072e0da14c0980597da9d03a1495662John McCall      }
37144ee0a710c59d8e6793189f903bae21c16814324Eli Friedman    }
37244ee0a710c59d8e6793189f903bae21c16814324Eli Friedman
37344ee0a710c59d8e6793189f903bae21c16814324Eli Friedman    // Just to be safe, we'll always reserve the full 24 bytes of linkage area
37444ee0a710c59d8e6793189f903bae21c16814324Eli Friedman    // plus 32 bytes of argument space in case any called code gets funky on us.
37534a0447b8072e0da14c0980597da9d03a1495662John McCall    // (Required by ABI to support var arg)
37634a0447b8072e0da14c0980597da9d03a1495662John McCall    if (NumBytes < 56) NumBytes = 56;
377a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall
378a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall    // Adjust the stack pointer for the new arguments...
37934a0447b8072e0da14c0980597da9d03a1495662John McCall    // These operations are automatically eliminated by the prolog/epilog pass
38034a0447b8072e0da14c0980597da9d03a1495662John McCall    Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
38134a0447b8072e0da14c0980597da9d03a1495662John McCall                        DAG.getConstant(NumBytes, getPointerTy()));
38234a0447b8072e0da14c0980597da9d03a1495662John McCall
38334a0447b8072e0da14c0980597da9d03a1495662John McCall    // Set up a copy of the stack pointer for use loading and storing any
38434a0447b8072e0da14c0980597da9d03a1495662John McCall    // arguments that may not fit in the registers available for argument
38534a0447b8072e0da14c0980597da9d03a1495662John McCall    // passing.
386a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall    SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
387a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall                                            PPC::R1, MVT::i32);
388a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall
389a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall    // Figure out which arguments are going to go in registers, and which in
39034a0447b8072e0da14c0980597da9d03a1495662John McCall    // memory.  Also, if this is a vararg function, floating point operations
39134a0447b8072e0da14c0980597da9d03a1495662John McCall    // must be stored to our stack, and loaded into integer regs as well, if
39234a0447b8072e0da14c0980597da9d03a1495662John McCall    // any integer regs are available for argument passing.
393a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall    unsigned ArgOffset = 24;
394a6f56429aa9a02e71426e99db8b0ae94d0115215John McCall    unsigned GPR_remaining = 8;
39534a0447b8072e0da14c0980597da9d03a1495662John McCall    unsigned FPR_remaining = 13;
3964ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall
39744ee0a710c59d8e6793189f903bae21c16814324Eli Friedman    std::vector<SDOperand> MemOps;
39844ee0a710c59d8e6793189f903bae21c16814324Eli Friedman    for (unsigned i = 0, e = Args.size(); i != e; ++i) {
39944ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      // PtrOff will be used to store the current argument to the stack if a
40044ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      // register cannot be found for it.
40144ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
40244ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
40344ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      MVT::ValueType ArgVT = getValueType(Args[i].second);
40444ee0a710c59d8e6793189f903bae21c16814324Eli Friedman
40544ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      switch (ArgVT) {
40644ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      default: assert(0 && "Unexpected ValueType for argument!");
40744ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      case MVT::i1:
40844ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      case MVT::i8:
4094ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall      case MVT::i16:
4104ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall        // Promote the integer to 32 bits.  If the input type is signed use a
4114ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall        // sign extend, otherwise use a zero extend.
4124ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall        if (Args[i].second->isSigned())
4134ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall          Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
4144ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall        else
4154ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall          Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
41634a0447b8072e0da14c0980597da9d03a1495662John McCall        // FALL THROUGH
41734a0447b8072e0da14c0980597da9d03a1495662John McCall      case MVT::i32:
41851bd803fbdade51d674598ed45da3d54190a656cJohn McCall        if (GPR_remaining > 0) {
41951bd803fbdade51d674598ed45da3d54190a656cJohn McCall          args_to_use.push_back(Args[i].first);
42051bd803fbdade51d674598ed45da3d54190a656cJohn McCall          --GPR_remaining;
42151bd803fbdade51d674598ed45da3d54190a656cJohn McCall        } else {
42251bd803fbdade51d674598ed45da3d54190a656cJohn McCall          MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
42339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie                                       Args[i].first, PtrOff,
424713da40fb093cb6ab50ddc57e1697fddf9802104Douglas Gregor                                       DAG.getSrcValue(NULL)));
425713da40fb093cb6ab50ddc57e1697fddf9802104Douglas Gregor        }
426713da40fb093cb6ab50ddc57e1697fddf9802104Douglas Gregor        ArgOffset += 4;
427713da40fb093cb6ab50ddc57e1697fddf9802104Douglas Gregor        break;
42865124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie      case MVT::i64:
42944ee0a710c59d8e6793189f903bae21c16814324Eli Friedman        // If we have one free GPR left, we can place the upper half of the i64
43044ee0a710c59d8e6793189f903bae21c16814324Eli Friedman        // in it, and store the other half to the stack.  If we have two or more
431b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis        // free GPRs, then we can pass both halves of the i64 in registers.
43265124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie        if (GPR_remaining > 0) {
43365124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie          SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
434b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis                                     Args[i].first, DAG.getConstant(1, MVT::i32));
435b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis          SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
43639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie                                     Args[i].first, DAG.getConstant(0, MVT::i32));
437f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall          args_to_use.push_back(Hi);
43851bd803fbdade51d674598ed45da3d54190a656cJohn McCall          --GPR_remaining;
4394ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall          if (GPR_remaining > 0) {
44034a0447b8072e0da14c0980597da9d03a1495662John McCall            args_to_use.push_back(Lo);
441b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis            --GPR_remaining;
442ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          } else {
44351bd803fbdade51d674598ed45da3d54190a656cJohn McCall            SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
44434a0447b8072e0da14c0980597da9d03a1495662John McCall            PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
445b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis            MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
446b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis                                         Lo, PtrOff, DAG.getSrcValue(NULL)));
44751bd803fbdade51d674598ed45da3d54190a656cJohn McCall          }
44851bd803fbdade51d674598ed45da3d54190a656cJohn McCall        } else {
449ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie          MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
450ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       Args[i].first, PtrOff,
451ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       DAG.getSrcValue(NULL)));
452ed97649e9574b9d854fa4d6109c9333ae0993554John McCall        }
453b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis        ArgOffset += 8;
45444ee0a710c59d8e6793189f903bae21c16814324Eli Friedman        break;
45544ee0a710c59d8e6793189f903bae21c16814324Eli Friedman      case MVT::f32:
456ed97649e9574b9d854fa4d6109c9333ae0993554John McCall      case MVT::f64:
457b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis        if (FPR_remaining > 0) {
45851bd803fbdade51d674598ed45da3d54190a656cJohn McCall          args_to_use.push_back(Args[i].first);
459b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis          --FPR_remaining;
460b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis          if (isVarArg) {
46151bd803fbdade51d674598ed45da3d54190a656cJohn McCall            SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
462b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis                                          Args[i].first, PtrOff,
463bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara                                          DAG.getSrcValue(NULL));
464b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis            MemOps.push_back(Store);
465b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis            // Float varargs are always shadowed in available integer registers
466c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor            if (GPR_remaining > 0) {
4674ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall              SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
4684ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall                                           DAG.getSrcValue(NULL));
469ed97649e9574b9d854fa4d6109c9333ae0993554John McCall              MemOps.push_back(Load);
47039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie              args_to_use.push_back(Load);
47139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie              --GPR_remaining;
47265124fe81f61eed98b845c87e3a78a780f3deb11David Blaikie            }
47351bd803fbdade51d674598ed45da3d54190a656cJohn McCall            if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
4744ce74bd425dccd9d9ad6ccfc9ffbc01698a6e71aJohn McCall              SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
475ed97649e9574b9d854fa4d6109c9333ae0993554John McCall              PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
476ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor              SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
477ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor                                           DAG.getSrcValue(NULL));
478ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor              MemOps.push_back(Load);
479ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor              args_to_use.push_back(Load);
480ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor              --GPR_remaining;
481ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor            }
482ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          } else {
483ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor            // If we have any FPRs remaining, we may also have GPRs remaining.
484ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor            // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
485ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor            // GPRs.
486ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor            if (GPR_remaining > 0) {
487ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor              args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
488ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor              --GPR_remaining;
489ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor            }
490ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor            if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
491ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor              args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
492ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor              --GPR_remaining;
493ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor            }
494ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          }
495ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        } else {
496ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
497ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor                                       Args[i].first, PtrOff,
498ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor                                       DAG.getSrcValue(NULL)));
499ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        }
500ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
501ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor        break;
502ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor      }
503ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    }
504ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    if (!MemOps.empty())
505d038f361d2b4368af7ab85bd04d5aafcc3ea649dDouglas Gregor      Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
506ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  }
507ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor
508ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  std::vector<MVT::ValueType> RetVals;
509ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  MVT::ValueType RetTyVT = getValueType(RetTy);
510ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  if (RetTyVT != MVT::isVoid)
511d31351288e25e8a7a2a919c99ffe281c2a41b53cDaniel Dunbar    RetVals.push_back(RetTyVT);
512ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  RetVals.push_back(MVT::Other);
513ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor
51444ee0a710c59d8e6793189f903bae21c16814324Eli Friedman  SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
51544ee0a710c59d8e6793189f903bae21c16814324Eli Friedman                                            Chain, Callee, args_to_use), 0);
51644ee0a710c59d8e6793189f903bae21c16814324Eli Friedman  Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
51744ee0a710c59d8e6793189f903bae21c16814324Eli Friedman  Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
518bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara                      DAG.getConstant(NumBytes, getPointerTy()));
519ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  return std::make_pair(TheCall, Chain);
520ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor}
521ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor
522ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas GregorSDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
523ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor                                            Value *VAListV, SelectionDAG &DAG) {
524ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  // vastart just stores the address of the VarArgsFrameIndex slot into the
525ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  // memory location argument.
526ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
527ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
528ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor                     DAG.getSrcValue(VAListV));
529ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor}
530ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor
531ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregorstd::pair<SDOperand,SDOperand>
532ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas GregorPPC32TargetLowering::LowerVAArg(SDOperand Chain,
533ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor                                SDOperand VAListP, Value *VAListV,
534ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor                                const Type *ArgTy, SelectionDAG &DAG) {
535ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  MVT::ValueType ArgVT = getValueType(ArgTy);
536ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor
537ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  SDOperand VAList =
538ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
539ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
540ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  unsigned Amt;
541ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
542ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    Amt = 4;
543ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  else {
544ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
545ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor           "Other types should have been promoted for varargs!");
546ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    Amt = 8;
547ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  }
548ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
549ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor                       DAG.getConstant(Amt, VAList.getValueType()));
550ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
551ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor                      VAList, VAListP, DAG.getSrcValue(VAListV));
552ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  return std::make_pair(Result, Chain);
553ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor}
554ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor
555ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor
556ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregorstd::pair<SDOperand, SDOperand> PPC32TargetLowering::
557ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas GregorLowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
558ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor                        SelectionDAG &DAG) {
559ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  assert(0 && "LowerFrameReturnAddress unimplemented");
560ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  abort();
561ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor}
562ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor
563ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas GregorMachineBasicBlock *
564ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas GregorPPC32TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
565ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor                                             MachineBasicBlock *BB) {
566ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
567ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor          MI->getOpcode() == PPC::SELECT_CC_FP) &&
568ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor         "Unexpected instr type to insert");
569ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor
570c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor  // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
571ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  // control-flow pattern.  The incoming instruction knows the destination vreg
572ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  // to set, the condition code register to branch on, the true/false values to
573ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  // select between, and a branch opcode to use.
574ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  const BasicBlock *LLVM_BB = BB->getBasicBlock();
575ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  ilist<MachineBasicBlock>::iterator It = BB;
576ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  ++It;
577ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor
578ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  //  thisMBB:
579ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  //  ...
580ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  //   TrueVal = ...
581ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  //   cmpTY ccX, r1, r2
582ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  //   bCC copy1MBB
58351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  //   fallthrough --> copy0MBB
584ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  MachineBasicBlock *thisMBB = BB;
585ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
586ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
58751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
588162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
58934a0447b8072e0da14c0980597da9d03a1495662John McCall  MachineFunction *F = BB->getParent();
5909036d5e369aae65e3baccdeed74c796e3d367b3dArgyrios Kyrtzidis  F->getBasicBlockList().insert(It, copy0MBB);
59134a0447b8072e0da14c0980597da9d03a1495662John McCall  F->getBasicBlockList().insert(It, sinkMBB);
5929036d5e369aae65e3baccdeed74c796e3d367b3dArgyrios Kyrtzidis  // Update machine-CFG edges
5933cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  BB->addSuccessor(copy0MBB);
5943cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  BB->addSuccessor(sinkMBB);
5953cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
5963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  //  copy0MBB:
5973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  //   %FalseValue = ...
5983cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  //   # fallthrough to sinkMBB
599427964e15f1b9595659cea3fcb4dd808a00f37b5Argyrios Kyrtzidis  BB = copy0MBB;
600427964e15f1b9595659cea3fcb4dd808a00f37b5Argyrios Kyrtzidis
601427964e15f1b9595659cea3fcb4dd808a00f37b5Argyrios Kyrtzidis  // Update machine-CFG edges
602427964e15f1b9595659cea3fcb4dd808a00f37b5Argyrios Kyrtzidis  BB->addSuccessor(sinkMBB);
6033cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
6043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  //  sinkMBB:
605ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
606ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  //  ...
607ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  BB = sinkMBB;
608ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
609ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
610ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
611ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
612ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  delete MI;   // The pseudo instruction is gone now.
613ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return BB;
614ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
615ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
616ed97649e9574b9d854fa4d6109c9333ae0993554John McCall