blob: a7439b5129b5bafa6ef1cb6e18fd3ea06c33dc7b [file] [log] [blame]
Bill Wendling6cdb1ab2010-08-09 23:59:04 +00001//===-- PeepholeOptimizer.cpp - Peephole Optimizations --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Perform peephole optimizations on the machine code:
11//
12// - Optimize Extensions
13//
14// Optimization of sign / zero extension instructions. It may be extended to
15// handle other instructions with similar properties.
16//
17// On some targets, some instructions, e.g. X86 sign / zero extension, may
18// leave the source value in the lower part of the result. This optimization
19// will replace some uses of the pre-extension value with uses of the
20// sub-register of the results.
21//
22// - Optimize Comparisons
23//
24// Optimization of comparison instructions. For instance, in this code:
25//
26// sub r1, 1
27// cmp r1, 0
28// bz L1
29//
30// If the "sub" instruction all ready sets (or could be modified to set) the
31// same flag that the "cmp" instruction sets and that "bz" uses, then we can
32// eliminate the "cmp" instruction.
Evan Chengd158fba2011-03-15 05:13:13 +000033//
Manman Ren247c5ab2012-05-11 01:30:47 +000034// Another instance, in this code:
35//
36// sub r1, r3 | sub r1, imm
37// cmp r3, r1 or cmp r1, r3 | cmp r1, imm
38// bge L1
39//
40// If the branch instruction can use flag from "sub", then we can replace
41// "sub" with "subs" and eliminate the "cmp" instruction.
42//
Evan Chengd158fba2011-03-15 05:13:13 +000043// - Optimize Bitcast pairs:
44//
45// v1 = bitcast v0
46// v2 = bitcast v1
47// = v2
48// =>
49// v1 = bitcast v0
50// = v0
Andrew Trick1df91b02012-02-08 21:22:43 +000051//
Joel Jones8293b7b2012-12-11 16:10:25 +000052// - Optimize Loads:
53//
54// Loads that can be folded into a later instruction. A load is foldable
55// if it loads to virtual registers and the virtual register defined has
56// a single use.
Bill Wendling6cdb1ab2010-08-09 23:59:04 +000057//===----------------------------------------------------------------------===//
58
59#define DEBUG_TYPE "peephole-opt"
60#include "llvm/CodeGen/Passes.h"
Evan Chengc4af4632010-11-17 20:13:28 +000061#include "llvm/ADT/DenseMap.h"
Bill Wendling6cdb1ab2010-08-09 23:59:04 +000062#include "llvm/ADT/SmallPtrSet.h"
Evan Chengc4af4632010-11-17 20:13:28 +000063#include "llvm/ADT/SmallSet.h"
Bill Wendling6cdb1ab2010-08-09 23:59:04 +000064#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000065#include "llvm/CodeGen/MachineDominators.h"
66#include "llvm/CodeGen/MachineInstrBuilder.h"
67#include "llvm/CodeGen/MachineRegisterInfo.h"
68#include "llvm/Support/CommandLine.h"
Craig Toppera1032b72012-12-17 03:56:00 +000069#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000070#include "llvm/Target/TargetInstrInfo.h"
71#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendling6cdb1ab2010-08-09 23:59:04 +000072using namespace llvm;
73
74// Optimize Extensions
75static cl::opt<bool>
76Aggressive("aggressive-ext-opt", cl::Hidden,
77 cl::desc("Aggressive extension optimization"));
78
Bill Wendling40a5eb12010-11-01 20:41:43 +000079static cl::opt<bool>
80DisablePeephole("disable-peephole", cl::Hidden, cl::init(false),
81 cl::desc("Disable the peephole optimizer"));
82
Bill Wendling69c5eb52010-08-27 20:39:09 +000083STATISTIC(NumReuse, "Number of extension results reused");
Evan Chengd158fba2011-03-15 05:13:13 +000084STATISTIC(NumBitcasts, "Number of bitcasts eliminated");
85STATISTIC(NumCmps, "Number of compares eliminated");
Lang Hames3b26eb62012-02-25 00:46:38 +000086STATISTIC(NumImmFold, "Number of move immediate folded");
Manman Rend7d003c2012-08-02 00:56:42 +000087STATISTIC(NumLoadFold, "Number of loads folded");
Jakob Stoklund Olesenf2c64ef2012-08-16 23:11:47 +000088STATISTIC(NumSelects, "Number of selects optimized");
Bill Wendling6cdb1ab2010-08-09 23:59:04 +000089
90namespace {
91 class PeepholeOptimizer : public MachineFunctionPass {
92 const TargetMachine *TM;
93 const TargetInstrInfo *TII;
94 MachineRegisterInfo *MRI;
95 MachineDominatorTree *DT; // Machine dominator tree
96
97 public:
98 static char ID; // Pass identification
Owen Anderson081c34b2010-10-19 17:21:58 +000099 PeepholeOptimizer() : MachineFunctionPass(ID) {
100 initializePeepholeOptimizerPass(*PassRegistry::getPassRegistry());
101 }
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000102
103 virtual bool runOnMachineFunction(MachineFunction &MF);
104
105 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
106 AU.setPreservesCFG();
107 MachineFunctionPass::getAnalysisUsage(AU);
108 if (Aggressive) {
109 AU.addRequired<MachineDominatorTree>();
110 AU.addPreserved<MachineDominatorTree>();
111 }
112 }
113
114 private:
Jim Grosbach39cc5132012-05-01 23:21:41 +0000115 bool optimizeBitcastInstr(MachineInstr *MI, MachineBasicBlock *MBB);
116 bool optimizeCmpInstr(MachineInstr *MI, MachineBasicBlock *MBB);
117 bool optimizeExtInstr(MachineInstr *MI, MachineBasicBlock *MBB,
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000118 SmallPtrSet<MachineInstr*, 8> &LocalMIs);
Jakob Stoklund Olesenf2c64ef2012-08-16 23:11:47 +0000119 bool optimizeSelect(MachineInstr *MI);
Evan Chengc4af4632010-11-17 20:13:28 +0000120 bool isMoveImmediate(MachineInstr *MI,
121 SmallSet<unsigned, 4> &ImmDefRegs,
122 DenseMap<unsigned, MachineInstr*> &ImmDefMIs);
Jim Grosbach39cc5132012-05-01 23:21:41 +0000123 bool foldImmediate(MachineInstr *MI, MachineBasicBlock *MBB,
Evan Chengc4af4632010-11-17 20:13:28 +0000124 SmallSet<unsigned, 4> &ImmDefRegs,
125 DenseMap<unsigned, MachineInstr*> &ImmDefMIs);
Manman Rend7d003c2012-08-02 00:56:42 +0000126 bool isLoadFoldable(MachineInstr *MI, unsigned &FoldAsLoadDefReg);
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000127 };
128}
129
130char PeepholeOptimizer::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +0000131char &llvm::PeepholeOptimizerID = PeepholeOptimizer::ID;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000132INITIALIZE_PASS_BEGIN(PeepholeOptimizer, "peephole-opts",
133 "Peephole Optimizations", false, false)
134INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
135INITIALIZE_PASS_END(PeepholeOptimizer, "peephole-opts",
Owen Andersonce665bd2010-10-07 22:25:06 +0000136 "Peephole Optimizations", false, false)
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000137
Jim Grosbach39cc5132012-05-01 23:21:41 +0000138/// optimizeExtInstr - If instruction is a copy-like instruction, i.e. it reads
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000139/// a single register and writes a single register and it does not modify the
140/// source, and if the source value is preserved as a sub-register of the
141/// result, then replace all reachable uses of the source with the subreg of the
142/// result.
Andrew Trick1df91b02012-02-08 21:22:43 +0000143///
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000144/// Do not generate an EXTRACT that is used only in a debug use, as this changes
145/// the code. Since this code does not currently share EXTRACTs, just ignore all
146/// debug uses.
147bool PeepholeOptimizer::
Jim Grosbach39cc5132012-05-01 23:21:41 +0000148optimizeExtInstr(MachineInstr *MI, MachineBasicBlock *MBB,
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000149 SmallPtrSet<MachineInstr*, 8> &LocalMIs) {
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000150 unsigned SrcReg, DstReg, SubIdx;
151 if (!TII->isCoalescableExtInstr(*MI, SrcReg, DstReg, SubIdx))
152 return false;
Andrew Trick1df91b02012-02-08 21:22:43 +0000153
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000154 if (TargetRegisterInfo::isPhysicalRegister(DstReg) ||
155 TargetRegisterInfo::isPhysicalRegister(SrcReg))
156 return false;
157
Jakob Stoklund Olesend8d02792012-06-19 21:10:18 +0000158 if (MRI->hasOneNonDBGUse(SrcReg))
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000159 // No other uses.
160 return false;
161
Jakob Stoklund Olesen418a3632012-05-20 18:42:55 +0000162 // Ensure DstReg can get a register class that actually supports
163 // sub-registers. Don't change the class until we commit.
164 const TargetRegisterClass *DstRC = MRI->getRegClass(DstReg);
165 DstRC = TM->getRegisterInfo()->getSubClassWithSubReg(DstRC, SubIdx);
166 if (!DstRC)
167 return false;
168
Jakob Stoklund Olesen71642882012-06-19 21:14:34 +0000169 // The ext instr may be operating on a sub-register of SrcReg as well.
170 // PPC::EXTSW is a 32 -> 64-bit sign extension, but it reads a 64-bit
171 // register.
172 // If UseSrcSubIdx is Set, SubIdx also applies to SrcReg, and only uses of
173 // SrcReg:SubIdx should be replaced.
174 bool UseSrcSubIdx = TM->getRegisterInfo()->
175 getSubClassWithSubReg(MRI->getRegClass(SrcReg), SubIdx) != 0;
176
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000177 // The source has other uses. See if we can replace the other uses with use of
178 // the result of the extension.
179 SmallPtrSet<MachineBasicBlock*, 4> ReachedBBs;
Jakob Stoklund Olesend8d02792012-06-19 21:10:18 +0000180 for (MachineRegisterInfo::use_nodbg_iterator
181 UI = MRI->use_nodbg_begin(DstReg), UE = MRI->use_nodbg_end();
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000182 UI != UE; ++UI)
183 ReachedBBs.insert(UI->getParent());
184
185 // Uses that are in the same BB of uses of the result of the instruction.
186 SmallVector<MachineOperand*, 8> Uses;
187
188 // Uses that the result of the instruction can reach.
189 SmallVector<MachineOperand*, 8> ExtendedUses;
190
191 bool ExtendLife = true;
Jakob Stoklund Olesend8d02792012-06-19 21:10:18 +0000192 for (MachineRegisterInfo::use_nodbg_iterator
193 UI = MRI->use_nodbg_begin(SrcReg), UE = MRI->use_nodbg_end();
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000194 UI != UE; ++UI) {
195 MachineOperand &UseMO = UI.getOperand();
196 MachineInstr *UseMI = &*UI;
197 if (UseMI == MI)
198 continue;
199
200 if (UseMI->isPHI()) {
201 ExtendLife = false;
202 continue;
203 }
204
Jakob Stoklund Olesen71642882012-06-19 21:14:34 +0000205 // Only accept uses of SrcReg:SubIdx.
206 if (UseSrcSubIdx && UseMO.getSubReg() != SubIdx)
207 continue;
208
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000209 // It's an error to translate this:
210 //
211 // %reg1025 = <sext> %reg1024
212 // ...
213 // %reg1026 = SUBREG_TO_REG 0, %reg1024, 4
214 //
215 // into this:
216 //
217 // %reg1025 = <sext> %reg1024
218 // ...
219 // %reg1027 = COPY %reg1025:4
220 // %reg1026 = SUBREG_TO_REG 0, %reg1027, 4
221 //
222 // The problem here is that SUBREG_TO_REG is there to assert that an
223 // implicit zext occurs. It doesn't insert a zext instruction. If we allow
224 // the COPY here, it will give us the value after the <sext>, not the
225 // original value of %reg1024 before <sext>.
226 if (UseMI->getOpcode() == TargetOpcode::SUBREG_TO_REG)
227 continue;
228
229 MachineBasicBlock *UseMBB = UseMI->getParent();
230 if (UseMBB == MBB) {
231 // Local uses that come after the extension.
232 if (!LocalMIs.count(UseMI))
233 Uses.push_back(&UseMO);
234 } else if (ReachedBBs.count(UseMBB)) {
235 // Non-local uses where the result of the extension is used. Always
236 // replace these unless it's a PHI.
237 Uses.push_back(&UseMO);
238 } else if (Aggressive && DT->dominates(MBB, UseMBB)) {
239 // We may want to extend the live range of the extension result in order
240 // to replace these uses.
241 ExtendedUses.push_back(&UseMO);
242 } else {
243 // Both will be live out of the def MBB anyway. Don't extend live range of
244 // the extension result.
245 ExtendLife = false;
246 break;
247 }
248 }
249
250 if (ExtendLife && !ExtendedUses.empty())
251 // Extend the liveness of the extension result.
252 std::copy(ExtendedUses.begin(), ExtendedUses.end(),
253 std::back_inserter(Uses));
254
255 // Now replace all uses.
256 bool Changed = false;
257 if (!Uses.empty()) {
258 SmallPtrSet<MachineBasicBlock*, 4> PHIBBs;
259
260 // Look for PHI uses of the extended result, we don't want to extend the
261 // liveness of a PHI input. It breaks all kinds of assumptions down
262 // stream. A PHI use is expected to be the kill of its source values.
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000263 for (MachineRegisterInfo::use_nodbg_iterator
Jakob Stoklund Olesend8d02792012-06-19 21:10:18 +0000264 UI = MRI->use_nodbg_begin(DstReg), UE = MRI->use_nodbg_end();
265 UI != UE; ++UI)
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000266 if (UI->isPHI())
267 PHIBBs.insert(UI->getParent());
268
269 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
270 for (unsigned i = 0, e = Uses.size(); i != e; ++i) {
271 MachineOperand *UseMO = Uses[i];
272 MachineInstr *UseMI = UseMO->getParent();
273 MachineBasicBlock *UseMBB = UseMI->getParent();
274 if (PHIBBs.count(UseMBB))
275 continue;
276
Lang Hamesc69cbd02012-02-25 02:01:00 +0000277 // About to add uses of DstReg, clear DstReg's kill flags.
Jakob Stoklund Olesen418a3632012-05-20 18:42:55 +0000278 if (!Changed) {
Lang Hamesc69cbd02012-02-25 02:01:00 +0000279 MRI->clearKillFlags(DstReg);
Jakob Stoklund Olesen418a3632012-05-20 18:42:55 +0000280 MRI->constrainRegClass(DstReg, DstRC);
281 }
Lang Hamesc69cbd02012-02-25 02:01:00 +0000282
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000283 unsigned NewVR = MRI->createVirtualRegister(RC);
Jakob Stoklund Olesen71642882012-06-19 21:14:34 +0000284 MachineInstr *Copy = BuildMI(*UseMBB, UseMI, UseMI->getDebugLoc(),
285 TII->get(TargetOpcode::COPY), NewVR)
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000286 .addReg(DstReg, 0, SubIdx);
Jakob Stoklund Olesen71642882012-06-19 21:14:34 +0000287 // SubIdx applies to both SrcReg and DstReg when UseSrcSubIdx is set.
288 if (UseSrcSubIdx) {
289 Copy->getOperand(0).setSubReg(SubIdx);
290 Copy->getOperand(0).setIsUndef();
291 }
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000292 UseMO->setReg(NewVR);
293 ++NumReuse;
294 Changed = true;
295 }
296 }
297
298 return Changed;
299}
300
Jim Grosbach39cc5132012-05-01 23:21:41 +0000301/// optimizeBitcastInstr - If the instruction is a bitcast instruction A that
Evan Chengd158fba2011-03-15 05:13:13 +0000302/// cannot be optimized away during isel (e.g. ARM::VMOVSR, which bitcast
303/// a value cross register classes), and the source is defined by another
304/// bitcast instruction B. And if the register class of source of B matches
305/// the register class of instruction A, then it is legal to replace all uses
306/// of the def of A with source of B. e.g.
307/// %vreg0<def> = VMOVSR %vreg1
308/// %vreg3<def> = VMOVRS %vreg0
309/// Replace all uses of vreg3 with vreg1.
310
Jim Grosbach39cc5132012-05-01 23:21:41 +0000311bool PeepholeOptimizer::optimizeBitcastInstr(MachineInstr *MI,
Evan Chengd158fba2011-03-15 05:13:13 +0000312 MachineBasicBlock *MBB) {
313 unsigned NumDefs = MI->getDesc().getNumDefs();
314 unsigned NumSrcs = MI->getDesc().getNumOperands() - NumDefs;
315 if (NumDefs != 1)
316 return false;
317
318 unsigned Def = 0;
319 unsigned Src = 0;
320 for (unsigned i = 0, e = NumDefs + NumSrcs; i != e; ++i) {
321 const MachineOperand &MO = MI->getOperand(i);
322 if (!MO.isReg())
323 continue;
324 unsigned Reg = MO.getReg();
325 if (!Reg)
326 continue;
327 if (MO.isDef())
328 Def = Reg;
329 else if (Src)
330 // Multiple sources?
331 return false;
332 else
333 Src = Reg;
334 }
335
336 assert(Def && Src && "Malformed bitcast instruction!");
337
338 MachineInstr *DefMI = MRI->getVRegDef(Src);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000339 if (!DefMI || !DefMI->isBitcast())
Evan Chengd158fba2011-03-15 05:13:13 +0000340 return false;
341
Evan Chengd158fba2011-03-15 05:13:13 +0000342 unsigned SrcSrc = 0;
343 NumDefs = DefMI->getDesc().getNumDefs();
344 NumSrcs = DefMI->getDesc().getNumOperands() - NumDefs;
345 if (NumDefs != 1)
346 return false;
347 for (unsigned i = 0, e = NumDefs + NumSrcs; i != e; ++i) {
348 const MachineOperand &MO = DefMI->getOperand(i);
349 if (!MO.isReg() || MO.isDef())
350 continue;
351 unsigned Reg = MO.getReg();
352 if (!Reg)
353 continue;
Duncan Sands7becbc42011-07-26 15:05:06 +0000354 if (!MO.isDef()) {
355 if (SrcSrc)
356 // Multiple sources?
357 return false;
358 else
359 SrcSrc = Reg;
360 }
Evan Chengd158fba2011-03-15 05:13:13 +0000361 }
362
363 if (MRI->getRegClass(SrcSrc) != MRI->getRegClass(Def))
364 return false;
365
366 MRI->replaceRegWith(Def, SrcSrc);
367 MRI->clearKillFlags(SrcSrc);
368 MI->eraseFromParent();
369 ++NumBitcasts;
370 return true;
371}
372
Jim Grosbach39cc5132012-05-01 23:21:41 +0000373/// optimizeCmpInstr - If the instruction is a compare and the previous
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000374/// instruction it's comparing against all ready sets (or could be modified to
375/// set) the same flag as the compare, then we can remove the comparison and use
376/// the flag from the previous instruction.
Jim Grosbach39cc5132012-05-01 23:21:41 +0000377bool PeepholeOptimizer::optimizeCmpInstr(MachineInstr *MI,
Evan Chengd158fba2011-03-15 05:13:13 +0000378 MachineBasicBlock *MBB) {
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000379 // If this instruction is a comparison against zero and isn't comparing a
380 // physical register, we can try to optimize it.
Manman Rende7266c2012-06-29 21:33:59 +0000381 unsigned SrcReg, SrcReg2;
Gabor Greif04ac81d2010-09-21 12:01:15 +0000382 int CmpMask, CmpValue;
Manman Rende7266c2012-06-29 21:33:59 +0000383 if (!TII->analyzeCompare(MI, SrcReg, SrcReg2, CmpMask, CmpValue) ||
384 TargetRegisterInfo::isPhysicalRegister(SrcReg) ||
385 (SrcReg2 != 0 && TargetRegisterInfo::isPhysicalRegister(SrcReg2)))
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000386 return false;
387
Bill Wendlinga6556862010-09-11 00:13:50 +0000388 // Attempt to optimize the comparison instruction.
Manman Rende7266c2012-06-29 21:33:59 +0000389 if (TII->optimizeCompareInstr(MI, SrcReg, SrcReg2, CmpMask, CmpValue, MRI)) {
Evan Chengd158fba2011-03-15 05:13:13 +0000390 ++NumCmps;
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000391 return true;
392 }
393
394 return false;
395}
396
Jakob Stoklund Olesenf2c64ef2012-08-16 23:11:47 +0000397/// Optimize a select instruction.
398bool PeepholeOptimizer::optimizeSelect(MachineInstr *MI) {
399 unsigned TrueOp = 0;
400 unsigned FalseOp = 0;
401 bool Optimizable = false;
402 SmallVector<MachineOperand, 4> Cond;
403 if (TII->analyzeSelect(MI, Cond, TrueOp, FalseOp, Optimizable))
404 return false;
405 if (!Optimizable)
406 return false;
407 if (!TII->optimizeSelect(MI))
408 return false;
409 MI->eraseFromParent();
410 ++NumSelects;
411 return true;
412}
413
Manman Rend7d003c2012-08-02 00:56:42 +0000414/// isLoadFoldable - Check whether MI is a candidate for folding into a later
415/// instruction. We only fold loads to virtual registers and the virtual
416/// register defined has a single use.
417bool PeepholeOptimizer::isLoadFoldable(MachineInstr *MI,
418 unsigned &FoldAsLoadDefReg) {
Manman Ren127eea82012-08-02 19:37:32 +0000419 if (!MI->canFoldAsLoad() || !MI->mayLoad())
420 return false;
421 const MCInstrDesc &MCID = MI->getDesc();
422 if (MCID.getNumDefs() != 1)
423 return false;
424
425 unsigned Reg = MI->getOperand(0).getReg();
426 // To reduce compilation time, we check MRI->hasOneUse when inserting
427 // loads. It should be checked when processing uses of the load, since
428 // uses can be removed during peephole.
429 if (!MI->getOperand(0).getSubReg() &&
430 TargetRegisterInfo::isVirtualRegister(Reg) &&
431 MRI->hasOneUse(Reg)) {
432 FoldAsLoadDefReg = Reg;
433 return true;
Manman Rend7d003c2012-08-02 00:56:42 +0000434 }
435 return false;
436}
437
Evan Chengc4af4632010-11-17 20:13:28 +0000438bool PeepholeOptimizer::isMoveImmediate(MachineInstr *MI,
439 SmallSet<unsigned, 4> &ImmDefRegs,
440 DenseMap<unsigned, MachineInstr*> &ImmDefMIs) {
Evan Chenge837dea2011-06-28 19:10:37 +0000441 const MCInstrDesc &MCID = MI->getDesc();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000442 if (!MI->isMoveImmediate())
Evan Chengc4af4632010-11-17 20:13:28 +0000443 return false;
Evan Chenge837dea2011-06-28 19:10:37 +0000444 if (MCID.getNumDefs() != 1)
Evan Chengc4af4632010-11-17 20:13:28 +0000445 return false;
446 unsigned Reg = MI->getOperand(0).getReg();
447 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
448 ImmDefMIs.insert(std::make_pair(Reg, MI));
449 ImmDefRegs.insert(Reg);
450 return true;
451 }
Andrew Trick1df91b02012-02-08 21:22:43 +0000452
Evan Chengc4af4632010-11-17 20:13:28 +0000453 return false;
454}
455
Jim Grosbach39cc5132012-05-01 23:21:41 +0000456/// foldImmediate - Try folding register operands that are defined by move
Evan Chengc4af4632010-11-17 20:13:28 +0000457/// immediate instructions, i.e. a trivial constant folding optimization, if
458/// and only if the def and use are in the same BB.
Jim Grosbach39cc5132012-05-01 23:21:41 +0000459bool PeepholeOptimizer::foldImmediate(MachineInstr *MI, MachineBasicBlock *MBB,
Evan Chengc4af4632010-11-17 20:13:28 +0000460 SmallSet<unsigned, 4> &ImmDefRegs,
461 DenseMap<unsigned, MachineInstr*> &ImmDefMIs) {
462 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
463 MachineOperand &MO = MI->getOperand(i);
464 if (!MO.isReg() || MO.isDef())
465 continue;
466 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000467 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Evan Chengc4af4632010-11-17 20:13:28 +0000468 continue;
469 if (ImmDefRegs.count(Reg) == 0)
470 continue;
471 DenseMap<unsigned, MachineInstr*>::iterator II = ImmDefMIs.find(Reg);
472 assert(II != ImmDefMIs.end());
473 if (TII->FoldImmediate(MI, II->second, Reg, MRI)) {
474 ++NumImmFold;
475 return true;
476 }
477 }
478 return false;
479}
480
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000481bool PeepholeOptimizer::runOnMachineFunction(MachineFunction &MF) {
Craig Toppera1032b72012-12-17 03:56:00 +0000482 DEBUG(dbgs() << "********** PEEPHOLE OPTIMIZER **********\n");
483 DEBUG(dbgs() << "********** Function: " << MF.getName() << '\n');
484
Evan Chengeb96a2f2010-11-15 21:20:45 +0000485 if (DisablePeephole)
486 return false;
Andrew Trick1df91b02012-02-08 21:22:43 +0000487
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000488 TM = &MF.getTarget();
489 TII = TM->getInstrInfo();
490 MRI = &MF.getRegInfo();
491 DT = Aggressive ? &getAnalysis<MachineDominatorTree>() : 0;
492
493 bool Changed = false;
494
495 SmallPtrSet<MachineInstr*, 8> LocalMIs;
Evan Chengc4af4632010-11-17 20:13:28 +0000496 SmallSet<unsigned, 4> ImmDefRegs;
497 DenseMap<unsigned, MachineInstr*> ImmDefMIs;
Manman Rend7d003c2012-08-02 00:56:42 +0000498 unsigned FoldAsLoadDefReg;
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000499 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
500 MachineBasicBlock *MBB = &*I;
Andrew Trick1df91b02012-02-08 21:22:43 +0000501
Evan Chengc4af4632010-11-17 20:13:28 +0000502 bool SeenMoveImm = false;
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000503 LocalMIs.clear();
Evan Chengc4af4632010-11-17 20:13:28 +0000504 ImmDefRegs.clear();
505 ImmDefMIs.clear();
Manman Rend7d003c2012-08-02 00:56:42 +0000506 FoldAsLoadDefReg = 0;
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000507
508 for (MachineBasicBlock::iterator
Bill Wendling220e2402010-09-10 21:55:43 +0000509 MII = I->begin(), MIE = I->end(); MII != MIE; ) {
Evan Chengcf75ab52011-02-14 21:50:37 +0000510 MachineInstr *MI = &*MII;
Jakob Stoklund Olesencabc0692012-08-17 14:38:59 +0000511 // We may be erasing MI below, increment MII now.
512 ++MII;
Evan Chengeb96a2f2010-11-15 21:20:45 +0000513 LocalMIs.insert(MI);
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000514
Manman Rend7d003c2012-08-02 00:56:42 +0000515 // If there exists an instruction which belongs to the following
516 // categories, we will discard the load candidate.
Evan Cheng30a343a2011-01-07 21:08:26 +0000517 if (MI->isLabel() || MI->isPHI() || MI->isImplicitDef() ||
518 MI->isKill() || MI->isInlineAsm() || MI->isDebugValue() ||
Evan Chengcf75ab52011-02-14 21:50:37 +0000519 MI->hasUnmodeledSideEffects()) {
Manman Rend7d003c2012-08-02 00:56:42 +0000520 FoldAsLoadDefReg = 0;
Evan Chengeb96a2f2010-11-15 21:20:45 +0000521 continue;
Evan Chengcf75ab52011-02-14 21:50:37 +0000522 }
Manman Rend7d003c2012-08-02 00:56:42 +0000523 if (MI->mayStore() || MI->isCall())
524 FoldAsLoadDefReg = 0;
Evan Chengeb96a2f2010-11-15 21:20:45 +0000525
Jakob Stoklund Olesenf2c64ef2012-08-16 23:11:47 +0000526 if ((MI->isBitcast() && optimizeBitcastInstr(MI, MBB)) ||
527 (MI->isCompare() && optimizeCmpInstr(MI, MBB)) ||
528 (MI->isSelect() && optimizeSelect(MI))) {
529 // MI is deleted.
530 LocalMIs.erase(MI);
531 Changed = true;
Jakob Stoklund Olesenf2c64ef2012-08-16 23:11:47 +0000532 continue;
Evan Chengcf75ab52011-02-14 21:50:37 +0000533 }
534
535 if (isMoveImmediate(MI, ImmDefRegs, ImmDefMIs)) {
Evan Chengc4af4632010-11-17 20:13:28 +0000536 SeenMoveImm = true;
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000537 } else {
Jim Grosbach39cc5132012-05-01 23:21:41 +0000538 Changed |= optimizeExtInstr(MI, MBB, LocalMIs);
Rafael Espindola10ad98b2012-10-15 18:21:07 +0000539 // optimizeExtInstr might have created new instructions after MI
540 // and before the already incremented MII. Adjust MII so that the
541 // next iteration sees the new instructions.
542 MII = MI;
543 ++MII;
Evan Chengc4af4632010-11-17 20:13:28 +0000544 if (SeenMoveImm)
Jim Grosbach39cc5132012-05-01 23:21:41 +0000545 Changed |= foldImmediate(MI, MBB, ImmDefRegs, ImmDefMIs);
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000546 }
Evan Cheng326d9762011-02-15 05:00:24 +0000547
Manman Rend7d003c2012-08-02 00:56:42 +0000548 // Check whether MI is a load candidate for folding into a later
549 // instruction. If MI is not a candidate, check whether we can fold an
550 // earlier load into MI.
551 if (!isLoadFoldable(MI, FoldAsLoadDefReg) && FoldAsLoadDefReg) {
552 // We need to fold load after optimizeCmpInstr, since optimizeCmpInstr
553 // can enable folding by converting SUB to CMP.
554 MachineInstr *DefMI = 0;
555 MachineInstr *FoldMI = TII->optimizeLoadInstr(MI, MRI,
556 FoldAsLoadDefReg, DefMI);
557 if (FoldMI) {
558 // Update LocalMIs since we replaced MI with FoldMI and deleted DefMI.
Craig Toppera1032b72012-12-17 03:56:00 +0000559 DEBUG(dbgs() << "Replacing: " << *MI);
560 DEBUG(dbgs() << " With: " << *FoldMI);
Manman Rend7d003c2012-08-02 00:56:42 +0000561 LocalMIs.erase(MI);
562 LocalMIs.erase(DefMI);
563 LocalMIs.insert(FoldMI);
564 MI->eraseFromParent();
565 DefMI->eraseFromParent();
566 ++NumLoadFold;
567
568 // MI is replaced with FoldMI.
569 Changed = true;
Manman Rend7d003c2012-08-02 00:56:42 +0000570 continue;
571 }
572 }
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000573 }
574 }
575
576 return Changed;
577}