PPCInstrInfo.cpp revision 864e2efce2cb5d02e376933933d96074723fe77c
1a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM//===- PPCInstrInfo.cpp - PowerPC32 Instruction Information -----*- C++ -*-===//
2a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM//
3a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM//                     The LLVM Compiler Infrastructure
4a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM//
5a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM// This file is distributed under the University of Illinois Open Source
6a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM// License. See LICENSE.TXT for details.
7a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM//
8a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM//===----------------------------------------------------------------------===//
9a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM//
10a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM// This file contains the PowerPC implementation of the TargetInstrInfo class.
11a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM//
12a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM//===----------------------------------------------------------------------===//
13a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
14a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "PPCInstrInfo.h"
15a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "PPCInstrBuilder.h"
16a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "PPCMachineFunctionInfo.h"
17a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "PPCPredicates.h"
18a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "PPCGenInstrInfo.inc"
19a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "PPCTargetMachine.h"
20a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "llvm/ADT/STLExtras.h"
21a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "llvm/CodeGen/MachineInstrBuilder.h"
22a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "llvm/Support/CommandLine.h"
23a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "llvm/Support/ErrorHandling.h"
24a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "llvm/Support/raw_ostream.h"
25a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM#include "llvm/MC/MCAsmInfo.h"
26a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLMusing namespace llvm;
27a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
28a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLMextern cl::opt<bool> EnablePPC32RS;  // FIXME (64-bit): See PPCRegisterInfo.cpp.
29a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLMextern cl::opt<bool> EnablePPC64RS;  // FIXME (64-bit): See PPCRegisterInfo.cpp.
30a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
31a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLMPPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
32a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
33a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    RI(*TM.getSubtargetImpl(), *this) {}
34a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
35a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLMbool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
36a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                               unsigned& sourceReg,
37a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                               unsigned& destReg,
38a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                               unsigned& sourceSubIdx,
39a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                               unsigned& destSubIdx) const {
40a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  sourceSubIdx = destSubIdx = 0; // No sub-registers.
41a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
42a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  unsigned oc = MI.getOpcode();
43a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
44a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      oc == PPC::OR4To8 || oc == PPC::OR8To4) {                // or r1, r2, r2
45a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    assert(MI.getNumOperands() >= 3 &&
46a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM           MI.getOperand(0).isReg() &&
47a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM           MI.getOperand(1).isReg() &&
48a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM           MI.getOperand(2).isReg() &&
49a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM           "invalid PPC OR instruction!");
50a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
51a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      sourceReg = MI.getOperand(1).getReg();
52a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      destReg = MI.getOperand(0).getReg();
53a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      return true;
54a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    }
55a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  } else if (oc == PPC::ADDI) {             // addi r1, r2, 0
56a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    assert(MI.getNumOperands() >= 3 &&
57a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM           MI.getOperand(0).isReg() &&
58a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM           MI.getOperand(2).isImm() &&
59a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM           "invalid PPC ADDI instruction!");
60a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
61a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      sourceReg = MI.getOperand(1).getReg();
62a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      destReg = MI.getOperand(0).getReg();
63a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      return true;
64a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    }
65a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  } else if (oc == PPC::ORI) {             // ori r1, r2, 0
66a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    assert(MI.getNumOperands() >= 3 &&
67a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM           MI.getOperand(0).isReg() &&
68a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM           MI.getOperand(1).isReg() &&
69a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM           MI.getOperand(2).isImm() &&
70a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM           "invalid PPC ORI instruction!");
71a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    if (MI.getOperand(2).getImm() == 0) {
72a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      sourceReg = MI.getOperand(1).getReg();
73a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      destReg = MI.getOperand(0).getReg();
74a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      return true;
75b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    }
76b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
77b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball             oc == PPC::FMRSD) {      // fmr r1, r2
78b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    assert(MI.getNumOperands() >= 2 &&
79b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball           MI.getOperand(0).isReg() &&
80b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball           MI.getOperand(1).isReg() &&
81b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball           "invalid PPC FMR instruction");
82b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    sourceReg = MI.getOperand(1).getReg();
83b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    destReg = MI.getOperand(0).getReg();
84b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    return true;
85b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  } else if (oc == PPC::MCRF) {             // mcrf cr1, cr2
86b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    assert(MI.getNumOperands() >= 2 &&
87b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball           MI.getOperand(0).isReg() &&
88b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball           MI.getOperand(1).isReg() &&
89b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball           "invalid PPC MCRF instruction");
90b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    sourceReg = MI.getOperand(1).getReg();
91b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    destReg = MI.getOperand(0).getReg();
92b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    return true;
93b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  }
94b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  return false;
95b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball}
96b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
97b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ballunsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
98b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball                                           int &FrameIndex) const {
99b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  switch (MI->getOpcode()) {
100b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  default: break;
101b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  case PPC::LD:
102b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  case PPC::LWZ:
103b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  case PPC::LFS:
104b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  case PPC::LFD:
105b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
106b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball        MI->getOperand(2).isFI()) {
107b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball      FrameIndex = MI->getOperand(2).getIndex();
108b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball      return MI->getOperand(0).getReg();
109b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    }
110b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    break;
111b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  }
112b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  return 0;
113b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball}
114b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
1158ba4466a4ad458618282f8bdcc2706025856a9f2Chris Ballunsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
1168ba4466a4ad458618282f8bdcc2706025856a9f2Chris Ball                                          int &FrameIndex) const {
117b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  switch (MI->getOpcode()) {
118b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  default: break;
119b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  case PPC::STD:
120b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  case PPC::STW:
121b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  case PPC::STFS:
122b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  case PPC::STFD:
123b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
124b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball        MI->getOperand(2).isFI()) {
125b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball      FrameIndex = MI->getOperand(2).getIndex();
126b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball      return MI->getOperand(0).getReg();
127b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    }
128b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    break;
129b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  }
130b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  return 0;
131b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball}
132b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
133b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball// commuteInstruction - We can commute rlwimi instructions, but only if the
134b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball// rotate amt is zero.  We also have to munge the immediates a bit.
135b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris BallMachineInstr *
136b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris BallPPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
137b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  MachineFunction &MF = *MI->getParent()->getParent();
138b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
139b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  // Normal instructions can be commuted the obvious way.
140b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  if (MI->getOpcode() != PPC::RLWIMI)
141b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
142b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
1438ba4466a4ad458618282f8bdcc2706025856a9f2Chris Ball  // Cannot commute if it has a non-zero rotate count.
1448ba4466a4ad458618282f8bdcc2706025856a9f2Chris Ball  if (MI->getOperand(3).getImm() != 0)
145b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    return 0;
146b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
147b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  // If we have a zero rotate count, we have:
148b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  //   M = mask(MB,ME)
149b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  //   Op0 = (Op1 & ~M) | (Op2 & M)
150b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  // Change this to:
151b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  //   M = mask((ME+1)&31, (MB-1)&31)
152b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  //   Op0 = (Op2 & ~M) | (Op1 & M)
153b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
154b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  // Swap op1/op2
155b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  unsigned Reg0 = MI->getOperand(0).getReg();
156b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  unsigned Reg1 = MI->getOperand(1).getReg();
157b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  unsigned Reg2 = MI->getOperand(2).getReg();
158b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  bool Reg1IsKill = MI->getOperand(1).isKill();
159b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  bool Reg2IsKill = MI->getOperand(2).isKill();
160b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  bool ChangeReg0 = false;
161b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  // If machine instrs are no longer in two-address forms, update
162b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  // destination register as well.
163b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  if (Reg0 == Reg1) {
164b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    // Must be two address instruction!
165b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
166b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball           "Expecting a two-address instruction!");
167b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    Reg2IsKill = false;
168b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    ChangeReg0 = true;
169b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  }
170b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
171b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  // Masks.
172b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  unsigned MB = MI->getOperand(4).getImm();
173b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  unsigned ME = MI->getOperand(5).getImm();
174b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das
175b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  if (NewMI) {
176b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das    // Create a new instruction.
177b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das    unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
178b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das    bool Reg0IsDead = MI->getOperand(0).isDead();
179b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das    return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
180b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das      .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
181b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das      .addReg(Reg2, getKillRegState(Reg2IsKill))
182b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das      .addReg(Reg1, getKillRegState(Reg1IsKill))
183b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das      .addImm((ME+1) & 31)
184b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das      .addImm((MB-1) & 31);
185b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  }
186b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das
187b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  if (ChangeReg0)
188b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das    MI->getOperand(0).setReg(Reg2);
189b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  MI->getOperand(2).setReg(Reg1);
190b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  MI->getOperand(1).setReg(Reg2);
191b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  MI->getOperand(2).setIsKill(Reg1IsKill);
192b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  MI->getOperand(1).setIsKill(Reg2IsKill);
193b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das
194b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  // Swap the mask around.
195b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  MI->getOperand(4).setImm((ME+1) & 31);
196b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  MI->getOperand(5).setImm((MB-1) & 31);
197b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  return MI;
198b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das}
199b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das
200b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Dasvoid PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
201b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das                              MachineBasicBlock::iterator MI) const {
202b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  DebugLoc DL = DebugLoc::getUnknownLoc();
203b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  if (MI != MBB.end()) DL = MI->getDebugLoc();
204b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das
205b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  BuildMI(MBB, MI, DL, get(PPC::NOP));
206b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das}
207b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das
208b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das
209b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das// Branch analysis.
210b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Dasbool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
211b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das                                 MachineBasicBlock *&FBB,
212b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das                                 SmallVectorImpl<MachineOperand> &Cond,
213b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das                                 bool AllowModify) const {
214b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  // If the block has no terminators, it just falls into the block after it.
215b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  MachineBasicBlock::iterator I = MBB.end();
216b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das  if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
2177bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    return false;
2187bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO
2197bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  // Get the last instruction in the block.
2207bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  MachineInstr *LastInst = I;
2217bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO
2227bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  // If there is only one terminator instruction, process it.
2237bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
2247bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    if (LastInst->getOpcode() == PPC::B) {
2257bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      if (!LastInst->getOperand(0).isMBB())
2267bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO        return true;
2277bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      TBB = LastInst->getOperand(0).getMBB();
2287bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      return false;
2297bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    } else if (LastInst->getOpcode() == PPC::BCC) {
2307bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      if (!LastInst->getOperand(2).isMBB())
2317bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO        return true;
2327bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      // Block ends with fall-through condbranch.
2337bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      TBB = LastInst->getOperand(2).getMBB();
2347bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      Cond.push_back(LastInst->getOperand(0));
2357bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      Cond.push_back(LastInst->getOperand(1));
2367bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      return false;
2377bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    }
2387bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    // Otherwise, don't know what this is.
2397bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    return true;
2407bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  }
2417bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO
2427bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  // Get the instruction before it if it's a terminator.
2437bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  MachineInstr *SecondLastInst = I;
2447bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO
2457bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  // If there are three terminators, we don't know what sort of block this is.
2467bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  if (SecondLastInst && I != MBB.begin() &&
2477bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      isUnpredicatedTerminator(--I))
2487bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    return true;
2497bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO
2507bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  // If the block ends with PPC::B and PPC:BCC, handle it.
2517bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  if (SecondLastInst->getOpcode() == PPC::BCC &&
2527bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      LastInst->getOpcode() == PPC::B) {
2537bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    if (!SecondLastInst->getOperand(2).isMBB() ||
2547bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO        !LastInst->getOperand(0).isMBB())
2557bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      return true;
2567bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    TBB =  SecondLastInst->getOperand(2).getMBB();
2577bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    Cond.push_back(SecondLastInst->getOperand(0));
2587bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    Cond.push_back(SecondLastInst->getOperand(1));
2597bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    FBB = LastInst->getOperand(0).getMBB();
2607bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    return false;
2617bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  }
2627bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO
2637bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  // If the block ends with two PPC:Bs, handle it.  The second one is not
2647bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  // executed, so remove it.
2657bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  if (SecondLastInst->getOpcode() == PPC::B &&
2667bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      LastInst->getOpcode() == PPC::B) {
2677bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    if (!SecondLastInst->getOperand(0).isMBB())
2687bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      return true;
2697bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    TBB = SecondLastInst->getOperand(0).getMBB();
2707bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    I = LastInst;
2717bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    if (AllowModify)
2727bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO      I->eraseFromParent();
2737bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO    return false;
2747bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  }
2757bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO
2767bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  // Otherwise, can't handle this.
2777bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  return true;
2787bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO}
2797bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO
2807bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLAROunsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
2817bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  MachineBasicBlock::iterator I = MBB.end();
2827bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO  if (I == MBB.begin()) return 0;
283f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  --I;
284f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
285f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball    return 0;
286f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball
287f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  // Remove the branch.
288f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  I->eraseFromParent();
289f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball
290f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  I = MBB.end();
291f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball
292f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  if (I == MBB.begin()) return 1;
293f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  --I;
294f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  if (I->getOpcode() != PPC::BCC)
295f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball    return 1;
296f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball
297f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  // Remove the branch.
298f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  I->eraseFromParent();
299f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  return 2;
300f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball}
301f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball
302f74dfe23cd00894aa9f235374468e05acb793e17Chris Ballunsigned
303f74dfe23cd00894aa9f235374468e05acb793e17Chris BallPPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
304f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball                           MachineBasicBlock *FBB,
305f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball                           const SmallVectorImpl<MachineOperand> &Cond) const {
306f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  // FIXME this should probably have a DebugLoc argument
307f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  DebugLoc dl = DebugLoc::getUnknownLoc();
308f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  // Shouldn't be a fall through.
309f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  assert(TBB && "InsertBranch must not be told to insert a fallthrough");
310f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  assert((Cond.size() == 2 || Cond.size() == 0) &&
311f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball         "PPC branch conditions have two components!");
312f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball
313f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  // One-way branch.
314f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  if (FBB == 0) {
315f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball    if (Cond.empty())   // Unconditional branch
316f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball      BuildMI(&MBB, dl, get(PPC::B)).addMBB(TBB);
317f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball    else                // Conditional branch
318f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball      BuildMI(&MBB, dl, get(PPC::BCC))
319f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball        .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
320f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball    return 1;
321f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  }
322f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball
323f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  // Two-way Conditional Branch.
324f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  BuildMI(&MBB, dl, get(PPC::BCC))
325f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball    .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
326f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  BuildMI(&MBB, dl, get(PPC::B)).addMBB(FBB);
327f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  return 2;
328f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball}
329f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball
330f74dfe23cd00894aa9f235374468e05acb793e17Chris Ballbool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
331f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball                                   MachineBasicBlock::iterator MI,
332f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball                                   unsigned DestReg, unsigned SrcReg,
333f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball                                   const TargetRegisterClass *DestRC,
334f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball                                   const TargetRegisterClass *SrcRC) const {
335f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  if (DestRC != SrcRC) {
336f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball    // Not yet supported!
337f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball    return false;
338f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  }
339f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball
340f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  DebugLoc DL = DebugLoc::getUnknownLoc();
341f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball  if (MI != MBB.end()) DL = MI->getDebugLoc();
3428649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung
3438649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  if (DestRC == PPC::GPRCRegisterClass) {
3448649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung    BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
3458649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  } else if (DestRC == PPC::G8RCRegisterClass) {
3468649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung    BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
3478649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  } else if (DestRC == PPC::F4RCRegisterClass) {
3488649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung    BuildMI(MBB, MI, DL, get(PPC::FMRS), DestReg).addReg(SrcReg);
3498649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  } else if (DestRC == PPC::F8RCRegisterClass) {
3508649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung    BuildMI(MBB, MI, DL, get(PPC::FMRD), DestReg).addReg(SrcReg);
3518649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  } else if (DestRC == PPC::CRRCRegisterClass) {
3528649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung    BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
3538649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  } else if (DestRC == PPC::VRRCRegisterClass) {
3548649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung    BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
3558649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  } else if (DestRC == PPC::CRBITRCRegisterClass) {
3568649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung    BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
3578649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  } else {
3588649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung    // Attempt to copy register that is not GPR or FPR
3598649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung    return false;
3608649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  }
3618649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung
3628649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  return true;
3638649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung}
3648649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung
3658649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chungbool
3668649651b743a5d7c290ea0f8058794f8d127736eJaehoon ChungPPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
3678649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung                                  unsigned SrcReg, bool isKill,
3688649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung                                  int FrameIdx,
3698649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung                                  const TargetRegisterClass *RC,
3708649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung                                  SmallVectorImpl<MachineInstr*> &NewMIs) const{
3718649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  DebugLoc DL = DebugLoc::getUnknownLoc();
3728649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung  if (RC == PPC::GPRCRegisterClass) {
3738649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung    if (SrcReg != PPC::LR) {
3748649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
3758649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung                                         .addReg(SrcReg,
3768649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung                                                 getKillRegState(isKill)),
3778649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung                                         FrameIdx));
3788649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung    } else {
3798649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung      // FIXME: this spills LR immediately to memory in one step.  To do this,
380a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      // we use R11, which we know cannot be used in the prolog/epilog.  This is
381a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      // a hack.
382a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
383a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
384a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                                         .addReg(PPC::R11,
385a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                                 getKillRegState(isKill)),
386a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                                         FrameIdx));
3878ba4466a4ad458618282f8bdcc2706025856a9f2Chris Ball    }
3888ba4466a4ad458618282f8bdcc2706025856a9f2Chris Ball  } else if (RC == PPC::G8RCRegisterClass) {
389a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    if (SrcReg != PPC::LR8) {
390a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
391a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                                         .addReg(SrcReg,
392a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                                                 getKillRegState(isKill)),
393a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                                         FrameIdx));
394a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    } else {
395a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      // FIXME: this spills LR immediately to memory in one step.  To do this,
396a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      // we use R11, which we know cannot be used in the prolog/epilog.  This is
397a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      // a hack.
398a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
399a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
400a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                                         .addReg(PPC::X11,
401a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                                                 getKillRegState(isKill)),
402a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM                                         FrameIdx));
403a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    }
404a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (RC == PPC::F8RCRegisterClass) {
405a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
406a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                       .addReg(SrcReg,
407a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                               getKillRegState(isKill)),
408a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                       FrameIdx));
409a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (RC == PPC::F4RCRegisterClass) {
410a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
411a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                       .addReg(SrcReg,
412a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                               getKillRegState(isKill)),
413a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                       FrameIdx));
414a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (RC == PPC::CRRCRegisterClass) {
415a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
416a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO        (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
417a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      // FIXME (64-bit): Enable
418a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
419a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                         .addReg(SrcReg,
420a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                                 getKillRegState(isKill)),
421a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                         FrameIdx));
422a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      return true;
423a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    } else {
424a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      // FIXME: We use R0 here, because it isn't available for RA.  We need to
425a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      // store the CR in the low 4-bits of the saved value.  First, issue a MFCR
426a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      // to save all of the CRBits.
427a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), PPC::R0));
428a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
429a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      // If the saved register wasn't CR0, shift the bits left so that they are
430a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      // in CR0's slot.
431a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      if (SrcReg != PPC::CR0) {
432a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO        unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
433a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO        // rlwinm r0, r0, ShiftBits, 0, 31.
434a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO        NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0)
435a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                       .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
436a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      }
437a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
438a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
439a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                         .addReg(PPC::R0,
440a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                                 getKillRegState(isKill)),
441a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                         FrameIdx));
442b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    }
443a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (RC == PPC::CRBITRCRegisterClass) {
444a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
445a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // backend currently only uses CR1EQ as an individual bit, this should
446a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // not cause any bug. If we need other uses of CR bits, the following
447a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // code may be invalid.
448b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    unsigned Reg = 0;
449a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
450a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO        SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
451a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR0;
452a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
453a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
454a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR1;
455a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
456a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
457a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR2;
458a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
459a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
460a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR3;
461a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
462a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
463a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR4;
464a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
465a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
466a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR5;
467a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
468a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
469a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR6;
470a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
471a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
472a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR7;
473a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
474a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
475a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                               PPC::CRRCRegisterClass, NewMIs);
476a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
477a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (RC == PPC::VRRCRegisterClass) {
478a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // We don't have indexed addressing for vector loads.  Emit:
479a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // R0 = ADDI FI#
480a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // STVX VAL, 0, R0
481a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    //
482a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // FIXME: We use R0 here, because it isn't available for RA.
483a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
484a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                       FrameIdx, 0, 0));
485a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
486a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                     .addReg(SrcReg, getKillRegState(isKill))
487b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball                     .addReg(PPC::R0)
488a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                     .addReg(PPC::R0));
489a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else {
490a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    llvm_unreachable("Unknown regclass!");
491a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  }
492a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
493a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  return false;
494a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO}
495a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
496a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLAROvoid
497a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLAROPPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
498a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                  MachineBasicBlock::iterator MI,
499a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                  unsigned SrcReg, bool isKill, int FrameIdx,
500a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                  const TargetRegisterClass *RC) const {
501a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  MachineFunction &MF = *MBB.getParent();
502a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  SmallVector<MachineInstr*, 4> NewMIs;
503a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
504a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
505a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
506a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    FuncInfo->setSpillsCR();
507a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  }
508b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
509a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
510a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    MBB.insert(MI, NewMIs[i]);
511a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO}
512a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
513a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLAROvoid
514a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLAROPPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
515a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                   unsigned DestReg, int FrameIdx,
516a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                   const TargetRegisterClass *RC,
517a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                   SmallVectorImpl<MachineInstr*> &NewMIs)const{
518a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  if (RC == PPC::GPRCRegisterClass) {
519a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    if (DestReg != PPC::LR) {
520a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
521a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                                 DestReg), FrameIdx));
522a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    } else {
523a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
524a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                                 PPC::R11), FrameIdx));
525a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
526a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    }
527a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (RC == PPC::G8RCRegisterClass) {
528a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    if (DestReg != PPC::LR8) {
529a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
530a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                         FrameIdx));
531a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    } else {
532a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
533a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                                 PPC::R11), FrameIdx));
534a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
535a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    }
536a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (RC == PPC::F8RCRegisterClass) {
537a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
538a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                       FrameIdx));
539a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (RC == PPC::F4RCRegisterClass) {
540a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
541a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                       FrameIdx));
542a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (RC == PPC::CRRCRegisterClass) {
543a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // FIXME: We use R0 here, because it isn't available for RA.
544a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), PPC::R0),
545a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                       FrameIdx));
546a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
547a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // If the reloaded register isn't CR0, shift the bits right so that they are
548a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // in the right CR's slot.
549a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    if (DestReg != PPC::CR0) {
550a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
551a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      // rlwinm r11, r11, 32-ShiftBits, 0, 31.
552a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0)
553a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                    .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
554a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    }
555a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
556a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
557a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (RC == PPC::CRBITRCRegisterClass) {
558a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
559a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    unsigned Reg = 0;
560a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
561a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO        DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
562a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR0;
563a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
564a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
565a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR1;
566a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
567a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
568a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR2;
569a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
570a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
571a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR3;
572a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
573a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
574a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR4;
575a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
576a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
577a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR5;
578a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
579a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
580a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR6;
581a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
582a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO             DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
583a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      Reg = PPC::CR7;
584a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
585a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
586a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                PPC::CRRCRegisterClass, NewMIs);
587a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
588a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (RC == PPC::VRRCRegisterClass) {
589a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // We don't have indexed addressing for vector loads.  Emit:
590a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // R0 = ADDI FI#
591a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // Dest = LVX 0, R0
592a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    //
593a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    // FIXME: We use R0 here, because it isn't available for RA.
594a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
595a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                       FrameIdx, 0, 0));
596a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
597a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                     .addReg(PPC::R0));
598a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else {
599a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    llvm_unreachable("Unknown regclass!");
600a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  }
601a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO}
602a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
603a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLAROvoid
604a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLAROPPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
605a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                   MachineBasicBlock::iterator MI,
606a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                   unsigned DestReg, int FrameIdx,
607a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                   const TargetRegisterClass *RC) const {
608a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  MachineFunction &MF = *MBB.getParent();
609a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  SmallVector<MachineInstr*, 4> NewMIs;
610a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  DebugLoc DL = DebugLoc::getUnknownLoc();
611a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  if (MI != MBB.end()) DL = MI->getDebugLoc();
612a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
613a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
614a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    MBB.insert(MI, NewMIs[i]);
615a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO}
616a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
617a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
618a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO/// copy instructions, turning them into load/store instructions.
619a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLAROMachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
620a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                                  MachineInstr *MI,
621a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                           const SmallVectorImpl<unsigned> &Ops,
622a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                                  int FrameIndex) const {
623a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  if (Ops.size() != 1) return NULL;
624a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
625a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  // Make sure this is a reg-reg copy.  Note that we can't handle MCRF, because
626a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  // it takes more than one instruction to store it.
627a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  unsigned Opc = MI->getOpcode();
628a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  unsigned OpNum = Ops[0];
629a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
630a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  MachineInstr *NewMI = NULL;
631a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  if ((Opc == PPC::OR &&
632a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO       MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
633a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    if (OpNum == 0) {  // move -> store
634a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      unsigned InReg = MI->getOperand(1).getReg();
635a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isKill = MI->getOperand(1).isKill();
636a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isUndef = MI->getOperand(1).isUndef();
637a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
638a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                .addReg(InReg,
639a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getKillRegState(isKill) |
640a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getUndefRegState(isUndef)),
641a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                FrameIndex);
642a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    } else {           // move -> load
643a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      unsigned OutReg = MI->getOperand(0).getReg();
644a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isDead = MI->getOperand(0).isDead();
645a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isUndef = MI->getOperand(0).isUndef();
646a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
647a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                .addReg(OutReg,
648a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        RegState::Define |
649a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getDeadRegState(isDead) |
650a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getUndefRegState(isUndef)),
651a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                FrameIndex);
652a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    }
653b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  } else if ((Opc == PPC::OR8 &&
654a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO              MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
655b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    if (OpNum == 0) {  // move -> store
656a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      unsigned InReg = MI->getOperand(1).getReg();
657a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isKill = MI->getOperand(1).isKill();
658a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isUndef = MI->getOperand(1).isUndef();
659a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
660a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                .addReg(InReg,
661a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getKillRegState(isKill) |
662a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getUndefRegState(isUndef)),
663a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                FrameIndex);
664a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    } else {           // move -> load
665a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      unsigned OutReg = MI->getOperand(0).getReg();
666a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isDead = MI->getOperand(0).isDead();
667a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isUndef = MI->getOperand(0).isUndef();
668a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
669a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                .addReg(OutReg,
670a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        RegState::Define |
671a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getDeadRegState(isDead) |
672a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getUndefRegState(isUndef)),
673a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                FrameIndex);
674a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    }
675a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (Opc == PPC::FMRD) {
676a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    if (OpNum == 0) {  // move -> store
677a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      unsigned InReg = MI->getOperand(1).getReg();
678a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isKill = MI->getOperand(1).isKill();
679a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isUndef = MI->getOperand(1).isUndef();
680a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFD))
681a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                .addReg(InReg,
682a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getKillRegState(isKill) |
683a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getUndefRegState(isUndef)),
684a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                FrameIndex);
685a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    } else {           // move -> load
686a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      unsigned OutReg = MI->getOperand(0).getReg();
687a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isDead = MI->getOperand(0).isDead();
688a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isUndef = MI->getOperand(0).isUndef();
689a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFD))
690a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                .addReg(OutReg,
691a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        RegState::Define |
692a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getDeadRegState(isDead) |
693a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getUndefRegState(isUndef)),
694a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                FrameIndex);
695a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    }
696a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  } else if (Opc == PPC::FMRS) {
697a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    if (OpNum == 0) {  // move -> store
698a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      unsigned InReg = MI->getOperand(1).getReg();
699a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isKill = MI->getOperand(1).isKill();
700a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isUndef = MI->getOperand(1).isUndef();
701a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFS))
702a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                .addReg(InReg,
703a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getKillRegState(isKill) |
704a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getUndefRegState(isUndef)),
705a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                FrameIndex);
706a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    } else {           // move -> load
707a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      unsigned OutReg = MI->getOperand(0).getReg();
708a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isDead = MI->getOperand(0).isDead();
709a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      bool isUndef = MI->getOperand(0).isUndef();
710a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO      NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFS))
711a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                .addReg(OutReg,
712a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        RegState::Define |
713a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getDeadRegState(isDead) |
714a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                        getUndefRegState(isUndef)),
715a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                FrameIndex);
716a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    }
717a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  }
718a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
719a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  return NewMI;
720a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO}
721a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
722a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARObool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
723a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO                                  const SmallVectorImpl<unsigned> &Ops) const {
724a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  if (Ops.size() != 1) return false;
725a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
726a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  // Make sure this is a reg-reg copy.  Note that we can't handle MCRF, because
727a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  // it takes more than one instruction to store it.
728a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  unsigned Opc = MI->getOpcode();
729a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
730a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  if ((Opc == PPC::OR &&
731a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO       MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
732a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    return true;
733a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  else if ((Opc == PPC::OR8 &&
734a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO              MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
735a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    return true;
736a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
737a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    return true;
738a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
739a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  return false;
740a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO}
741a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
742a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
743a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARObool PPCInstrInfo::
744a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLAROReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
745a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
746a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  // Leave the CR# the same, but invert the condition.
747a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
748a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  return false;
749a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO}
750a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO
751a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO/// GetInstSize - Return the number of bytes of code the specified
752a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO/// instruction may be.  This returns the maximum number of bytes.
753a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO///
754a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLAROunsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
755a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  switch (MI->getOpcode()) {
756a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  case PPC::INLINEASM: {       // Inline Asm: Variable size.
757a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    const MachineFunction *MF = MI->getParent()->getParent();
758a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    const char *AsmStr = MI->getOperand(0).getSymbolName();
759a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
760a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  }
761a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  case PPC::DBG_LABEL:
762b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  case PPC::EH_LABEL:
763a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  case PPC::GC_LABEL:
764a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    return 0;
765a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  default:
766a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO    return 4; // PowerPC instructions are all 4 bytes
767a5bf4a2030a9dcfbcebf1b647e65c1e936a56e14Giuseppe CAVALLARO  }
768a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM}
769a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM