blob: d7ee5ed04e8beff779a8be156556093855fa5b33 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000019using namespace llvm;
20
Chris Lattnerb1d26f62006-06-17 00:01:04 +000021PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Chris Lattner804e0672006-07-11 00:48:23 +000022 : TargetInstrInfo(PPCInsts, sizeof(PPCInsts)/sizeof(PPCInsts[0])), TM(tm),
Evan Cheng7ce45782006-11-13 23:36:35 +000023 RI(*TM.getSubtargetImpl(), *this) {}
Chris Lattnerb1d26f62006-06-17 00:01:04 +000024
25/// getPointerRegClass - Return the register class to use to hold pointers.
26/// This is used for addressing modes.
27const TargetRegisterClass *PPCInstrInfo::getPointerRegClass() const {
28 if (TM.getSubtargetImpl()->isPPC64())
29 return &PPC::G8RCRegClass;
30 else
31 return &PPC::GPRCRegClass;
32}
33
Misha Brukmanf2ccb772004-08-17 04:55:41 +000034
Nate Begeman21e463b2005-10-16 05:39:50 +000035bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
36 unsigned& sourceReg,
37 unsigned& destReg) const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +000038 MachineOpCode oc = MI.getOpcode();
Chris Lattnerb410dc92006-06-20 23:18:58 +000039 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
Chris Lattner14c09b82005-10-19 01:50:36 +000040 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000041 assert(MI.getNumOperands() >= 3 &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000042 MI.getOperand(0).isRegister() &&
43 MI.getOperand(1).isRegister() &&
44 MI.getOperand(2).isRegister() &&
45 "invalid PPC OR instruction!");
46 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
47 sourceReg = MI.getOperand(1).getReg();
48 destReg = MI.getOperand(0).getReg();
49 return true;
50 }
51 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000052 assert(MI.getNumOperands() >= 3 &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000053 MI.getOperand(0).isRegister() &&
54 MI.getOperand(2).isImmediate() &&
55 "invalid PPC ADDI instruction!");
56 if (MI.getOperand(1).isRegister() && MI.getOperand(2).getImmedValue()==0) {
57 sourceReg = MI.getOperand(1).getReg();
58 destReg = MI.getOperand(0).getReg();
59 return true;
60 }
Nate Begemancb90de32004-10-07 22:26:12 +000061 } else if (oc == PPC::ORI) { // ori r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000062 assert(MI.getNumOperands() >= 3 &&
Nate Begemancb90de32004-10-07 22:26:12 +000063 MI.getOperand(0).isRegister() &&
64 MI.getOperand(1).isRegister() &&
65 MI.getOperand(2).isImmediate() &&
66 "invalid PPC ORI instruction!");
67 if (MI.getOperand(2).getImmedValue()==0) {
68 sourceReg = MI.getOperand(1).getReg();
69 destReg = MI.getOperand(0).getReg();
70 return true;
71 }
Chris Lattnereb5d47d2005-10-07 05:00:52 +000072 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
73 oc == PPC::FMRSD) { // fmr r1, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000074 assert(MI.getNumOperands() >= 2 &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000075 MI.getOperand(0).isRegister() &&
76 MI.getOperand(1).isRegister() &&
77 "invalid PPC FMR instruction");
78 sourceReg = MI.getOperand(1).getReg();
79 destReg = MI.getOperand(0).getReg();
80 return true;
Nate Begeman7af02482005-04-12 07:04:16 +000081 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
Evan Cheng1e3417292007-04-25 07:12:14 +000082 assert(MI.getNumOperands() >= 2 &&
Nate Begeman7af02482005-04-12 07:04:16 +000083 MI.getOperand(0).isRegister() &&
84 MI.getOperand(1).isRegister() &&
85 "invalid PPC MCRF instruction");
86 sourceReg = MI.getOperand(1).getReg();
87 destReg = MI.getOperand(0).getReg();
88 return true;
Misha Brukmanf2ccb772004-08-17 04:55:41 +000089 }
90 return false;
91}
Chris Lattner043870d2005-09-09 18:17:41 +000092
Chris Lattner40839602006-02-02 20:12:32 +000093unsigned PPCInstrInfo::isLoadFromStackSlot(MachineInstr *MI,
Chris Lattner9c09c9e2006-03-16 22:24:02 +000094 int &FrameIndex) const {
Chris Lattner40839602006-02-02 20:12:32 +000095 switch (MI->getOpcode()) {
96 default: break;
97 case PPC::LD:
98 case PPC::LWZ:
99 case PPC::LFS:
100 case PPC::LFD:
101 if (MI->getOperand(1).isImmediate() && !MI->getOperand(1).getImmedValue() &&
102 MI->getOperand(2).isFrameIndex()) {
103 FrameIndex = MI->getOperand(2).getFrameIndex();
104 return MI->getOperand(0).getReg();
105 }
106 break;
107 }
108 return 0;
Chris Lattner65242872006-02-02 20:16:12 +0000109}
Chris Lattner40839602006-02-02 20:12:32 +0000110
Chris Lattner65242872006-02-02 20:16:12 +0000111unsigned PPCInstrInfo::isStoreToStackSlot(MachineInstr *MI,
112 int &FrameIndex) const {
113 switch (MI->getOpcode()) {
114 default: break;
Nate Begeman3b478b32006-02-02 21:07:50 +0000115 case PPC::STD:
Chris Lattner65242872006-02-02 20:16:12 +0000116 case PPC::STW:
117 case PPC::STFS:
118 case PPC::STFD:
119 if (MI->getOperand(1).isImmediate() && !MI->getOperand(1).getImmedValue() &&
120 MI->getOperand(2).isFrameIndex()) {
121 FrameIndex = MI->getOperand(2).getFrameIndex();
122 return MI->getOperand(0).getReg();
123 }
124 break;
125 }
126 return 0;
127}
Chris Lattner40839602006-02-02 20:12:32 +0000128
Chris Lattner043870d2005-09-09 18:17:41 +0000129// commuteInstruction - We can commute rlwimi instructions, but only if the
130// rotate amt is zero. We also have to munge the immediates a bit.
Nate Begeman21e463b2005-10-16 05:39:50 +0000131MachineInstr *PPCInstrInfo::commuteInstruction(MachineInstr *MI) const {
Chris Lattner043870d2005-09-09 18:17:41 +0000132 // Normal instructions can be commuted the obvious way.
133 if (MI->getOpcode() != PPC::RLWIMI)
134 return TargetInstrInfo::commuteInstruction(MI);
135
136 // Cannot commute if it has a non-zero rotate count.
137 if (MI->getOperand(3).getImmedValue() != 0)
138 return 0;
139
140 // If we have a zero rotate count, we have:
141 // M = mask(MB,ME)
142 // Op0 = (Op1 & ~M) | (Op2 & M)
143 // Change this to:
144 // M = mask((ME+1)&31, (MB-1)&31)
145 // Op0 = (Op2 & ~M) | (Op1 & M)
146
147 // Swap op1/op2
148 unsigned Reg1 = MI->getOperand(1).getReg();
149 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000150 bool Reg1IsKill = MI->getOperand(1).isKill();
151 bool Reg2IsKill = MI->getOperand(2).isKill();
Chris Lattnere53f4a02006-05-04 17:52:23 +0000152 MI->getOperand(2).setReg(Reg1);
153 MI->getOperand(1).setReg(Reg2);
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000154 if (Reg1IsKill)
155 MI->getOperand(2).setIsKill();
156 else
157 MI->getOperand(2).unsetIsKill();
158 if (Reg2IsKill)
159 MI->getOperand(1).setIsKill();
160 else
161 MI->getOperand(1).unsetIsKill();
Chris Lattner043870d2005-09-09 18:17:41 +0000162
163 // Swap the mask around.
164 unsigned MB = MI->getOperand(4).getImmedValue();
165 unsigned ME = MI->getOperand(5).getImmedValue();
166 MI->getOperand(4).setImmedValue((ME+1) & 31);
167 MI->getOperand(5).setImmedValue((MB-1) & 31);
168 return MI;
169}
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000170
171void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
172 MachineBasicBlock::iterator MI) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000173 BuildMI(MBB, MI, get(PPC::NOP));
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000174}
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000175
176
177// Branch analysis.
178bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
179 MachineBasicBlock *&FBB,
180 std::vector<MachineOperand> &Cond) const {
181 // If the block has no terminators, it just falls into the block after it.
182 MachineBasicBlock::iterator I = MBB.end();
Evan Chengbfd2ec42007-06-08 21:59:56 +0000183 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000184 return false;
185
186 // Get the last instruction in the block.
187 MachineInstr *LastInst = I;
188
189 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000190 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000191 if (LastInst->getOpcode() == PPC::B) {
192 TBB = LastInst->getOperand(0).getMachineBasicBlock();
193 return false;
Chris Lattner289c2d52006-11-17 22:14:47 +0000194 } else if (LastInst->getOpcode() == PPC::BCC) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000195 // Block ends with fall-through condbranch.
196 TBB = LastInst->getOperand(2).getMachineBasicBlock();
197 Cond.push_back(LastInst->getOperand(0));
198 Cond.push_back(LastInst->getOperand(1));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000199 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000200 }
201 // Otherwise, don't know what this is.
202 return true;
203 }
204
205 // Get the instruction before it if it's a terminator.
206 MachineInstr *SecondLastInst = I;
207
208 // If there are three terminators, we don't know what sort of block this is.
209 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000210 isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000211 return true;
212
Chris Lattner289c2d52006-11-17 22:14:47 +0000213 // If the block ends with PPC::B and PPC:BCC, handle it.
214 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000215 LastInst->getOpcode() == PPC::B) {
216 TBB = SecondLastInst->getOperand(2).getMachineBasicBlock();
217 Cond.push_back(SecondLastInst->getOperand(0));
218 Cond.push_back(SecondLastInst->getOperand(1));
219 FBB = LastInst->getOperand(0).getMachineBasicBlock();
220 return false;
221 }
222
Dale Johannesen13e8b512007-06-13 17:59:52 +0000223 // If the block ends with two PPC:Bs, handle it. The second one is not
224 // executed, so remove it.
225 if (SecondLastInst->getOpcode() == PPC::B &&
226 LastInst->getOpcode() == PPC::B) {
227 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
228 I = LastInst;
229 I->eraseFromParent();
230 return false;
231 }
232
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000233 // Otherwise, can't handle this.
234 return true;
235}
236
Evan Chengb5cdaa22007-05-18 00:05:48 +0000237unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000238 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000239 if (I == MBB.begin()) return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000240 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000241 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000242 return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000243
244 // Remove the branch.
245 I->eraseFromParent();
246
247 I = MBB.end();
248
Evan Chengb5cdaa22007-05-18 00:05:48 +0000249 if (I == MBB.begin()) return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000250 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000251 if (I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000252 return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000253
254 // Remove the branch.
255 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000256 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000257}
258
Evan Chengb5cdaa22007-05-18 00:05:48 +0000259unsigned
260PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
261 MachineBasicBlock *FBB,
262 const std::vector<MachineOperand> &Cond) const {
Chris Lattner2dc77232006-10-17 18:06:55 +0000263 // Shouldn't be a fall through.
264 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner54108062006-10-21 05:36:13 +0000265 assert((Cond.size() == 2 || Cond.size() == 0) &&
266 "PPC branch conditions have two components!");
Chris Lattner2dc77232006-10-17 18:06:55 +0000267
Chris Lattner54108062006-10-21 05:36:13 +0000268 // One-way branch.
Chris Lattner2dc77232006-10-17 18:06:55 +0000269 if (FBB == 0) {
Chris Lattner54108062006-10-21 05:36:13 +0000270 if (Cond.empty()) // Unconditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000271 BuildMI(&MBB, get(PPC::B)).addMBB(TBB);
Chris Lattner54108062006-10-21 05:36:13 +0000272 else // Conditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000273 BuildMI(&MBB, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000274 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000275 return 1;
Chris Lattner2dc77232006-10-17 18:06:55 +0000276 }
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000277
Chris Lattner879d09c2006-10-21 05:42:09 +0000278 // Two-way Conditional Branch.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000279 BuildMI(&MBB, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000280 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000281 BuildMI(&MBB, get(PPC::B)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000282 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000283}
284
Chris Lattneref139822006-10-28 17:35:02 +0000285bool PPCInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
286 if (MBB.empty()) return false;
287
288 switch (MBB.back().getOpcode()) {
Evan Cheng126f17a2007-05-21 18:44:17 +0000289 case PPC::BLR: // Return.
Chris Lattneref139822006-10-28 17:35:02 +0000290 case PPC::B: // Uncond branch.
291 case PPC::BCTR: // Indirect branch.
292 return true;
293 default: return false;
294 }
295}
296
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000297bool PPCInstrInfo::
298ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000299 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
300 // Leave the CR# the same, but invert the condition.
Chris Lattner18258c62006-11-17 22:37:34 +0000301 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000302 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000303}