blob: 4bac10de88365fc00be865c6745facb48ef8d31e [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===- PPCInstrInfo.cpp - PowerPC32 Instruction Information -----*- C++ -*-===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukmanf2ccb772004-08-17 04:55:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukmanf2ccb772004-08-17 04:55:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains the PowerPC implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner16e71f22005-10-14 23:59:06 +000014#include "PPCInstrInfo.h"
Chris Lattnerdf4ed632006-11-17 22:10:59 +000015#include "PPCPredicates.h"
Chris Lattner4c7b43b2005-10-14 23:37:35 +000016#include "PPCGenInstrInfo.inc"
Chris Lattnerb1d26f62006-06-17 00:01:04 +000017#include "PPCTargetMachine.h"
Owen Anderson718cb662007-09-07 04:06:50 +000018#include "llvm/ADT/STLExtras.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000020using namespace llvm;
21
Chris Lattnerb1d26f62006-06-17 00:01:04 +000022PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Owen Anderson718cb662007-09-07 04:06:50 +000023 : TargetInstrInfo(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
Evan Cheng7ce45782006-11-13 23:36:35 +000024 RI(*TM.getSubtargetImpl(), *this) {}
Chris Lattnerb1d26f62006-06-17 00:01:04 +000025
26/// getPointerRegClass - Return the register class to use to hold pointers.
27/// This is used for addressing modes.
28const TargetRegisterClass *PPCInstrInfo::getPointerRegClass() const {
29 if (TM.getSubtargetImpl()->isPPC64())
30 return &PPC::G8RCRegClass;
31 else
32 return &PPC::GPRCRegClass;
33}
34
Misha Brukmanf2ccb772004-08-17 04:55:41 +000035
Nate Begeman21e463b2005-10-16 05:39:50 +000036bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
37 unsigned& sourceReg,
38 unsigned& destReg) const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +000039 MachineOpCode oc = MI.getOpcode();
Chris Lattnerb410dc92006-06-20 23:18:58 +000040 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
Chris Lattner14c09b82005-10-19 01:50:36 +000041 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000042 assert(MI.getNumOperands() >= 3 &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000043 MI.getOperand(0).isRegister() &&
44 MI.getOperand(1).isRegister() &&
45 MI.getOperand(2).isRegister() &&
46 "invalid PPC OR instruction!");
47 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
48 sourceReg = MI.getOperand(1).getReg();
49 destReg = MI.getOperand(0).getReg();
50 return true;
51 }
52 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000053 assert(MI.getNumOperands() >= 3 &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000054 MI.getOperand(0).isRegister() &&
55 MI.getOperand(2).isImmediate() &&
56 "invalid PPC ADDI instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +000057 if (MI.getOperand(1).isRegister() && MI.getOperand(2).getImm() == 0) {
Misha Brukmanf2ccb772004-08-17 04:55:41 +000058 sourceReg = MI.getOperand(1).getReg();
59 destReg = MI.getOperand(0).getReg();
60 return true;
61 }
Nate Begemancb90de32004-10-07 22:26:12 +000062 } else if (oc == PPC::ORI) { // ori r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000063 assert(MI.getNumOperands() >= 3 &&
Nate Begemancb90de32004-10-07 22:26:12 +000064 MI.getOperand(0).isRegister() &&
65 MI.getOperand(1).isRegister() &&
66 MI.getOperand(2).isImmediate() &&
67 "invalid PPC ORI instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +000068 if (MI.getOperand(2).getImm() == 0) {
Nate Begemancb90de32004-10-07 22:26:12 +000069 sourceReg = MI.getOperand(1).getReg();
70 destReg = MI.getOperand(0).getReg();
71 return true;
72 }
Chris Lattnereb5d47d2005-10-07 05:00:52 +000073 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
74 oc == PPC::FMRSD) { // fmr r1, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000075 assert(MI.getNumOperands() >= 2 &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000076 MI.getOperand(0).isRegister() &&
77 MI.getOperand(1).isRegister() &&
78 "invalid PPC FMR instruction");
79 sourceReg = MI.getOperand(1).getReg();
80 destReg = MI.getOperand(0).getReg();
81 return true;
Nate Begeman7af02482005-04-12 07:04:16 +000082 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
Evan Cheng1e3417292007-04-25 07:12:14 +000083 assert(MI.getNumOperands() >= 2 &&
Nate Begeman7af02482005-04-12 07:04:16 +000084 MI.getOperand(0).isRegister() &&
85 MI.getOperand(1).isRegister() &&
86 "invalid PPC MCRF instruction");
87 sourceReg = MI.getOperand(1).getReg();
88 destReg = MI.getOperand(0).getReg();
89 return true;
Misha Brukmanf2ccb772004-08-17 04:55:41 +000090 }
91 return false;
92}
Chris Lattner043870d2005-09-09 18:17:41 +000093
Chris Lattner40839602006-02-02 20:12:32 +000094unsigned PPCInstrInfo::isLoadFromStackSlot(MachineInstr *MI,
Chris Lattner9c09c9e2006-03-16 22:24:02 +000095 int &FrameIndex) const {
Chris Lattner40839602006-02-02 20:12:32 +000096 switch (MI->getOpcode()) {
97 default: break;
98 case PPC::LD:
99 case PPC::LWZ:
100 case PPC::LFS:
101 case PPC::LFD:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000102 if (MI->getOperand(1).isImmediate() && !MI->getOperand(1).getImm() &&
Chris Lattner40839602006-02-02 20:12:32 +0000103 MI->getOperand(2).isFrameIndex()) {
104 FrameIndex = MI->getOperand(2).getFrameIndex();
105 return MI->getOperand(0).getReg();
106 }
107 break;
108 }
109 return 0;
Chris Lattner65242872006-02-02 20:16:12 +0000110}
Chris Lattner40839602006-02-02 20:12:32 +0000111
Chris Lattner65242872006-02-02 20:16:12 +0000112unsigned PPCInstrInfo::isStoreToStackSlot(MachineInstr *MI,
113 int &FrameIndex) const {
114 switch (MI->getOpcode()) {
115 default: break;
Nate Begeman3b478b32006-02-02 21:07:50 +0000116 case PPC::STD:
Chris Lattner65242872006-02-02 20:16:12 +0000117 case PPC::STW:
118 case PPC::STFS:
119 case PPC::STFD:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000120 if (MI->getOperand(1).isImmediate() && !MI->getOperand(1).getImm() &&
Chris Lattner65242872006-02-02 20:16:12 +0000121 MI->getOperand(2).isFrameIndex()) {
122 FrameIndex = MI->getOperand(2).getFrameIndex();
123 return MI->getOperand(0).getReg();
124 }
125 break;
126 }
127 return 0;
128}
Chris Lattner40839602006-02-02 20:12:32 +0000129
Chris Lattner043870d2005-09-09 18:17:41 +0000130// commuteInstruction - We can commute rlwimi instructions, but only if the
131// rotate amt is zero. We also have to munge the immediates a bit.
Nate Begeman21e463b2005-10-16 05:39:50 +0000132MachineInstr *PPCInstrInfo::commuteInstruction(MachineInstr *MI) const {
Chris Lattner043870d2005-09-09 18:17:41 +0000133 // Normal instructions can be commuted the obvious way.
134 if (MI->getOpcode() != PPC::RLWIMI)
135 return TargetInstrInfo::commuteInstruction(MI);
136
137 // Cannot commute if it has a non-zero rotate count.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000138 if (MI->getOperand(3).getImm() != 0)
Chris Lattner043870d2005-09-09 18:17:41 +0000139 return 0;
140
141 // If we have a zero rotate count, we have:
142 // M = mask(MB,ME)
143 // Op0 = (Op1 & ~M) | (Op2 & M)
144 // Change this to:
145 // M = mask((ME+1)&31, (MB-1)&31)
146 // Op0 = (Op2 & ~M) | (Op1 & M)
147
148 // Swap op1/op2
149 unsigned Reg1 = MI->getOperand(1).getReg();
150 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000151 bool Reg1IsKill = MI->getOperand(1).isKill();
152 bool Reg2IsKill = MI->getOperand(2).isKill();
Chris Lattnere53f4a02006-05-04 17:52:23 +0000153 MI->getOperand(2).setReg(Reg1);
154 MI->getOperand(1).setReg(Reg2);
Chris Lattnerf7382302007-12-30 21:56:09 +0000155 MI->getOperand(2).setIsKill(Reg1IsKill);
156 MI->getOperand(1).setIsKill(Reg2IsKill);
Chris Lattner043870d2005-09-09 18:17:41 +0000157
158 // Swap the mask around.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000159 unsigned MB = MI->getOperand(4).getImm();
160 unsigned ME = MI->getOperand(5).getImm();
161 MI->getOperand(4).setImm((ME+1) & 31);
162 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattner043870d2005-09-09 18:17:41 +0000163 return MI;
164}
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000165
166void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
167 MachineBasicBlock::iterator MI) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000168 BuildMI(MBB, MI, get(PPC::NOP));
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000169}
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000170
171
172// Branch analysis.
173bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
174 MachineBasicBlock *&FBB,
175 std::vector<MachineOperand> &Cond) const {
176 // If the block has no terminators, it just falls into the block after it.
177 MachineBasicBlock::iterator I = MBB.end();
Evan Chengbfd2ec42007-06-08 21:59:56 +0000178 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000179 return false;
180
181 // Get the last instruction in the block.
182 MachineInstr *LastInst = I;
183
184 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000185 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000186 if (LastInst->getOpcode() == PPC::B) {
187 TBB = LastInst->getOperand(0).getMachineBasicBlock();
188 return false;
Chris Lattner289c2d52006-11-17 22:14:47 +0000189 } else if (LastInst->getOpcode() == PPC::BCC) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000190 // Block ends with fall-through condbranch.
191 TBB = LastInst->getOperand(2).getMachineBasicBlock();
192 Cond.push_back(LastInst->getOperand(0));
193 Cond.push_back(LastInst->getOperand(1));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000194 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000195 }
196 // Otherwise, don't know what this is.
197 return true;
198 }
199
200 // Get the instruction before it if it's a terminator.
201 MachineInstr *SecondLastInst = I;
202
203 // If there are three terminators, we don't know what sort of block this is.
204 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000205 isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000206 return true;
207
Chris Lattner289c2d52006-11-17 22:14:47 +0000208 // If the block ends with PPC::B and PPC:BCC, handle it.
209 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000210 LastInst->getOpcode() == PPC::B) {
211 TBB = SecondLastInst->getOperand(2).getMachineBasicBlock();
212 Cond.push_back(SecondLastInst->getOperand(0));
213 Cond.push_back(SecondLastInst->getOperand(1));
214 FBB = LastInst->getOperand(0).getMachineBasicBlock();
215 return false;
216 }
217
Dale Johannesen13e8b512007-06-13 17:59:52 +0000218 // If the block ends with two PPC:Bs, handle it. The second one is not
219 // executed, so remove it.
220 if (SecondLastInst->getOpcode() == PPC::B &&
221 LastInst->getOpcode() == PPC::B) {
222 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
223 I = LastInst;
224 I->eraseFromParent();
225 return false;
226 }
227
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000228 // Otherwise, can't handle this.
229 return true;
230}
231
Evan Chengb5cdaa22007-05-18 00:05:48 +0000232unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000233 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000234 if (I == MBB.begin()) return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000235 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000236 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000237 return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000238
239 // Remove the branch.
240 I->eraseFromParent();
241
242 I = MBB.end();
243
Evan Chengb5cdaa22007-05-18 00:05:48 +0000244 if (I == MBB.begin()) return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000245 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000246 if (I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000247 return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000248
249 // Remove the branch.
250 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000251 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000252}
253
Evan Chengb5cdaa22007-05-18 00:05:48 +0000254unsigned
255PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
256 MachineBasicBlock *FBB,
257 const std::vector<MachineOperand> &Cond) const {
Chris Lattner2dc77232006-10-17 18:06:55 +0000258 // Shouldn't be a fall through.
259 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner54108062006-10-21 05:36:13 +0000260 assert((Cond.size() == 2 || Cond.size() == 0) &&
261 "PPC branch conditions have two components!");
Chris Lattner2dc77232006-10-17 18:06:55 +0000262
Chris Lattner54108062006-10-21 05:36:13 +0000263 // One-way branch.
Chris Lattner2dc77232006-10-17 18:06:55 +0000264 if (FBB == 0) {
Chris Lattner54108062006-10-21 05:36:13 +0000265 if (Cond.empty()) // Unconditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000266 BuildMI(&MBB, get(PPC::B)).addMBB(TBB);
Chris Lattner54108062006-10-21 05:36:13 +0000267 else // Conditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000268 BuildMI(&MBB, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000269 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000270 return 1;
Chris Lattner2dc77232006-10-17 18:06:55 +0000271 }
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000272
Chris Lattner879d09c2006-10-21 05:42:09 +0000273 // Two-way Conditional Branch.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000274 BuildMI(&MBB, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000275 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000276 BuildMI(&MBB, get(PPC::B)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000277 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000278}
279
Chris Lattneref139822006-10-28 17:35:02 +0000280bool PPCInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
281 if (MBB.empty()) return false;
282
283 switch (MBB.back().getOpcode()) {
Evan Cheng126f17a2007-05-21 18:44:17 +0000284 case PPC::BLR: // Return.
Chris Lattneref139822006-10-28 17:35:02 +0000285 case PPC::B: // Uncond branch.
286 case PPC::BCTR: // Indirect branch.
287 return true;
288 default: return false;
289 }
290}
291
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000292bool PPCInstrInfo::
293ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000294 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
295 // Leave the CR# the same, but invert the condition.
Chris Lattner18258c62006-11-17 22:37:34 +0000296 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000297 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000298}