PPCInstrInfo.cpp revision df4ed6350b2a51f71c0980e86c9078f4046ea706
121e463b2bf864671a87ebe386cb100ef9349a540Nate Begeman//===- PPCInstrInfo.cpp - PowerPC32 Instruction Information -----*- C++ -*-===//
2b5f662fa0314f7e7e690aae8ebff7136cc3a5ab0Misha Brukman//
3f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//                     The LLVM Compiler Infrastructure
4f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman//
5f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman// This file was developed by the LLVM research group and is distributed under
6f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman// the University of Illinois Open Source 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"
15df4ed6350b2a51f71c0980e86c9078f4046ea706Chris Lattner#include "PPCPredicates.h"
164c7b43b43fdf943c7298718e15ab5d6dfe345be7Chris Lattner#include "PPCGenInstrInfo.inc"
17b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner#include "PPCTargetMachine.h"
18f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman#include "llvm/CodeGen/MachineInstrBuilder.h"
19f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman#include <iostream>
20f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukmanusing namespace llvm;
21f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman
22b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris LattnerPPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
23804e06704261f233111913a047ef7f7dec1b8725Chris Lattner  : TargetInstrInfo(PPCInsts, sizeof(PPCInsts)/sizeof(PPCInsts[0])), TM(tm),
247ce45783531cfa81bfd7be561ea7e4738e8c6ca8Evan Cheng    RI(*TM.getSubtargetImpl(), *this) {}
25b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner
26b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner/// getPointerRegClass - Return the register class to use to hold pointers.
27b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner/// This is used for addressing modes.
28b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattnerconst TargetRegisterClass *PPCInstrInfo::getPointerRegClass() const {
29b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner  if (TM.getSubtargetImpl()->isPPC64())
30b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner    return &PPC::G8RCRegClass;
31b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner  else
32b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner    return &PPC::GPRCRegClass;
33b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner}
34b1d26f66658cff3ceb7d44a72fbc8c8e975532f9Chris Lattner
35f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman
3621e463b2bf864671a87ebe386cb100ef9349a540Nate Begemanbool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
3721e463b2bf864671a87ebe386cb100ef9349a540Nate Begeman                               unsigned& sourceReg,
3821e463b2bf864671a87ebe386cb100ef9349a540Nate Begeman                               unsigned& destReg) const {
39f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman  MachineOpCode oc = MI.getOpcode();
40b410dc99774d52b4491750dab10b91cca1d661d8Chris Lattner  if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
4114c09b81ead8fe8b754fca2d0a8237cb810b37d6Chris Lattner      oc == PPC::OR4To8 || oc == PPC::OR8To4) {                // or r1, r2, r2
42f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    assert(MI.getNumOperands() == 3 &&
43f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(0).isRegister() &&
44f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(1).isRegister() &&
45f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(2).isRegister() &&
46f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           "invalid PPC OR instruction!");
47f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
48f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      sourceReg = MI.getOperand(1).getReg();
49f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      destReg = MI.getOperand(0).getReg();
50f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      return true;
51f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    }
52f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman  } else if (oc == PPC::ADDI) {             // addi r1, r2, 0
53f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    assert(MI.getNumOperands() == 3 &&
54f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(0).isRegister() &&
55f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(2).isImmediate() &&
56f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           "invalid PPC ADDI instruction!");
57f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    if (MI.getOperand(1).isRegister() && MI.getOperand(2).getImmedValue()==0) {
58f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      sourceReg = MI.getOperand(1).getReg();
59f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      destReg = MI.getOperand(0).getReg();
60f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman      return true;
61f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    }
62cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman  } else if (oc == PPC::ORI) {             // ori r1, r2, 0
63cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman    assert(MI.getNumOperands() == 3 &&
64cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman           MI.getOperand(0).isRegister() &&
65cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman           MI.getOperand(1).isRegister() &&
66cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman           MI.getOperand(2).isImmediate() &&
67cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman           "invalid PPC ORI instruction!");
68cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman    if (MI.getOperand(2).getImmedValue()==0) {
69cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman      sourceReg = MI.getOperand(1).getReg();
70cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman      destReg = MI.getOperand(0).getReg();
71cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman      return true;
72cb90de37a720b0b00d6303b49b8df6d5ac5f34f9Nate Begeman    }
73eb5d47d99db0d9e4fc11f136fbacbd507c71a4c2Chris Lattner  } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
74eb5d47d99db0d9e4fc11f136fbacbd507c71a4c2Chris Lattner             oc == PPC::FMRSD) {      // fmr r1, r2
75f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    assert(MI.getNumOperands() == 2 &&
76f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(0).isRegister() &&
77f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           MI.getOperand(1).isRegister() &&
78f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman           "invalid PPC FMR instruction");
79f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    sourceReg = MI.getOperand(1).getReg();
80f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    destReg = MI.getOperand(0).getReg();
81f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman    return true;
827af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman  } else if (oc == PPC::MCRF) {             // mcrf cr1, cr2
837af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman    assert(MI.getNumOperands() == 2 &&
847af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman           MI.getOperand(0).isRegister() &&
857af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman           MI.getOperand(1).isRegister() &&
867af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman           "invalid PPC MCRF instruction");
877af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman    sourceReg = MI.getOperand(1).getReg();
887af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman    destReg = MI.getOperand(0).getReg();
897af0248af47fbd86ec65d308adda22ec367accc4Nate Begeman    return true;
90f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman  }
91f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman  return false;
92f2ccb77ee9d8ab35866dae111fa36929689c7511Misha Brukman}
93043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
94408396014742a05cad1c91949d2226169e3f9d80Chris Lattnerunsigned PPCInstrInfo::isLoadFromStackSlot(MachineInstr *MI,
959c09c9ec9dab61450800b42cbf746164aa076b88Chris Lattner                                           int &FrameIndex) const {
96408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  switch (MI->getOpcode()) {
97408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  default: break;
98408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  case PPC::LD:
99408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  case PPC::LWZ:
100408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  case PPC::LFS:
101408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  case PPC::LFD:
102408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    if (MI->getOperand(1).isImmediate() && !MI->getOperand(1).getImmedValue() &&
103408396014742a05cad1c91949d2226169e3f9d80Chris Lattner        MI->getOperand(2).isFrameIndex()) {
104408396014742a05cad1c91949d2226169e3f9d80Chris Lattner      FrameIndex = MI->getOperand(2).getFrameIndex();
105408396014742a05cad1c91949d2226169e3f9d80Chris Lattner      return MI->getOperand(0).getReg();
106408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    }
107408396014742a05cad1c91949d2226169e3f9d80Chris Lattner    break;
108408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  }
109408396014742a05cad1c91949d2226169e3f9d80Chris Lattner  return 0;
1106524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner}
111408396014742a05cad1c91949d2226169e3f9d80Chris Lattner
1126524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattnerunsigned PPCInstrInfo::isStoreToStackSlot(MachineInstr *MI,
1136524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner                                          int &FrameIndex) const {
1146524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  switch (MI->getOpcode()) {
1156524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  default: break;
1163b478b31e297208ef2c9f74750a8a603eb3726fbNate Begeman  case PPC::STD:
1176524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  case PPC::STW:
1186524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  case PPC::STFS:
1196524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  case PPC::STFD:
1206524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner    if (MI->getOperand(1).isImmediate() && !MI->getOperand(1).getImmedValue() &&
1216524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner        MI->getOperand(2).isFrameIndex()) {
1226524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner      FrameIndex = MI->getOperand(2).getFrameIndex();
1236524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner      return MI->getOperand(0).getReg();
1246524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner    }
1256524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner    break;
1266524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  }
1276524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner  return 0;
1286524287c53cf727a8ef33517403fcb1bbd7adff9Chris Lattner}
129408396014742a05cad1c91949d2226169e3f9d80Chris Lattner
130043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner// commuteInstruction - We can commute rlwimi instructions, but only if the
131043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner// rotate amt is zero.  We also have to munge the immediates a bit.
13221e463b2bf864671a87ebe386cb100ef9349a540Nate BegemanMachineInstr *PPCInstrInfo::commuteInstruction(MachineInstr *MI) const {
133043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // Normal instructions can be commuted the obvious way.
134043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  if (MI->getOpcode() != PPC::RLWIMI)
135043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner    return TargetInstrInfo::commuteInstruction(MI);
136043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
137043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // Cannot commute if it has a non-zero rotate count.
138043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  if (MI->getOperand(3).getImmedValue() != 0)
139043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner    return 0;
140043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
141043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // If we have a zero rotate count, we have:
142043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  //   M = mask(MB,ME)
143043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  //   Op0 = (Op1 & ~M) | (Op2 & M)
144043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // Change this to:
145043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  //   M = mask((ME+1)&31, (MB-1)&31)
146043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  //   Op0 = (Op2 & ~M) | (Op1 & M)
147043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
148043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // Swap op1/op2
149043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  unsigned Reg1 = MI->getOperand(1).getReg();
150043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  unsigned Reg2 = MI->getOperand(2).getReg();
1516ce7dc2a97260eea5fba414332796464912b9359Evan Cheng  bool Reg1IsKill = MI->getOperand(1).isKill();
1526ce7dc2a97260eea5fba414332796464912b9359Evan Cheng  bool Reg2IsKill = MI->getOperand(2).isKill();
153e53f4a055f74bded20d6129b4724ddd17fd199f6Chris Lattner  MI->getOperand(2).setReg(Reg1);
154e53f4a055f74bded20d6129b4724ddd17fd199f6Chris Lattner  MI->getOperand(1).setReg(Reg2);
1556ce7dc2a97260eea5fba414332796464912b9359Evan Cheng  if (Reg1IsKill)
1566ce7dc2a97260eea5fba414332796464912b9359Evan Cheng    MI->getOperand(2).setIsKill();
1576ce7dc2a97260eea5fba414332796464912b9359Evan Cheng  else
1586ce7dc2a97260eea5fba414332796464912b9359Evan Cheng    MI->getOperand(2).unsetIsKill();
1596ce7dc2a97260eea5fba414332796464912b9359Evan Cheng  if (Reg2IsKill)
1606ce7dc2a97260eea5fba414332796464912b9359Evan Cheng    MI->getOperand(1).setIsKill();
1616ce7dc2a97260eea5fba414332796464912b9359Evan Cheng  else
1626ce7dc2a97260eea5fba414332796464912b9359Evan Cheng    MI->getOperand(1).unsetIsKill();
163043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner
164043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  // Swap the mask around.
165043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  unsigned MB = MI->getOperand(4).getImmedValue();
166043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  unsigned ME = MI->getOperand(5).getImmedValue();
167043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  MI->getOperand(4).setImmedValue((ME+1) & 31);
168043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  MI->getOperand(5).setImmedValue((MB-1) & 31);
169043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner  return MI;
170043870dd85ea41e8972c304b122070a417c8a4bcChris Lattner}
171bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattner
172bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattnervoid PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
173bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattner                              MachineBasicBlock::iterator MI) const {
174bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattner  BuildMI(MBB, MI, PPC::NOP, 0);
175bbf1c72d51a77bf54c9c684b90a78e59f0b70b2fChris Lattner}
176c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
177c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
178c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner// Branch analysis.
179c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattnerbool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
180c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner                                 MachineBasicBlock *&FBB,
181c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner                                 std::vector<MachineOperand> &Cond) const {
182c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // If the block has no terminators, it just falls into the block after it.
183c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  MachineBasicBlock::iterator I = MBB.end();
184c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
185c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    return false;
186c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
187c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // Get the last instruction in the block.
188c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  MachineInstr *LastInst = I;
189c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
190c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // If there is only one terminator instruction, process it.
191c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
192c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    if (LastInst->getOpcode() == PPC::B) {
193c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      TBB = LastInst->getOperand(0).getMachineBasicBlock();
194c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      return false;
195c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    } else if (LastInst->getOpcode() == PPC::COND_BRANCH) {
196c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      // Block ends with fall-through condbranch.
197c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      TBB = LastInst->getOperand(2).getMachineBasicBlock();
198c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      Cond.push_back(LastInst->getOperand(0));
199c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      Cond.push_back(LastInst->getOperand(1));
2007c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner      return false;
201c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    }
202c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    // Otherwise, don't know what this is.
203c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    return true;
204c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  }
205c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
206c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // Get the instruction before it if it's a terminator.
207c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  MachineInstr *SecondLastInst = I;
208c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
209c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // If there are three terminators, we don't know what sort of block this is.
210c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  if (SecondLastInst && I != MBB.begin() &&
211c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      isTerminatorInstr((--I)->getOpcode()))
212c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    return true;
213c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
214c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // If the block ends with PPC::B and PPC:COND_BRANCH, handle it.
215c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  if (SecondLastInst->getOpcode() == PPC::COND_BRANCH &&
216c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner      LastInst->getOpcode() == PPC::B) {
217c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    TBB =  SecondLastInst->getOperand(2).getMachineBasicBlock();
218c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    Cond.push_back(SecondLastInst->getOperand(0));
219c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    Cond.push_back(SecondLastInst->getOperand(1));
220c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    FBB = LastInst->getOperand(0).getMachineBasicBlock();
221c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    return false;
222c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  }
223c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
224c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // Otherwise, can't handle this.
225c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  return true;
226c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner}
227c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
228c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattnervoid PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
229c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  MachineBasicBlock::iterator I = MBB.end();
230c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  if (I == MBB.begin()) return;
231c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  --I;
232c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::COND_BRANCH)
233c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    return;
234c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
235c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // Remove the branch.
236c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  I->eraseFromParent();
237c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
238c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  I = MBB.end();
239c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
240c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  if (I == MBB.begin()) return;
241c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  --I;
242c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  if (I->getOpcode() != PPC::COND_BRANCH)
243c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    return;
244c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
245c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  // Remove the branch.
246c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  I->eraseFromParent();
247c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner}
248c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
249c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattnervoid PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
250c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner                                MachineBasicBlock *FBB,
251c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner                                const std::vector<MachineOperand> &Cond) const {
2522dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner  // Shouldn't be a fall through.
2532dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner  assert(TBB && "InsertBranch must not be told to insert a fallthrough");
25454108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner  assert((Cond.size() == 2 || Cond.size() == 0) &&
25554108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner         "PPC branch conditions have two components!");
2562dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner
25754108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner  // One-way branch.
2582dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner  if (FBB == 0) {
25954108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner    if (Cond.empty())   // Unconditional branch
26054108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner      BuildMI(&MBB, PPC::B, 1).addMBB(TBB);
26154108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner    else                // Conditional branch
26254108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner      BuildMI(&MBB, PPC::COND_BRANCH, 3)
26354108068b71a7dbc48f4ebf1b2d7d87ca541070aChris Lattner        .addReg(Cond[0].getReg()).addImm(Cond[1].getImm()).addMBB(TBB);
2642dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner    return;
2652dc7723474c54efcbcac6265dad0a7271902f1a5Chris Lattner  }
266c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
267879d09cf130f3760a08865913c04d9ff328fad5fChris Lattner  // Two-way Conditional Branch.
268c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner  BuildMI(&MBB, PPC::COND_BRANCH, 3)
269c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner    .addReg(Cond[0].getReg()).addImm(Cond[1].getImm()).addMBB(TBB);
270879d09cf130f3760a08865913c04d9ff328fad5fChris Lattner  BuildMI(&MBB, PPC::B, 1).addMBB(FBB);
271c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner}
272c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner
273ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattnerbool PPCInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
274ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  if (MBB.empty()) return false;
275ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner
276ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  switch (MBB.back().getOpcode()) {
277ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  case PPC::B:     // Uncond branch.
278ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  case PPC::BCTR:  // Indirect branch.
279ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner    return true;
280ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  default: return false;
281ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner  }
282ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner}
283ef13982aa7f3e57e82cd48370e79033dff0da295Chris Lattner
284c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattnerbool PPCInstrInfo::
285c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris LattnerReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
2867c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner  assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
2877c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner  // Leave the CR# the same, but invert the condition.
288df4ed6350b2a51f71c0980e86c9078f4046ea706Chris Lattner  Cond[1].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[1].getImm()));
2897c4fe259f8bfeae542cfef25c1f1e9b1ff25a39bChris Lattner  return false;
290c50e2bcdf7bff1f9681ab80e52691f274950fab5Chris Lattner}
291