1 //===-- X86PeepholeOpt.cpp - X86 Peephole Optimizer -----------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains a peephole optimizer for the X86.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/MachineFunctionPass.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/Target/MRegisterInfo.h"
18 #include "llvm/Target/TargetInstrInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/STLExtras.h"
26 Statistic<> NumPHOpts("x86-peephole",
27 "Number of peephole optimization performed");
28 Statistic<> NumPHMoves("x86-peephole", "Number of peephole moves folded");
29 struct PH : public MachineFunctionPass {
30 virtual bool runOnMachineFunction(MachineFunction &MF);
32 bool PeepholeOptimize(MachineBasicBlock &MBB,
33 MachineBasicBlock::iterator &I);
35 virtual const char *getPassName() const { return "X86 Peephole Optimizer"; }
39 FunctionPass *llvm::createX86PeepholeOptimizerPass() { return new PH(); }
41 bool PH::runOnMachineFunction(MachineFunction &MF) {
44 for (MachineFunction::iterator BI = MF.begin(), E = MF.end(); BI != E; ++BI)
45 for (MachineBasicBlock::iterator I = BI->begin(); I != BI->end(); )
46 if (PeepholeOptimize(*BI, I)) {
56 bool PH::PeepholeOptimize(MachineBasicBlock &MBB,
57 MachineBasicBlock::iterator &I) {
58 assert(I != MBB.end());
59 MachineBasicBlock::iterator NextI = next(I);
62 MachineInstr *Next = (NextI != MBB.end()) ? &*NextI : (MachineInstr*)0;
64 switch (MI->getOpcode()) {
67 case X86::MOV32rr: // Destroy X = X copies...
68 if (MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) {
74 // A large number of X86 instructions have forms which take an 8-bit
75 // immediate despite the fact that the operands are 16 or 32 bits. Because
76 // this can save three bytes of code size (and icache space), we want to
77 // shrink them if possible.
78 case X86::IMUL16rri: case X86::IMUL32rri:
79 assert(MI->getNumOperands() == 3 && "These should all have 3 operands!");
80 if (MI->getOperand(2).isImmediate()) {
81 int Val = MI->getOperand(2).getImmedValue();
82 // If the value is the same when signed extended from 8 bits...
83 if (Val == (signed int)(signed char)Val) {
85 switch (MI->getOpcode()) {
86 default: assert(0 && "Unknown opcode value!");
87 case X86::IMUL16rri: Opcode = X86::IMUL16rri8; break;
88 case X86::IMUL32rri: Opcode = X86::IMUL32rri8; break;
90 unsigned R0 = MI->getOperand(0).getReg();
91 unsigned R1 = MI->getOperand(1).getReg();
92 I = MBB.insert(MBB.erase(I),
93 BuildMI(Opcode, 2, R0).addReg(R1).addZImm((char)Val));
100 case X86::IMUL16rmi: case X86::IMUL32rmi:
101 assert(MI->getNumOperands() == 6 && "These should all have 6 operands!");
102 if (MI->getOperand(5).isImmediate()) {
103 int Val = MI->getOperand(5).getImmedValue();
104 // If the value is the same when signed extended from 8 bits...
105 if (Val == (signed int)(signed char)Val) {
107 switch (MI->getOpcode()) {
108 default: assert(0 && "Unknown opcode value!");
109 case X86::IMUL16rmi: Opcode = X86::IMUL16rmi8; break;
110 case X86::IMUL32rmi: Opcode = X86::IMUL32rmi8; break;
112 unsigned R0 = MI->getOperand(0).getReg();
113 unsigned R1 = MI->getOperand(1).getReg();
114 unsigned Scale = MI->getOperand(2).getImmedValue();
115 unsigned R2 = MI->getOperand(3).getReg();
116 unsigned Offset = MI->getOperand(4).getImmedValue();
117 I = MBB.insert(MBB.erase(I),
118 BuildMI(Opcode, 5, R0).addReg(R1).addZImm(Scale).
119 addReg(R2).addSImm(Offset).addZImm((char)Val));
126 case X86::ADD16ri: case X86::ADD32ri: case X86::ADC32ri:
127 case X86::SUB16ri: case X86::SUB32ri:
128 case X86::SBB16ri: case X86::SBB32ri:
129 case X86::AND16ri: case X86::AND32ri:
130 case X86::OR16ri: case X86::OR32ri:
131 case X86::XOR16ri: case X86::XOR32ri:
132 assert(MI->getNumOperands() == 2 && "These should all have 2 operands!");
133 if (MI->getOperand(1).isImmediate()) {
134 int Val = MI->getOperand(1).getImmedValue();
135 // If the value is the same when signed extended from 8 bits...
136 if (Val == (signed int)(signed char)Val) {
138 switch (MI->getOpcode()) {
139 default: assert(0 && "Unknown opcode value!");
140 case X86::ADD16ri: Opcode = X86::ADD16ri8; break;
141 case X86::ADD32ri: Opcode = X86::ADD32ri8; break;
142 case X86::ADC32ri: Opcode = X86::ADC32ri8; break;
143 case X86::SUB16ri: Opcode = X86::SUB16ri8; break;
144 case X86::SUB32ri: Opcode = X86::SUB32ri8; break;
145 case X86::SBB16ri: Opcode = X86::SBB16ri8; break;
146 case X86::SBB32ri: Opcode = X86::SBB32ri8; break;
147 case X86::AND16ri: Opcode = X86::AND16ri8; break;
148 case X86::AND32ri: Opcode = X86::AND32ri8; break;
149 case X86::OR16ri: Opcode = X86::OR16ri8; break;
150 case X86::OR32ri: Opcode = X86::OR32ri8; break;
151 case X86::XOR16ri: Opcode = X86::XOR16ri8; break;
152 case X86::XOR32ri: Opcode = X86::XOR32ri8; break;
154 unsigned R0 = MI->getOperand(0).getReg();
155 I = MBB.insert(MBB.erase(I),
156 BuildMI(Opcode, 1, R0, MachineOperand::UseAndDef)
157 .addZImm((char)Val));
163 case X86::ADD16mi: case X86::ADD32mi: case X86::ADC32mi:
164 case X86::SUB16mi: case X86::SUB32mi:
165 case X86::SBB16mi: case X86::SBB32mi:
166 case X86::AND16mi: case X86::AND32mi:
167 case X86::OR16mi: case X86::OR32mi:
168 case X86::XOR16mi: case X86::XOR32mi:
169 assert(MI->getNumOperands() == 5 && "These should all have 5 operands!");
170 if (MI->getOperand(4).isImmediate()) {
171 int Val = MI->getOperand(4).getImmedValue();
172 // If the value is the same when signed extended from 8 bits...
173 if (Val == (signed int)(signed char)Val) {
175 switch (MI->getOpcode()) {
176 default: assert(0 && "Unknown opcode value!");
177 case X86::ADD16mi: Opcode = X86::ADD16mi8; break;
178 case X86::ADD32mi: Opcode = X86::ADD32mi8; break;
179 case X86::ADC32mi: Opcode = X86::ADC32mi8; break;
180 case X86::SUB16mi: Opcode = X86::SUB16mi8; break;
181 case X86::SUB32mi: Opcode = X86::SUB32mi8; break;
182 case X86::SBB16mi: Opcode = X86::SBB16mi8; break;
183 case X86::SBB32mi: Opcode = X86::SBB32mi8; break;
184 case X86::AND16mi: Opcode = X86::AND16mi8; break;
185 case X86::AND32mi: Opcode = X86::AND32mi8; break;
186 case X86::OR16mi: Opcode = X86::OR16mi8; break;
187 case X86::OR32mi: Opcode = X86::OR32mi8; break;
188 case X86::XOR16mi: Opcode = X86::XOR16mi8; break;
189 case X86::XOR32mi: Opcode = X86::XOR32mi8; break;
191 unsigned R0 = MI->getOperand(0).getReg();
192 unsigned Scale = MI->getOperand(1).getImmedValue();
193 unsigned R1 = MI->getOperand(2).getReg();
194 if (MI->getOperand(3).isImmediate()) {
195 unsigned Offset = MI->getOperand(3).getImmedValue();
196 I = MBB.insert(MBB.erase(I),
197 BuildMI(Opcode, 5).addReg(R0).addZImm(Scale).
198 addReg(R1).addSImm(Offset).addZImm((char)Val));
199 } else if (MI->getOperand(3).isGlobalAddress()) {
200 GlobalValue *GA = MI->getOperand(3).getGlobal();
201 I = MBB.insert(MBB.erase(I),
202 BuildMI(Opcode, 5).addReg(R0).addZImm(Scale).
203 addReg(R1).addGlobalAddress(GA).addZImm((char)Val));
211 case X86::MOV32ri: Size++;
212 case X86::MOV16ri: Size++;
214 // FIXME: We can only do this transformation if we know that flags are not
215 // used here, because XOR clobbers the flags!
216 if (MI->getOperand(1).isImmediate()) { // avoid mov EAX, <value>
217 int Val = MI->getOperand(1).getImmedValue();
218 if (Val == 0) { // mov EAX, 0 -> xor EAX, EAX
219 static const unsigned Opcode[] ={X86::XOR8rr,X86::XOR16rr,X86::XOR32rr};
220 unsigned Reg = MI->getOperand(0).getReg();
221 I = MBB.insert(MBB.erase(I),
222 BuildMI(Opcode[Size], 2, Reg).addReg(Reg).addReg(Reg));
224 } else if (Val == -1) { // mov EAX, -1 -> or EAX, -1
225 // TODO: 'or Reg, -1' has a smaller encoding than 'mov Reg, -1'
230 case X86::BSWAP32r: // Change bswap EAX, bswap EAX into nothing
231 if (Next->getOpcode() == X86::BSWAP32r &&
232 MI->getOperand(0).getReg() == Next->getOperand(0).getReg()) {
233 I = MBB.erase(MBB.erase(I));
243 class UseDefChains : public MachineFunctionPass {
244 std::vector<MachineInstr*> DefiningInst;
246 // getDefinition - Return the machine instruction that defines the specified
247 // SSA virtual register.
248 MachineInstr *getDefinition(unsigned Reg) {
249 assert(MRegisterInfo::isVirtualRegister(Reg) &&
250 "use-def chains only exist for SSA registers!");
251 assert(Reg - MRegisterInfo::FirstVirtualRegister < DefiningInst.size() &&
252 "Unknown register number!");
253 assert(DefiningInst[Reg-MRegisterInfo::FirstVirtualRegister] &&
254 "Unknown register number!");
255 return DefiningInst[Reg-MRegisterInfo::FirstVirtualRegister];
258 // setDefinition - Update the use-def chains to indicate that MI defines
260 void setDefinition(unsigned Reg, MachineInstr *MI) {
261 if (Reg-MRegisterInfo::FirstVirtualRegister >= DefiningInst.size())
262 DefiningInst.resize(Reg-MRegisterInfo::FirstVirtualRegister+1);
263 DefiningInst[Reg-MRegisterInfo::FirstVirtualRegister] = MI;
266 // removeDefinition - Update the use-def chains to forget about Reg
268 void removeDefinition(unsigned Reg) {
269 assert(getDefinition(Reg)); // Check validity
270 DefiningInst[Reg-MRegisterInfo::FirstVirtualRegister] = 0;
273 virtual bool runOnMachineFunction(MachineFunction &MF) {
274 for (MachineFunction::iterator BI = MF.begin(), E = MF.end(); BI!=E; ++BI)
275 for (MachineBasicBlock::iterator I = BI->begin(); I != BI->end(); ++I) {
276 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
277 MachineOperand &MO = I->getOperand(i);
278 if (MO.isRegister() && MO.isDef() && !MO.isUse() &&
279 MRegisterInfo::isVirtualRegister(MO.getReg()))
280 setDefinition(MO.getReg(), I);
286 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
287 AU.setPreservesAll();
288 MachineFunctionPass::getAnalysisUsage(AU);
291 virtual void releaseMemory() {
292 std::vector<MachineInstr*>().swap(DefiningInst);
296 RegisterAnalysis<UseDefChains> X("use-def-chains",
297 "use-def chain construction for machine code");
302 Statistic<> NumSSAPHOpts("x86-ssa-peephole",
303 "Number of SSA peephole optimization performed");
305 /// SSAPH - This pass is an X86-specific, SSA-based, peephole optimizer. This
306 /// pass is really a bad idea: a better instruction selector should completely
307 /// supersume it. However, that will take some time to develop, and the
308 /// simple things this can do are important now.
309 class SSAPH : public MachineFunctionPass {
312 virtual bool runOnMachineFunction(MachineFunction &MF);
314 bool PeepholeOptimize(MachineBasicBlock &MBB,
315 MachineBasicBlock::iterator &I);
317 virtual const char *getPassName() const {
318 return "X86 SSA-based Peephole Optimizer";
321 /// Propagate - Set MI[DestOpNo] = Src[SrcOpNo], optionally change the
322 /// opcode of the instruction, then return true.
323 bool Propagate(MachineInstr *MI, unsigned DestOpNo,
324 MachineInstr *Src, unsigned SrcOpNo, unsigned NewOpcode = 0){
325 MI->getOperand(DestOpNo) = Src->getOperand(SrcOpNo);
326 if (NewOpcode) MI->setOpcode(NewOpcode);
330 /// OptimizeAddress - If we can fold the addressing arithmetic for this
331 /// memory instruction into the instruction itself, do so and return true.
332 bool OptimizeAddress(MachineInstr *MI, unsigned OpNo);
334 /// getDefininingInst - If the specified operand is a read of an SSA
335 /// register, return the machine instruction defining it, otherwise, return
337 MachineInstr *getDefiningInst(MachineOperand &MO) {
338 if (MO.isDef() || !MO.isRegister() ||
339 !MRegisterInfo::isVirtualRegister(MO.getReg())) return 0;
340 return UDC->getDefinition(MO.getReg());
343 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
344 AU.addRequired<UseDefChains>();
345 AU.addPreserved<UseDefChains>();
346 MachineFunctionPass::getAnalysisUsage(AU);
351 FunctionPass *llvm::createX86SSAPeepholeOptimizerPass() { return new SSAPH(); }
353 bool SSAPH::runOnMachineFunction(MachineFunction &MF) {
354 bool Changed = false;
357 UDC = &getAnalysis<UseDefChains>();
360 LocalChanged = false;
362 for (MachineFunction::iterator BI = MF.begin(), E = MF.end(); BI != E; ++BI)
363 for (MachineBasicBlock::iterator I = BI->begin(); I != BI->end(); )
364 if (PeepholeOptimize(*BI, I)) {
369 Changed |= LocalChanged;
370 } while (LocalChanged);
375 static bool isValidScaleAmount(unsigned Scale) {
376 return Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8;
379 /// OptimizeAddress - If we can fold the addressing arithmetic for this
380 /// memory instruction into the instruction itself, do so and return true.
381 bool SSAPH::OptimizeAddress(MachineInstr *MI, unsigned OpNo) {
382 MachineOperand &BaseRegOp = MI->getOperand(OpNo+0);
383 MachineOperand &ScaleOp = MI->getOperand(OpNo+1);
384 MachineOperand &IndexRegOp = MI->getOperand(OpNo+2);
385 MachineOperand &DisplacementOp = MI->getOperand(OpNo+3);
387 unsigned BaseReg = BaseRegOp.hasAllocatedReg() ? BaseRegOp.getReg() : 0;
388 unsigned Scale = ScaleOp.getImmedValue();
389 unsigned IndexReg = IndexRegOp.hasAllocatedReg() ? IndexRegOp.getReg() : 0;
391 bool Changed = false;
393 // If the base register is unset, and the index register is set with a scale
394 // of 1, move it to be the base register.
395 if (BaseRegOp.hasAllocatedReg() && BaseReg == 0 &&
396 Scale == 1 && IndexReg != 0) {
397 BaseRegOp.setReg(IndexReg);
398 IndexRegOp.setReg(0);
402 // Attempt to fold instructions used by the base register into the instruction
403 if (MachineInstr *DefInst = getDefiningInst(BaseRegOp)) {
404 switch (DefInst->getOpcode()) {
406 // If there is no displacement set for this instruction set one now.
407 // FIXME: If we can fold two immediates together, we should do so!
408 if (DisplacementOp.isImmediate() && !DisplacementOp.getImmedValue()) {
409 if (DefInst->getOperand(1).isImmediate()) {
411 return Propagate(MI, OpNo+3, DefInst, 1);
417 // If the source is a register-register add, and we do not yet have an
418 // index register, fold the add into the memory address.
420 BaseRegOp = DefInst->getOperand(1);
421 IndexRegOp = DefInst->getOperand(2);
422 ScaleOp.setImmedValue(1);
428 // If this shift could be folded into the index portion of the address if
429 // it were the index register, move it to the index register operand now,
430 // so it will be folded in below.
431 if ((Scale == 1 || (IndexReg == 0 && IndexRegOp.hasAllocatedReg())) &&
432 DefInst->getOperand(2).getImmedValue() < 4) {
433 std::swap(BaseRegOp, IndexRegOp);
434 ScaleOp.setImmedValue(1); Scale = 1;
435 std::swap(IndexReg, BaseReg);
442 // Attempt to fold instructions used by the index into the instruction
443 if (MachineInstr *DefInst = getDefiningInst(IndexRegOp)) {
444 switch (DefInst->getOpcode()) {
446 // Figure out what the resulting scale would be if we folded this shift.
447 unsigned ResScale = Scale * (1 << DefInst->getOperand(2).getImmedValue());
448 if (isValidScaleAmount(ResScale)) {
449 IndexRegOp = DefInst->getOperand(1);
450 ScaleOp.setImmedValue(ResScale);
461 bool SSAPH::PeepholeOptimize(MachineBasicBlock &MBB,
462 MachineBasicBlock::iterator &I) {
463 MachineBasicBlock::iterator NextI = next(I);
465 MachineInstr *MI = I;
466 MachineInstr *Next = (NextI != MBB.end()) ? &*NextI : (MachineInstr*)0;
468 bool Changed = false;
470 const TargetInstrInfo &TII = *MBB.getParent()->getTarget().getInstrInfo();
472 // Scan the operands of this instruction. If any operands are
473 // register-register copies, replace the operand with the source.
474 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
475 // Is this an SSA register use?
476 if (MachineInstr *DefInst = getDefiningInst(MI->getOperand(i))) {
477 // If the operand is a vreg-vreg copy, it is always safe to replace the
478 // source value with the input operand.
479 unsigned Source, Dest;
480 if (TII.isMoveInstr(*DefInst, Source, Dest)) {
481 // Don't propagate physical registers into any instructions.
482 if (DefInst->getOperand(1).isRegister() &&
483 MRegisterInfo::isVirtualRegister(Source)) {
484 MI->getOperand(i).setReg(Source);
492 // Perform instruction specific optimizations.
493 switch (MI->getOpcode()) {
495 // Register to memory stores. Format: <base,scale,indexreg,immdisp>, srcreg
496 case X86::MOV32mr: case X86::MOV16mr: case X86::MOV8mr:
497 case X86::MOV32mi: case X86::MOV16mi: case X86::MOV8mi:
498 // Check to see if we can fold the source instruction into this one...
499 if (MachineInstr *SrcInst = getDefiningInst(MI->getOperand(4))) {
500 switch (SrcInst->getOpcode()) {
501 // Fold the immediate value into the store, if possible.
502 case X86::MOV8ri: return Propagate(MI, 4, SrcInst, 1, X86::MOV8mi);
503 case X86::MOV16ri: return Propagate(MI, 4, SrcInst, 1, X86::MOV16mi);
504 case X86::MOV32ri: return Propagate(MI, 4, SrcInst, 1, X86::MOV32mi);
509 // If we can optimize the addressing expression, do so now.
510 if (OptimizeAddress(MI, 0))
517 // If we can optimize the addressing expression, do so now.
518 if (OptimizeAddress(MI, 1))