PPCInstrInfo.cpp revision 894018228b0e0bdbd7aa7e8f47d4a9458789ca82
1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- PPCInstrInfo.cpp - PowerPC32 Instruction Information -----*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file contains the PowerPC implementation of the TargetInstrInfo class.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "PPCInstrInfo.h"
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "PPCInstrBuilder.h"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "PPCMachineFunctionInfo.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "PPCPredicates.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "PPCGenInstrInfo.inc"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "PPCTargetMachine.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/STLExtras.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFrameInfo.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineInstrBuilder.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineMemOperand.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineRegisterInfo.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/PseudoSourceValue.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/CommandLine.h"
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/raw_ostream.h"
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCAsmInfo.h"
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanextern cl::opt<bool> EnablePPC32RS;  // FIXME (64-bit): See PPCRegisterInfo.cpp.
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanextern cl::opt<bool> EnablePPC64RS;  // FIXME (64-bit): See PPCRegisterInfo.cpp.
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RI(*TM.getSubtargetImpl(), *this) {}
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           int &FrameIndex) const {
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (MI->getOpcode()) {
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::LD:
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::LWZ:
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::LFS:
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::LFD:
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MI->getOperand(2).isFI()) {
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      FrameIndex = MI->getOperand(2).getIndex();
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return MI->getOperand(0).getReg();
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0;
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          int &FrameIndex) const {
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (MI->getOpcode()) {
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::STD:
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::STW:
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::STFS:
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::STFD:
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MI->getOperand(2).isFI()) {
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      FrameIndex = MI->getOperand(2).getIndex();
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return MI->getOperand(0).getReg();
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0;
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// commuteInstruction - We can commute rlwimi instructions, but only if the
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// rotate amt is zero.  We also have to munge the immediates a bit.
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineInstr *
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = *MI->getParent()->getParent();
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Normal instructions can be commuted the obvious way.
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MI->getOpcode() != PPC::RLWIMI)
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Cannot commute if it has a non-zero rotate count.
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MI->getOperand(3).getImm() != 0)
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 0;
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we have a zero rotate count, we have:
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   M = mask(MB,ME)
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   Op0 = (Op1 & ~M) | (Op2 & M)
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Change this to:
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   M = mask((ME+1)&31, (MB-1)&31)
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   Op0 = (Op2 & ~M) | (Op1 & M)
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Swap op1/op2
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Reg0 = MI->getOperand(0).getReg();
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Reg1 = MI->getOperand(1).getReg();
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Reg2 = MI->getOperand(2).getReg();
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Reg1IsKill = MI->getOperand(1).isKill();
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Reg2IsKill = MI->getOperand(2).isKill();
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool ChangeReg0 = false;
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If machine instrs are no longer in two-address forms, update
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // destination register as well.
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Reg0 == Reg1) {
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Must be two address instruction!
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Expecting a two-address instruction!");
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reg2IsKill = false;
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ChangeReg0 = true;
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Masks.
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned MB = MI->getOperand(4).getImm();
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned ME = MI->getOperand(5).getImm();
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NewMI) {
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Create a new instruction.
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool Reg0IsDead = MI->getOperand(0).isDead();
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(Reg2, getKillRegState(Reg2IsKill))
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(Reg1, getKillRegState(Reg1IsKill))
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addImm((ME+1) & 31)
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addImm((MB-1) & 31);
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ChangeReg0)
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MI->getOperand(0).setReg(Reg2);
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->getOperand(2).setReg(Reg1);
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->getOperand(1).setReg(Reg2);
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->getOperand(2).setIsKill(Reg1IsKill);
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->getOperand(1).setIsKill(Reg2IsKill);
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Swap the mask around.
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->getOperand(4).setImm((ME+1) & 31);
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->getOperand(5).setImm((MB-1) & 31);
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return MI;
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              MachineBasicBlock::iterator MI) const {
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL;
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(MBB, MI, DL, get(PPC::NOP));
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Branch analysis.
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 MachineBasicBlock *&FBB,
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 SmallVectorImpl<MachineOperand> &Cond,
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 bool AllowModify) const {
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the block has no terminators, it just falls into the block after it.
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock::iterator I = MBB.end();
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I == MBB.begin())
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  --I;
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (I->isDebugValue()) {
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (I == MBB.begin())
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    --I;
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isUnpredicatedTerminator(I))
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the last instruction in the block.
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr *LastInst = I;
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If there is only one terminator instruction, process it.
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (LastInst->getOpcode() == PPC::B) {
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!LastInst->getOperand(0).isMBB())
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TBB = LastInst->getOperand(0).getMBB();
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (LastInst->getOpcode() == PPC::BCC) {
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!LastInst->getOperand(2).isMBB())
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Block ends with fall-through condbranch.
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TBB = LastInst->getOperand(2).getMBB();
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond.push_back(LastInst->getOperand(0));
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond.push_back(LastInst->getOperand(1));
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise, don't know what this is.
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the instruction before it if it's a terminator.
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr *SecondLastInst = I;
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If there are three terminators, we don't know what sort of block this is.
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SecondLastInst && I != MBB.begin() &&
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      isUnpredicatedTerminator(--I))
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the block ends with PPC::B and PPC:BCC, handle it.
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SecondLastInst->getOpcode() == PPC::BCC &&
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LastInst->getOpcode() == PPC::B) {
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!SecondLastInst->getOperand(2).isMBB() ||
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        !LastInst->getOperand(0).isMBB())
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TBB =  SecondLastInst->getOperand(2).getMBB();
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Cond.push_back(SecondLastInst->getOperand(0));
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Cond.push_back(SecondLastInst->getOperand(1));
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FBB = LastInst->getOperand(0).getMBB();
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the block ends with two PPC:Bs, handle it.  The second one is not
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // executed, so remove it.
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SecondLastInst->getOpcode() == PPC::B &&
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LastInst->getOpcode() == PPC::B) {
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!SecondLastInst->getOperand(0).isMBB())
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TBB = SecondLastInst->getOperand(0).getMBB();
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    I = LastInst;
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (AllowModify)
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      I->eraseFromParent();
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, can't handle this.
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock::iterator I = MBB.end();
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I == MBB.begin()) return 0;
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  --I;
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (I->isDebugValue()) {
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (I == MBB.begin())
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return 0;
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    --I;
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 0;
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Remove the branch.
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  I->eraseFromParent();
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  I = MBB.end();
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I == MBB.begin()) return 1;
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  --I;
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I->getOpcode() != PPC::BCC)
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 1;
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Remove the branch.
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  I->eraseFromParent();
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 2;
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           MachineBasicBlock *FBB,
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           const SmallVectorImpl<MachineOperand> &Cond,
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DebugLoc DL) const {
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Shouldn't be a fall through.
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(TBB && "InsertBranch must not be told to insert a fallthrough");
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((Cond.size() == 2 || Cond.size() == 0) &&
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "PPC branch conditions have two components!");
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // One-way branch.
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (FBB == 0) {
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Cond.empty())   // Unconditional branch
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else                // Conditional branch
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BuildMI(&MBB, DL, get(PPC::BCC))
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 1;
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Two-way Conditional Branch.
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(&MBB, DL, get(PPC::BCC))
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 2;
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               MachineBasicBlock::iterator I, DebugLoc DL,
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               unsigned DestReg, unsigned SrcReg,
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               bool KillSrc) const {
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opc;
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opc = PPC::OR;
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opc = PPC::OR8;
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opc = PPC::FMR;
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opc = PPC::MCRF;
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opc = PPC::VOR;
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opc = PPC::CROR;
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    llvm_unreachable("Impossible reg-to-reg copy");
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetInstrDesc &TID = get(Opc);
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (TID.getNumOperands() == 3)
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BuildMI(MBB, I, DL, TID, DestReg)
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BuildMI(MBB, I, DL, TID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  unsigned SrcReg, bool isKill,
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  int FrameIdx,
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  const TargetRegisterClass *RC,
318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  SmallVectorImpl<MachineInstr*> &NewMIs) const{
319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL;
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (RC == PPC::GPRCRegisterClass) {
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SrcReg != PPC::LR) {
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         .addReg(SrcReg,
324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 getKillRegState(isKill)),
325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         FrameIdx));
326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // FIXME: this spills LR immediately to memory in one step.  To do this,
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // we use R11, which we know cannot be used in the prolog/epilog.  This is
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // a hack.
330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         .addReg(PPC::R11,
333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 getKillRegState(isKill)),
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         FrameIdx));
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::G8RCRegisterClass) {
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SrcReg != PPC::LR8) {
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         .addReg(SrcReg,
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 getKillRegState(isKill)),
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         FrameIdx));
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // FIXME: this spills LR immediately to memory in one step.  To do this,
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // we use R11, which we know cannot be used in the prolog/epilog.  This is
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // a hack.
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         .addReg(PPC::X11,
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 getKillRegState(isKill)),
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         FrameIdx));
351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::F8RCRegisterClass) {
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       .addReg(SrcReg,
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               getKillRegState(isKill)),
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       FrameIdx));
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::F4RCRegisterClass) {
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       .addReg(SrcReg,
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               getKillRegState(isKill)),
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       FrameIdx));
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::CRRCRegisterClass) {
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // FIXME (64-bit): Enable
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         .addReg(SrcReg,
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 getKillRegState(isKill)),
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         FrameIdx));
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // FIXME: We need a scatch reg here.  The trouble with using R0 is that
373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // it's possible for the stack frame to be so big the save location is
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // out of range of immediate offsets, necessitating another register.
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // We hack this on Darwin by reserving R2.  It's probably broken on Linux
376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // at the moment.
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // We need to store the CR in the low 4-bits of the saved value.  First,
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // issue a MFCR to save all of the CRBits.
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                           PPC::R2 : PPC::R0;
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCRpseud), ScratchReg)
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               .addReg(SrcReg, getKillRegState(isKill)));
384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If the saved register wasn't CR0, shift the bits left so that they are
386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // in CR0's slot.
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (SrcReg != PPC::CR0) {
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // rlwinm scratch, scratch, ShiftBits, 0, 31.
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       .addReg(ScratchReg).addImm(ShiftBits)
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       .addImm(0).addImm(31));
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         .addReg(ScratchReg,
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 getKillRegState(isKill)),
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         FrameIdx));
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::CRBITRCRegisterClass) {
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // backend currently only uses CR1EQ as an individual bit, this should
403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // not cause any bug. If we need other uses of CR bits, the following
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // code may be invalid.
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Reg = 0;
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR0;
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR1;
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR2;
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR3;
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR4;
421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR5;
424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR6;
427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR7;
430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               PPC::CRRCRegisterClass, NewMIs);
433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::VRRCRegisterClass) {
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We don't have indexed addressing for vector loads.  Emit:
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // R0 = ADDI FI#
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // STVX VAL, 0, R0
438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: We use R0 here, because it isn't available for RA.
440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       FrameIdx, 0, 0));
442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     .addReg(SrcReg, getKillRegState(isKill))
444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     .addReg(PPC::R0)
445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     .addReg(PPC::R0));
446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    llvm_unreachable("Unknown regclass!");
448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid
454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  MachineBasicBlock::iterator MI,
456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  unsigned SrcReg, bool isKill, int FrameIdx,
457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  const TargetRegisterClass *RC,
458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  const TargetRegisterInfo *TRI) const {
459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = *MBB.getParent();
460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<MachineInstr*, 4> NewMIs;
461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FuncInfo->setSpillsCR();
465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MBB.insert(MI, NewMIs[i]);
469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const MachineFrameInfo &MFI = *MF.getFrameInfo();
471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineMemOperand *MMO =
472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIdx),
473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            MachineMemOperand::MOStore, /*Offset=*/0,
474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            MFI.getObjectSize(FrameIdx),
475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            MFI.getObjectAlignment(FrameIdx));
476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  NewMIs.back()->addMemOperand(MF, MMO);
477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid
480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   unsigned DestReg, int FrameIdx,
482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   const TargetRegisterClass *RC,
483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   SmallVectorImpl<MachineInstr*> &NewMIs)const{
484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (RC == PPC::GPRCRegisterClass) {
485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (DestReg != PPC::LR) {
486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 DestReg), FrameIdx));
488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 PPC::R11), FrameIdx));
491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::G8RCRegisterClass) {
494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (DestReg != PPC::LR8) {
495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         FrameIdx));
497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 PPC::R11), FrameIdx));
500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::F8RCRegisterClass) {
503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       FrameIdx));
505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::F4RCRegisterClass) {
506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       FrameIdx));
508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::CRRCRegisterClass) {
509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: We need a scatch reg here.  The trouble with using R0 is that
510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // it's possible for the stack frame to be so big the save location is
511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // out of range of immediate offsets, necessitating another register.
512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We hack this on Darwin by reserving R2.  It's probably broken on Linux
513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // at the moment.
514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                          PPC::R2 : PPC::R0;
516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       ScratchReg), FrameIdx));
518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the reloaded register isn't CR0, shift the bits right so that they are
520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // in the right CR's slot.
521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (DestReg != PPC::CR0) {
522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // rlwinm r11, r11, 32-ShiftBits, 0, 31.
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    .addImm(31));
527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg)
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     .addReg(ScratchReg));
531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::CRBITRCRegisterClass) {
532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Reg = 0;
534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR0;
537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR1;
540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR2;
543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR3;
546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR4;
549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR5;
552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR6;
555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = PPC::CR7;
558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                PPC::CRRCRegisterClass, NewMIs);
561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (RC == PPC::VRRCRegisterClass) {
563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We don't have indexed addressing for vector loads.  Emit:
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // R0 = ADDI FI#
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Dest = LVX 0, R0
566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //
567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: We use R0 here, because it isn't available for RA.
568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       FrameIdx, 0, 0));
570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     .addReg(PPC::R0));
572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    llvm_unreachable("Unknown regclass!");
574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid
578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   MachineBasicBlock::iterator MI,
580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   unsigned DestReg, int FrameIdx,
581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   const TargetRegisterClass *RC,
582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   const TargetRegisterInfo *TRI) const {
583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = *MBB.getParent();
584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<MachineInstr*, 4> NewMIs;
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL;
586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MI != MBB.end()) DL = MI->getDebugLoc();
587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MBB.insert(MI, NewMIs[i]);
590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const MachineFrameInfo &MFI = *MF.getFrameInfo();
592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineMemOperand *MMO =
593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIdx),
594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            MachineMemOperand::MOLoad, /*Offset=*/0,
595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            MFI.getObjectSize(FrameIdx),
596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            MFI.getObjectAlignment(FrameIdx));
597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  NewMIs.back()->addMemOperand(MF, MMO);
598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineInstr*
601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       int FrameIx, uint64_t Offset,
603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       const MDNode *MDPtr,
604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       DebugLoc DL) const {
605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return &*MIB;
608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool PPCInstrInfo::
611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Leave the CR# the same, but invert the condition.
614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// GetInstSize - Return the number of bytes of code the specified
619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// instruction may be.  This returns the maximum number of bytes.
620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (MI->getOpcode()) {
623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::INLINEASM: {       // Inline Asm: Variable size.
624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const MachineFunction *MF = MI->getParent()->getParent();
625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const char *AsmStr = MI->getOperand(0).getSymbolName();
626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::PROLOG_LABEL:
629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::EH_LABEL:
630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::GC_LABEL:
631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::DBG_VALUE:
632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 0;
633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 4; // PowerPC instructions are all 4 bytes
635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
637