PPCInstrInfo.cpp revision 71a2cb25ebc818383dd0f80475bc166f834e8d99
121e463b2bf864671a87ebe386cb100ef9349a540Nate Begeman//===- PPCInstrInfo.cpp - PowerPC32 Instruction Information -----*- C++ -*-===//
2b5f662fa0314f7e7e690aae8ebff7136cc3a5ab0Misha Brukman//
3f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//                     The LLVM Compiler Infrastructure
4f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7b5f662fa0314f7e7e690aae8ebff7136cc3a5ab0Misha Brukman//
8f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//===----------------------------------------------------------------------===//
9f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//
10f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman// This file contains the PowerPC implementation of the TargetInstrInfo class.
11f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//
12f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//===----------------------------------------------------------------------===//
13f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman
1416e71f2f70811c69c56052dd146324fe20e31db5Chris Lattner#include "PPCInstrInfo.h"
15f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson#include "PPCInstrBuilder.h"
167194aaf738a1b89441635340403f1c5b06ae18efBill Wendling#include "PPCMachineFunctionInfo.h"
17df4ed6350b2a51f71c0980e86c9078f4046ea706Chris Lattner#include "PPCPredicates.h"
184c7b43b43fdf943c7298718e15ab5d6dfe345be7Chris Lattner#include "PPCGenInstrInfo.inc"
19b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner#include "PPCTargetMachine.h"
20718cb665ca6ce2bc4d8e8479f46a45db91b49f86Owen Anderson#include "llvm/ADT/STLExtras.h"
21f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman#include "llvm/CodeGen/MachineInstrBuilder.h"
22880d0f6018b6928bdcad291be60c801238619955Bill Wendling#include "llvm/Support/CommandLine.h"
23f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukmanusing namespace llvm;
24f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman
254a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendlingextern cl::opt<bool> EnablePPC32RS;  // FIXME (64-bit): See PPCRegisterInfo.cpp.
264a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendlingextern cl::opt<bool> EnablePPC64RS;  // FIXME (64-bit): See PPCRegisterInfo.cpp.
27880d0f6018b6928bdcad291be60c801238619955Bill Wendling
28b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris LattnerPPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
29641055225092833197efe8e5bce01d50bcf1daaeChris Lattner  : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
307ce45783531cfa81bfd7be561ea7e4738e8c6ca8Evan Cheng    RI(*TM.getSubtargetImpl(), *this) {}
31b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner
32b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner/// getPointerRegClass - Return the register class to use to hold pointers.
33b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner/// This is used for addressing modes.
34b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattnerconst TargetRegisterClass *PPCInstrInfo::getPointerRegClass() const {
35b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner  if (TM.getSubtargetImpl()->isPPC64())
36b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner    return &PPC::G8RCRegClass;
37b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner  else
38b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner    return &PPC::GPRCRegClass;
39b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner}
40b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner
41f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman
4221e463b2bf864671a87ebe386cb100ef9349a540Nate Begemanbool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
4321e463b2bf864671a87ebe386cb100ef9349a540Nate Begeman                               unsigned& sourceReg,
4421e463b2bf864671a87ebe386cb100ef9349a540Nate Begeman                               unsigned& destReg) const {
45cc8cd0cbf12c12916d4b38ef0de5be5501c8270eChris Lattner  unsigned oc = MI.getOpcode();
46b410dc99774d52b4491750dab10b91cca1d661d8Chris Lattner  if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
4714c09b81ead8fe8b754fca2d0a8237cb810b37d6Chris Lattner      oc == PPC::OR4To8 || oc == PPC::OR8To4) {                // or r1, r2, r2
481e341729dd003ca33ecea4abf13134f20062c5f8Evan Cheng    assert(MI.getNumOperands() >= 3 &&
49f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(0).isRegister() &&
50f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(1).isRegister() &&
51f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(2).isRegister() &&
52f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           "invalid PPC OR instruction!");
53f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
54f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      sourceReg = MI.getOperand(1).getReg();
55f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      destReg = MI.getOperand(0).getReg();
56f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      return true;
57f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    }
58f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman  } else if (oc == PPC::ADDI) {             // addi r1, r2, 0
591e341729dd003ca33ecea4abf13134f20062c5f8Evan Cheng    assert(MI.getNumOperands() >= 3 &&
60f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(0).isRegister() &&
61f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(2).isImmediate() &&
62f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           "invalid PPC ADDI instruction!");
639a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner    if (MI.getOperand(1).isRegister() && MI.getOperand(2).getImm() == 0) {
64f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      sourceReg = MI.getOperand(1).getReg();
65f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      destReg = MI.getOperand(0).getReg();
66f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      return true;
67f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    }
68cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman  } else if (oc == PPC::ORI) {             // ori r1, r2, 0
691e341729dd003ca33ecea4abf13134f20062c5f8Evan Cheng    assert(MI.getNumOperands() >= 3 &&
70cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman           MI.getOperand(0).isRegister() &&
71cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman           MI.getOperand(1).isRegister() &&
72cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman           MI.getOperand(2).isImmediate() &&
73cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman           "invalid PPC ORI instruction!");
749a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner    if (MI.getOperand(2).getImm() == 0) {
75cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman      sourceReg = MI.getOperand(1).getReg();
76cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman      destReg = MI.getOperand(0).getReg();
77cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman      return true;
78cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman    }
79eb5d47d99db0d9e4fc11f136fbacbd507c71a4c2Chris Lattner  } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
80eb5d47d99db0d9e4fc11f136fbacbd507c71a4c2Chris Lattner             oc == PPC::FMRSD) {      // fmr r1, r2
811e341729dd003ca33ecea4abf13134f20062c5f8Evan Cheng    assert(MI.getNumOperands() >= 2 &&
82f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(0).isRegister() &&
83f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(1).isRegister() &&
84f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           "invalid PPC FMR instruction");
85f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    sourceReg = MI.getOperand(1).getReg();
86f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    destReg = MI.getOperand(0).getReg();
87f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    return true;
887af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman  } else if (oc == PPC::MCRF) {             // mcrf cr1, cr2
891e341729dd003ca33ecea4abf13134f20062c5f8Evan Cheng    assert(MI.getNumOperands() >= 2 &&
907af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman           MI.getOperand(0).isRegister() &&
917af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman           MI.getOperand(1).isRegister() &&
927af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman           "invalid PPC MCRF instruction");
937af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman    sourceReg = MI.getOperand(1).getReg();
947af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman    destReg = MI.getOperand(0).getReg();
957af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman    return true;
96f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman  }
97f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman  return false;
98f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman}
99043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
100408396014742a05cad1c91949d2226169e3f9d80Chris Lattnerunsigned PPCInstrInfo::isLoadFromStackSlot(MachineInstr *MI,
1019c09c9ec9dab61450800b42cbf746164aa076b88Chris Lattner                                           int &FrameIndex) const {
102408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  switch (MI->getOpcode()) {
103408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  default: break;
104408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  case PPC::LD:
105408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  case PPC::LWZ:
106408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  case PPC::LFS:
107408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  case PPC::LFD:
1088aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner    if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
1098aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner        MI->getOperand(2).isFI()) {
1108aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner      FrameIndex = MI->getOperand(2).getIndex();
111408396014742a05cad1c91949d2226169e3f9d80Chris Lattner      return MI->getOperand(0).getReg();
112408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    }
113408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    break;
114408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  }
115408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  return 0;
1166524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner}
117408396014742a05cad1c91949d2226169e3f9d80Chris Lattner
1186524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattnerunsigned PPCInstrInfo::isStoreToStackSlot(MachineInstr *MI,
1196524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner                                          int &FrameIndex) const {
1206524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  switch (MI->getOpcode()) {
1216524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  default: break;
1223b478b31e297208ef2c9f74750a8a603eb3726fbNate Begeman  case PPC::STD:
1236524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  case PPC::STW:
1246524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  case PPC::STFS:
1256524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  case PPC::STFD:
1268aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner    if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
1278aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner        MI->getOperand(2).isFI()) {
1288aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner      FrameIndex = MI->getOperand(2).getIndex();
1296524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner      return MI->getOperand(0).getReg();
1306524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner    }
1316524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner    break;
1326524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  }
1336524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  return 0;
1346524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner}
135408396014742a05cad1c91949d2226169e3f9d80Chris Lattner
136043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner// commuteInstruction - We can commute rlwimi instructions, but only if the
137043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner// rotate amt is zero.  We also have to munge the immediates a bit.
13821e463b2bf864671a87ebe386cb100ef9349a540Nate BegemanMachineInstr *PPCInstrInfo::commuteInstruction(MachineInstr *MI) const {
139043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // Normal instructions can be commuted the obvious way.
140043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  if (MI->getOpcode() != PPC::RLWIMI)
141264e6fec9f3962cb269031d6d84cee9f896e0286Chris Lattner    return TargetInstrInfoImpl::commuteInstruction(MI);
142043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
143043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // Cannot commute if it has a non-zero rotate count.
1449a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner  if (MI->getOperand(3).getImm() != 0)
145043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner    return 0;
146043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
147043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // If we have a zero rotate count, we have:
148043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  //   M = mask(MB,ME)
149043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  //   Op0 = (Op1 & ~M) | (Op2 & M)
150043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // Change this to:
151043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  //   M = mask((ME+1)&31, (MB-1)&31)
152043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  //   Op0 = (Op2 & ~M) | (Op1 & M)
153043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
154043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // Swap op1/op2
155a4d16a1f0dcdd1ab2862737105f900e2c577532dEvan Cheng  unsigned Reg0 = MI->getOperand(0).getReg();
156043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  unsigned Reg1 = MI->getOperand(1).getReg();
157043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  unsigned Reg2 = MI->getOperand(2).getReg();
1586ce7dc2a97260eea5fba414332796464912b9359Evan Cheng  bool Reg1IsKill = MI->getOperand(1).isKill();
1596ce7dc2a97260eea5fba414332796464912b9359Evan Cheng  bool Reg2IsKill = MI->getOperand(2).isKill();
160a4d16a1f0dcdd1ab2862737105f900e2c577532dEvan Cheng  // If machine instrs are no longer in two-address forms, update
161a4d16a1f0dcdd1ab2862737105f900e2c577532dEvan Cheng  // destination register as well.
162a4d16a1f0dcdd1ab2862737105f900e2c577532dEvan Cheng  if (Reg0 == Reg1) {
163a4d16a1f0dcdd1ab2862737105f900e2c577532dEvan Cheng    // Must be two address instruction!
164a4d16a1f0dcdd1ab2862737105f900e2c577532dEvan Cheng    assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
165a4d16a1f0dcdd1ab2862737105f900e2c577532dEvan Cheng           "Expecting a two-address instruction!");
166a4d16a1f0dcdd1ab2862737105f900e2c577532dEvan Cheng    MI->getOperand(0).setReg(Reg2);
167a4d16a1f0dcdd1ab2862737105f900e2c577532dEvan Cheng    Reg2IsKill = false;
168a4d16a1f0dcdd1ab2862737105f900e2c577532dEvan Cheng  }
169e53f4a055f74bded20d6129b4724ddd17fd199f6Chris Lattner  MI->getOperand(2).setReg(Reg1);
170e53f4a055f74bded20d6129b4724ddd17fd199f6Chris Lattner  MI->getOperand(1).setReg(Reg2);
171f73823000e2d5d6e1cf65bdf5a107297e18d35fbChris Lattner  MI->getOperand(2).setIsKill(Reg1IsKill);
172f73823000e2d5d6e1cf65bdf5a107297e18d35fbChris Lattner  MI->getOperand(1).setIsKill(Reg2IsKill);
173043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
174043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // Swap the mask around.
1759a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner  unsigned MB = MI->getOperand(4).getImm();
1769a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner  unsigned ME = MI->getOperand(5).getImm();
1779a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner  MI->getOperand(4).setImm((ME+1) & 31);
1789a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner  MI->getOperand(5).setImm((MB-1) & 31);
179043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  return MI;
180043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner}
181bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattner
182bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattnervoid PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
183bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattner                              MachineBasicBlock::iterator MI) const {
184c0f64ffab93d11fb27a3b8a0707b77400918a20eEvan Cheng  BuildMI(MBB, MI, get(PPC::NOP));
185bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattner}
186c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
187c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
188c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner// Branch analysis.
189c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattnerbool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
190c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner                                 MachineBasicBlock *&FBB,
191c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner                                 std::vector<MachineOperand> &Cond) const {
192c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // If the block has no terminators, it just falls into the block after it.
193c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  MachineBasicBlock::iterator I = MBB.end();
194bfd2ec4a8ef51ebe982363a7e8d7156fdb3827d8Evan Cheng  if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
195c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    return false;
196c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
197c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // Get the last instruction in the block.
198c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  MachineInstr *LastInst = I;
199c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
200c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // If there is only one terminator instruction, process it.
201bfd2ec4a8ef51ebe982363a7e8d7156fdb3827d8Evan Cheng  if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
202c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    if (LastInst->getOpcode() == PPC::B) {
2038aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner      TBB = LastInst->getOperand(0).getMBB();
204c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      return false;
205289c2d5f4566d8d7722e3934f4763d3df92886f3Chris Lattner    } else if (LastInst->getOpcode() == PPC::BCC) {
206c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      // Block ends with fall-through condbranch.
2078aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner      TBB = LastInst->getOperand(2).getMBB();
208c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      Cond.push_back(LastInst->getOperand(0));
209c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      Cond.push_back(LastInst->getOperand(1));
2107c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner      return false;
211c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    }
212c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    // Otherwise, don't know what this is.
213c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    return true;
214c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  }
215c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
216c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // Get the instruction before it if it's a terminator.
217c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  MachineInstr *SecondLastInst = I;
218c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
219c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // If there are three terminators, we don't know what sort of block this is.
220c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  if (SecondLastInst && I != MBB.begin() &&
221bfd2ec4a8ef51ebe982363a7e8d7156fdb3827d8Evan Cheng      isUnpredicatedTerminator(--I))
222c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    return true;
223c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
224289c2d5f4566d8d7722e3934f4763d3df92886f3Chris Lattner  // If the block ends with PPC::B and PPC:BCC, handle it.
225289c2d5f4566d8d7722e3934f4763d3df92886f3Chris Lattner  if (SecondLastInst->getOpcode() == PPC::BCC &&
226c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      LastInst->getOpcode() == PPC::B) {
2278aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner    TBB =  SecondLastInst->getOperand(2).getMBB();
228c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    Cond.push_back(SecondLastInst->getOperand(0));
229c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    Cond.push_back(SecondLastInst->getOperand(1));
2308aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner    FBB = LastInst->getOperand(0).getMBB();
231c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    return false;
232c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  }
233c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
23413e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen  // If the block ends with two PPC:Bs, handle it.  The second one is not
23513e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen  // executed, so remove it.
23613e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen  if (SecondLastInst->getOpcode() == PPC::B &&
23713e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen      LastInst->getOpcode() == PPC::B) {
2388aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner    TBB = SecondLastInst->getOperand(0).getMBB();
23913e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen    I = LastInst;
24013e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen    I->eraseFromParent();
24113e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen    return false;
24213e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen  }
24313e8b51e3ec014c5d7ae83afdf3b8fd29c3a461dDale Johannesen
244c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // Otherwise, can't handle this.
245c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  return true;
246c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner}
247c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
248b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Chengunsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
249c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  MachineBasicBlock::iterator I = MBB.end();
250b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng  if (I == MBB.begin()) return 0;
251c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  --I;
252289c2d5f4566d8d7722e3934f4763d3df92886f3Chris Lattner  if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
253b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng    return 0;
254c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
255c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // Remove the branch.
256c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  I->eraseFromParent();
257c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
258c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  I = MBB.end();
259c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
260b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng  if (I == MBB.begin()) return 1;
261c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  --I;
262289c2d5f4566d8d7722e3934f4763d3df92886f3Chris Lattner  if (I->getOpcode() != PPC::BCC)
263b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng    return 1;
264c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
265c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // Remove the branch.
266c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  I->eraseFromParent();
267b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng  return 2;
268c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner}
269c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
270b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Chengunsigned
271b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan ChengPPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
272b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng                           MachineBasicBlock *FBB,
273b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng                           const std::vector<MachineOperand> &Cond) const {
2742dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner  // Shouldn't be a fall through.
2752dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner  assert(TBB && "InsertBranch must not be told to insert a fallthrough");
27654108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner  assert((Cond.size() == 2 || Cond.size() == 0) &&
27754108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner         "PPC branch conditions have two components!");
2782dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner
27954108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner  // One-way branch.
2802dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner  if (FBB == 0) {
28154108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner    if (Cond.empty())   // Unconditional branch
282c0f64ffab93d11fb27a3b8a0707b77400918a20eEvan Cheng      BuildMI(&MBB, get(PPC::B)).addMBB(TBB);
28354108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner    else                // Conditional branch
284c0f64ffab93d11fb27a3b8a0707b77400918a20eEvan Cheng      BuildMI(&MBB, get(PPC::BCC))
28518258c640466274c26e89016e361ec411ff78520Chris Lattner        .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
286b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng    return 1;
2872dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner  }
288c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
289879d09cf130f3760a08865913c04d9ff328fad5fChris Lattner  // Two-way Conditional Branch.
290c0f64ffab93d11fb27a3b8a0707b77400918a20eEvan Cheng  BuildMI(&MBB, get(PPC::BCC))
29118258c640466274c26e89016e361ec411ff78520Chris Lattner    .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
292c0f64ffab93d11fb27a3b8a0707b77400918a20eEvan Cheng  BuildMI(&MBB, get(PPC::B)).addMBB(FBB);
293b5cdaa257e167a08a8a54ea9249d847ccc415ce0Evan Cheng  return 2;
294c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner}
295c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
296d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Andersonvoid PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
297d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson                                   MachineBasicBlock::iterator MI,
298d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson                                   unsigned DestReg, unsigned SrcReg,
299d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson                                   const TargetRegisterClass *DestRC,
300d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson                                   const TargetRegisterClass *SrcRC) const {
301d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson  if (DestRC != SrcRC) {
302d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    cerr << "Not yet supported!";
303d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    abort();
304d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson  }
305d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson
306d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson  if (DestRC == PPC::GPRCRegisterClass) {
307d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    BuildMI(MBB, MI, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
308d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson  } else if (DestRC == PPC::G8RCRegisterClass) {
309d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    BuildMI(MBB, MI, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
310d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson  } else if (DestRC == PPC::F4RCRegisterClass) {
311d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    BuildMI(MBB, MI, get(PPC::FMRS), DestReg).addReg(SrcReg);
312d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson  } else if (DestRC == PPC::F8RCRegisterClass) {
313d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    BuildMI(MBB, MI, get(PPC::FMRD), DestReg).addReg(SrcReg);
314d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson  } else if (DestRC == PPC::CRRCRegisterClass) {
315d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    BuildMI(MBB, MI, get(PPC::MCRF), DestReg).addReg(SrcReg);
316d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson  } else if (DestRC == PPC::VRRCRegisterClass) {
317d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    BuildMI(MBB, MI, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
3180404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray  } else if (DestRC == PPC::CRBITRCRegisterClass) {
3190404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray    BuildMI(MBB, MI, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
320d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson  } else {
321d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    cerr << "Attempt to copy register that is not GPR or FPR";
322d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson    abort();
323d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson  }
324d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson}
325d10fd9791c20fd8368fa0ce94b626b769c6c8ba0Owen Anderson
3264a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendlingbool
3274a66e9a57e679b4f3243bf2061daf53c70102030Bill WendlingPPCInstrInfo::StoreRegToStackSlot(unsigned SrcReg, bool isKill,
3284a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling                                  int FrameIdx,
3294a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling                                  const TargetRegisterClass *RC,
3304a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling                                  SmallVectorImpl<MachineInstr*> &NewMIs) const{
331f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  if (RC == PPC::GPRCRegisterClass) {
332f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    if (SrcReg != PPC::LR) {
3334a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(addFrameReference(BuildMI(get(PPC::STW))
3344a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling                                         .addReg(SrcReg, false, false, isKill),
3354a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling                                         FrameIdx));
336f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    } else {
337f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      // FIXME: this spills LR immediately to memory in one step.  To do this,
338f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      // we use R11, which we know cannot be used in the prolog/epilog.  This is
339f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      // a hack.
3404a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(BuildMI(get(PPC::MFLR), PPC::R11));
3414a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(addFrameReference(BuildMI(get(PPC::STW))
3424a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling                                         .addReg(PPC::R11, false, false, isKill),
3434a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling                                         FrameIdx));
344f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    }
345f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::G8RCRegisterClass) {
346f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    if (SrcReg != PPC::LR8) {
3474a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(addFrameReference(BuildMI(get(PPC::STD))
348cb341de0e238f80dabf3da7b4f2aad58de6914bdChris Lattner                              .addReg(SrcReg, false, false, isKill), FrameIdx));
349f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    } else {
350f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      // FIXME: this spills LR immediately to memory in one step.  To do this,
351f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      // we use R11, which we know cannot be used in the prolog/epilog.  This is
352f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      // a hack.
3534a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(BuildMI(get(PPC::MFLR8), PPC::X11));
3544a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(addFrameReference(BuildMI(get(PPC::STD))
355cb341de0e238f80dabf3da7b4f2aad58de6914bdChris Lattner                            .addReg(PPC::X11, false, false, isKill), FrameIdx));
356f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    }
357f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::F8RCRegisterClass) {
3584a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    NewMIs.push_back(addFrameReference(BuildMI(get(PPC::STFD))
359cb341de0e238f80dabf3da7b4f2aad58de6914bdChris Lattner                              .addReg(SrcReg, false, false, isKill), FrameIdx));
360f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::F4RCRegisterClass) {
3614a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    NewMIs.push_back(addFrameReference(BuildMI(get(PPC::STFS))
362cb341de0e238f80dabf3da7b4f2aad58de6914bdChris Lattner                              .addReg(SrcReg, false, false, isKill), FrameIdx));
363f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::CRRCRegisterClass) {
3644a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
3654a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling        (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
3664a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      // FIXME (64-bit): Enable
3674a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(addFrameReference(BuildMI(get(PPC::SPILL_CR))
3687194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                         .addReg(SrcReg, false, false, isKill),
36971a2cb25ebc818383dd0f80475bc166f834e8d99Chris Lattner                                         FrameIdx));
3707194aaf738a1b89441635340403f1c5b06ae18efBill Wendling      return true;
3717194aaf738a1b89441635340403f1c5b06ae18efBill Wendling    } else {
3727194aaf738a1b89441635340403f1c5b06ae18efBill Wendling      // FIXME: We use R0 here, because it isn't available for RA.  We need to
3737194aaf738a1b89441635340403f1c5b06ae18efBill Wendling      // store the CR in the low 4-bits of the saved value.  First, issue a MFCR
3747194aaf738a1b89441635340403f1c5b06ae18efBill Wendling      // to save all of the CRBits.
3754a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(BuildMI(get(PPC::MFCR), PPC::R0));
376f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
3777194aaf738a1b89441635340403f1c5b06ae18efBill Wendling      // If the saved register wasn't CR0, shift the bits left so that they are
3787194aaf738a1b89441635340403f1c5b06ae18efBill Wendling      // in CR0's slot.
3797194aaf738a1b89441635340403f1c5b06ae18efBill Wendling      if (SrcReg != PPC::CR0) {
3807194aaf738a1b89441635340403f1c5b06ae18efBill Wendling        unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
3817194aaf738a1b89441635340403f1c5b06ae18efBill Wendling        // rlwinm r0, r0, ShiftBits, 0, 31.
3824a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling        NewMIs.push_back(BuildMI(get(PPC::RLWINM), PPC::R0)
383cb341de0e238f80dabf3da7b4f2aad58de6914bdChris Lattner                       .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
3847194aaf738a1b89441635340403f1c5b06ae18efBill Wendling      }
385f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
3864a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(addFrameReference(BuildMI(get(PPC::STW))
3877194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                         .addReg(PPC::R0, false, false, isKill),
3887194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                         FrameIdx));
3897194aaf738a1b89441635340403f1c5b06ae18efBill Wendling    }
3900404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray  } else if (RC == PPC::CRBITRCRegisterClass) {
3910404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray    // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
3920404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray    // backend currently only uses CR1EQ as an individual bit, this should
3930404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray    // not cause any bug. If we need other uses of CR bits, the following
3940404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray    // code may be invalid.
3959348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    unsigned Reg = 0;
3960404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray    if (SrcReg >= PPC::CR0LT || SrcReg <= PPC::CR0UN)
3979348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR0;
3980404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray    else if (SrcReg >= PPC::CR1LT || SrcReg <= PPC::CR1UN)
3999348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR1;
4009348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (SrcReg >= PPC::CR2LT || SrcReg <= PPC::CR2UN)
4019348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR2;
4029348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (SrcReg >= PPC::CR3LT || SrcReg <= PPC::CR3UN)
4039348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR3;
4049348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (SrcReg >= PPC::CR4LT || SrcReg <= PPC::CR4UN)
4059348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR4;
4069348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (SrcReg >= PPC::CR5LT || SrcReg <= PPC::CR5UN)
4079348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR5;
4089348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (SrcReg >= PPC::CR6LT || SrcReg <= PPC::CR6UN)
4099348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR6;
4109348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (SrcReg >= PPC::CR7LT || SrcReg <= PPC::CR7UN)
4119348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR7;
4129348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray
4134a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    return StoreRegToStackSlot(Reg, isKill, FrameIdx,
4149348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray                               PPC::CRRCRegisterClass, NewMIs);
4159348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray
416f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::VRRCRegisterClass) {
417f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    // We don't have indexed addressing for vector loads.  Emit:
418f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    // R0 = ADDI FI#
419f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    // STVX VAL, 0, R0
420f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    //
421f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    // FIXME: We use R0 here, because it isn't available for RA.
4224a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    NewMIs.push_back(addFrameReference(BuildMI(get(PPC::ADDI), PPC::R0),
423f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson                                       FrameIdx, 0, 0));
4244a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    NewMIs.push_back(BuildMI(get(PPC::STVX))
425cb341de0e238f80dabf3da7b4f2aad58de6914bdChris Lattner         .addReg(SrcReg, false, false, isKill).addReg(PPC::R0).addReg(PPC::R0));
426f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else {
427f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    assert(0 && "Unknown regclass!");
428f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    abort();
429f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  }
4307194aaf738a1b89441635340403f1c5b06ae18efBill Wendling
4317194aaf738a1b89441635340403f1c5b06ae18efBill Wendling  return false;
432f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson}
433f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
434f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Andersonvoid
435f6372aa1cc568df19da7c5023e83c75aa9404a07Owen AndersonPPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
4367194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                  MachineBasicBlock::iterator MI,
4377194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                  unsigned SrcReg, bool isKill, int FrameIdx,
4387194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                  const TargetRegisterClass *RC) const {
439f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  SmallVector<MachineInstr*, 4> NewMIs;
4407194aaf738a1b89441635340403f1c5b06ae18efBill Wendling
4414a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling  if (StoreRegToStackSlot(SrcReg, isKill, FrameIdx, RC, NewMIs)) {
4427194aaf738a1b89441635340403f1c5b06ae18efBill Wendling    PPCFunctionInfo *FuncInfo = MBB.getParent()->getInfo<PPCFunctionInfo>();
4437194aaf738a1b89441635340403f1c5b06ae18efBill Wendling    FuncInfo->setSpillsCR();
4447194aaf738a1b89441635340403f1c5b06ae18efBill Wendling  }
4457194aaf738a1b89441635340403f1c5b06ae18efBill Wendling
446f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
447f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    MBB.insert(MI, NewMIs[i]);
448f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson}
449f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
450f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Andersonvoid PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
4517194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                  bool isKill,
4527194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                  SmallVectorImpl<MachineOperand> &Addr,
4537194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                  const TargetRegisterClass *RC,
4547194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                  SmallVectorImpl<MachineInstr*> &NewMIs) const{
455f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  if (Addr[0].isFrameIndex()) {
4564a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    if (StoreRegToStackSlot(SrcReg, isKill, Addr[0].getIndex(), RC, NewMIs)) {
4577194aaf738a1b89441635340403f1c5b06ae18efBill Wendling      PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
4587194aaf738a1b89441635340403f1c5b06ae18efBill Wendling      FuncInfo->setSpillsCR();
4597194aaf738a1b89441635340403f1c5b06ae18efBill Wendling    }
4607194aaf738a1b89441635340403f1c5b06ae18efBill Wendling
461f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    return;
462f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  }
463f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
464f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  unsigned Opc = 0;
465f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  if (RC == PPC::GPRCRegisterClass) {
466f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    Opc = PPC::STW;
467f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::G8RCRegisterClass) {
468f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    Opc = PPC::STD;
469f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::F8RCRegisterClass) {
470f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    Opc = PPC::STFD;
471f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::F4RCRegisterClass) {
472f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    Opc = PPC::STFS;
473f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::VRRCRegisterClass) {
474f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    Opc = PPC::STVX;
475f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else {
476f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    assert(0 && "Unknown regclass!");
477f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    abort();
478f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  }
479f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  MachineInstrBuilder MIB = BuildMI(get(Opc))
480f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    .addReg(SrcReg, false, false, isKill);
481f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
482f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    MachineOperand &MO = Addr[i];
483f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    if (MO.isRegister())
484f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      MIB.addReg(MO.getReg());
485f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    else if (MO.isImmediate())
486f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      MIB.addImm(MO.getImm());
487f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    else
488f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      MIB.addFrameIndex(MO.getIndex());
489f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  }
490f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  NewMIs.push_back(MIB);
491f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  return;
492f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson}
493f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
4944a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendlingvoid
4954a66e9a57e679b4f3243bf2061daf53c70102030Bill WendlingPPCInstrInfo::LoadRegFromStackSlot(unsigned DestReg, int FrameIdx,
4964a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling                                   const TargetRegisterClass *RC,
4974a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling                                   SmallVectorImpl<MachineInstr*> &NewMIs)const{
498f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  if (RC == PPC::GPRCRegisterClass) {
499f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    if (DestReg != PPC::LR) {
5004a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(addFrameReference(BuildMI(get(PPC::LWZ), DestReg),
501f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson                                         FrameIdx));
502f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    } else {
5034a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(addFrameReference(BuildMI(get(PPC::LWZ), PPC::R11),
504f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson                                         FrameIdx));
5054a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(BuildMI(get(PPC::MTLR)).addReg(PPC::R11));
506f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    }
507f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::G8RCRegisterClass) {
508f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    if (DestReg != PPC::LR8) {
5094a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(addFrameReference(BuildMI(get(PPC::LD), DestReg),
510f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson                                         FrameIdx));
511f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    } else {
5124a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(addFrameReference(BuildMI(get(PPC::LD), PPC::R11),
513f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson                                         FrameIdx));
5144a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(BuildMI(get(PPC::MTLR8)).addReg(PPC::R11));
515f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    }
516f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::F8RCRegisterClass) {
5174a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    NewMIs.push_back(addFrameReference(BuildMI(get(PPC::LFD), DestReg),
518f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson                                       FrameIdx));
519f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::F4RCRegisterClass) {
5204a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    NewMIs.push_back(addFrameReference(BuildMI(get(PPC::LFS), DestReg),
521f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson                                       FrameIdx));
522f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::CRRCRegisterClass) {
523f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    // FIXME: We use R0 here, because it isn't available for RA.
5244a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    NewMIs.push_back(addFrameReference(BuildMI(get(PPC::LWZ), PPC::R0),
525f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson                                       FrameIdx));
526f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
527f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    // If the reloaded register isn't CR0, shift the bits right so that they are
528f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    // in the right CR's slot.
529f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    if (DestReg != PPC::CR0) {
530f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
531f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      // rlwinm r11, r11, 32-ShiftBits, 0, 31.
5324a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling      NewMIs.push_back(BuildMI(get(PPC::RLWINM), PPC::R0)
533f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson                    .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
534f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    }
535f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
5364a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    NewMIs.push_back(BuildMI(get(PPC::MTCRF), DestReg).addReg(PPC::R0));
5370404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray  } else if (RC == PPC::CRBITRCRegisterClass) {
5389348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray
5399348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    unsigned Reg = 0;
5400404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray    if (DestReg >= PPC::CR0LT || DestReg <= PPC::CR0UN)
5419348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR0;
5420404cd97e4f6ebfe4f8057d4e21119d77654dff2Nicolas Geoffray    else if (DestReg >= PPC::CR1LT || DestReg <= PPC::CR1UN)
5439348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR1;
5449348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (DestReg >= PPC::CR2LT || DestReg <= PPC::CR2UN)
5459348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR2;
5469348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (DestReg >= PPC::CR3LT || DestReg <= PPC::CR3UN)
5479348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR3;
5489348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (DestReg >= PPC::CR4LT || DestReg <= PPC::CR4UN)
5499348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR4;
5509348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (DestReg >= PPC::CR5LT || DestReg <= PPC::CR5UN)
5519348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR5;
5529348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (DestReg >= PPC::CR6LT || DestReg <= PPC::CR6UN)
5539348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR6;
5549348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray    else if (DestReg >= PPC::CR7LT || DestReg <= PPC::CR7UN)
5559348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray      Reg = PPC::CR7;
5569348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray
5574a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    return LoadRegFromStackSlot(Reg, FrameIdx,
5589348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray                                PPC::CRRCRegisterClass, NewMIs);
5599348c69dcfe1aa1e7f92752a18222dcfbcd96214Nicolas Geoffray
560f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::VRRCRegisterClass) {
561f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    // We don't have indexed addressing for vector loads.  Emit:
562f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    // R0 = ADDI FI#
563f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    // Dest = LVX 0, R0
564f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    //
565f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    // FIXME: We use R0 here, because it isn't available for RA.
5664a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    NewMIs.push_back(addFrameReference(BuildMI(get(PPC::ADDI), PPC::R0),
567f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson                                       FrameIdx, 0, 0));
5684a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    NewMIs.push_back(BuildMI(get(PPC::LVX),DestReg).addReg(PPC::R0)
569f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson                     .addReg(PPC::R0));
570f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else {
571f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    assert(0 && "Unknown regclass!");
572f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    abort();
573f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  }
574f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson}
575f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
576f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Andersonvoid
577f6372aa1cc568df19da7c5023e83c75aa9404a07Owen AndersonPPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
5787194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                   MachineBasicBlock::iterator MI,
5797194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                   unsigned DestReg, int FrameIdx,
5807194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                   const TargetRegisterClass *RC) const {
581f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  SmallVector<MachineInstr*, 4> NewMIs;
5824a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling  LoadRegFromStackSlot(DestReg, FrameIdx, RC, NewMIs);
583f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
584f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    MBB.insert(MI, NewMIs[i]);
585f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson}
586f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
587f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Andersonvoid PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
5887194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                   SmallVectorImpl<MachineOperand> &Addr,
5897194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                   const TargetRegisterClass *RC,
5907194aaf738a1b89441635340403f1c5b06ae18efBill Wendling                                   SmallVectorImpl<MachineInstr*> &NewMIs)const{
591f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  if (Addr[0].isFrameIndex()) {
5924a66e9a57e679b4f3243bf2061daf53c70102030Bill Wendling    LoadRegFromStackSlot(DestReg, Addr[0].getIndex(), RC, NewMIs);
593f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    return;
594f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  }
595f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
596f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  unsigned Opc = 0;
597f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  if (RC == PPC::GPRCRegisterClass) {
598f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    assert(DestReg != PPC::LR && "Can't handle this yet!");
599f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    Opc = PPC::LWZ;
600f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::G8RCRegisterClass) {
601f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    assert(DestReg != PPC::LR8 && "Can't handle this yet!");
602f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    Opc = PPC::LD;
603f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::F8RCRegisterClass) {
604f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    Opc = PPC::LFD;
605f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::F4RCRegisterClass) {
606f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    Opc = PPC::LFS;
607f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else if (RC == PPC::VRRCRegisterClass) {
608f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    Opc = PPC::LVX;
609f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  } else {
610f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    assert(0 && "Unknown regclass!");
611f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    abort();
612f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  }
613f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  MachineInstrBuilder MIB = BuildMI(get(Opc), DestReg);
614f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
615f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    MachineOperand &MO = Addr[i];
616f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    if (MO.isRegister())
617f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      MIB.addReg(MO.getReg());
618f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    else if (MO.isImmediate())
619f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      MIB.addImm(MO.getImm());
620f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson    else
621f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson      MIB.addFrameIndex(MO.getIndex());
622f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  }
623f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  NewMIs.push_back(MIB);
624f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson  return;
625f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson}
626f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
62743dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
62843dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson/// copy instructions, turning them into load/store instructions.
6295fd79d0560570fed977788a86fa038b898564dfaEvan ChengMachineInstr *PPCInstrInfo::foldMemoryOperand(MachineFunction &MF,
6305fd79d0560570fed977788a86fa038b898564dfaEvan Cheng                                              MachineInstr *MI,
63143dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson                                              SmallVectorImpl<unsigned> &Ops,
63243dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson                                              int FrameIndex) const {
63343dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  if (Ops.size() != 1) return NULL;
63443dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson
63543dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  // Make sure this is a reg-reg copy.  Note that we can't handle MCRF, because
63643dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  // it takes more than one instruction to store it.
63743dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  unsigned Opc = MI->getOpcode();
63843dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  unsigned OpNum = Ops[0];
63943dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson
64043dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  MachineInstr *NewMI = NULL;
64143dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  if ((Opc == PPC::OR &&
64243dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson       MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
64343dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    if (OpNum == 0) {  // move -> store
64443dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      unsigned InReg = MI->getOperand(1).getReg();
64543dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      NewMI = addFrameReference(BuildMI(get(PPC::STW)).addReg(InReg),
64643dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson                                FrameIndex);
64743dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    } else {           // move -> load
64843dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      unsigned OutReg = MI->getOperand(0).getReg();
64943dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      NewMI = addFrameReference(BuildMI(get(PPC::LWZ), OutReg),
65043dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson                                FrameIndex);
65143dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    }
65243dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  } else if ((Opc == PPC::OR8 &&
65343dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson              MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
65443dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    if (OpNum == 0) {  // move -> store
65543dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      unsigned InReg = MI->getOperand(1).getReg();
65643dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      NewMI = addFrameReference(BuildMI(get(PPC::STD)).addReg(InReg),
65743dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson                                FrameIndex);
65843dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    } else {           // move -> load
65943dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      unsigned OutReg = MI->getOperand(0).getReg();
66043dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      NewMI = addFrameReference(BuildMI(get(PPC::LD), OutReg), FrameIndex);
66143dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    }
66243dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  } else if (Opc == PPC::FMRD) {
66343dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    if (OpNum == 0) {  // move -> store
66443dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      unsigned InReg = MI->getOperand(1).getReg();
66543dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      NewMI = addFrameReference(BuildMI(get(PPC::STFD)).addReg(InReg),
66643dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson                                FrameIndex);
66743dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    } else {           // move -> load
66843dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      unsigned OutReg = MI->getOperand(0).getReg();
66943dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      NewMI = addFrameReference(BuildMI(get(PPC::LFD), OutReg), FrameIndex);
67043dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    }
67143dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  } else if (Opc == PPC::FMRS) {
67243dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    if (OpNum == 0) {  // move -> store
67343dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      unsigned InReg = MI->getOperand(1).getReg();
67443dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      NewMI = addFrameReference(BuildMI(get(PPC::STFS)).addReg(InReg),
67543dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson                                FrameIndex);
67643dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    } else {           // move -> load
67743dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      unsigned OutReg = MI->getOperand(0).getReg();
67843dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson      NewMI = addFrameReference(BuildMI(get(PPC::LFS), OutReg), FrameIndex);
67943dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    }
68043dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  }
68143dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson
68243dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  if (NewMI)
68343dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    NewMI->copyKillDeadInfo(MI);
68443dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  return NewMI;
68543dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson}
68643dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson
68743dbe05279b753aabda571d9c83eaeb36987001aOwen Andersonbool PPCInstrInfo::canFoldMemoryOperand(MachineInstr *MI,
6885fd79d0560570fed977788a86fa038b898564dfaEvan Cheng                                        SmallVectorImpl<unsigned> &Ops) const {
68943dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  if (Ops.size() != 1) return false;
69043dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson
69143dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  // Make sure this is a reg-reg copy.  Note that we can't handle MCRF, because
69243dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  // it takes more than one instruction to store it.
69343dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  unsigned Opc = MI->getOpcode();
69443dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson
69543dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  if ((Opc == PPC::OR &&
69643dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson       MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
69743dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    return true;
69843dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  else if ((Opc == PPC::OR8 &&
69943dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson              MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
70043dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    return true;
70143dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
70243dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson    return true;
70343dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson
70443dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson  return false;
70543dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson}
70643dbe05279b753aabda571d9c83eaeb36987001aOwen Anderson
707f6372aa1cc568df19da7c5023e83c75aa9404a07Owen Anderson
708ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattnerbool PPCInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
709ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  if (MBB.empty()) return false;
710ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner
711ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  switch (MBB.back().getOpcode()) {
712126f17a17625876adb63f06d043fc1b1e4f0361cEvan Cheng  case PPC::BLR:   // Return.
713ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  case PPC::B:     // Uncond branch.
714ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  case PPC::BCTR:  // Indirect branch.
715ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner    return true;
716ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  default: return false;
717ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  }
718ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner}
719ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner
720c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattnerbool PPCInstrInfo::
721c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris LattnerReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
7227c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner  assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
7237c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner  // Leave the CR# the same, but invert the condition.
72418258c640466274c26e89016e361ec411ff78520Chris Lattner  Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
7257c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner  return false;
726c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner}
727