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