blob: ab672c9c585536747f0575da9ddd853af82d7514 [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//
Bill Wendling6cdb1ab2010-08-09 23:59:04 +000052//===----------------------------------------------------------------------===//
53
54#define DEBUG_TYPE "peephole-opt"
55#include "llvm/CodeGen/Passes.h"
56#include "llvm/CodeGen/MachineDominators.h"
57#include "llvm/CodeGen/MachineInstrBuilder.h"
58#include "llvm/CodeGen/MachineRegisterInfo.h"
59#include "llvm/Target/TargetInstrInfo.h"
60#include "llvm/Target/TargetRegisterInfo.h"
61#include "llvm/Support/CommandLine.h"
Evan Chengc4af4632010-11-17 20:13:28 +000062#include "llvm/ADT/DenseMap.h"
Bill Wendling6cdb1ab2010-08-09 23:59:04 +000063#include "llvm/ADT/SmallPtrSet.h"
Evan Chengc4af4632010-11-17 20:13:28 +000064#include "llvm/ADT/SmallSet.h"
Bill Wendling6cdb1ab2010-08-09 23:59:04 +000065#include "llvm/ADT/Statistic.h"
66using namespace llvm;
67
68// Optimize Extensions
69static cl::opt<bool>
70Aggressive("aggressive-ext-opt", cl::Hidden,
71 cl::desc("Aggressive extension optimization"));
72
Bill Wendling40a5eb12010-11-01 20:41:43 +000073static cl::opt<bool>
74DisablePeephole("disable-peephole", cl::Hidden, cl::init(false),
75 cl::desc("Disable the peephole optimizer"));
76
Bill Wendling69c5eb52010-08-27 20:39:09 +000077STATISTIC(NumReuse, "Number of extension results reused");
Evan Chengd158fba2011-03-15 05:13:13 +000078STATISTIC(NumBitcasts, "Number of bitcasts eliminated");
79STATISTIC(NumCmps, "Number of compares eliminated");
Lang Hames3b26eb62012-02-25 00:46:38 +000080STATISTIC(NumImmFold, "Number of move immediate folded");
Bill Wendling6cdb1ab2010-08-09 23:59:04 +000081
82namespace {
83 class PeepholeOptimizer : public MachineFunctionPass {
84 const TargetMachine *TM;
85 const TargetInstrInfo *TII;
86 MachineRegisterInfo *MRI;
87 MachineDominatorTree *DT; // Machine dominator tree
88
89 public:
90 static char ID; // Pass identification
Owen Anderson081c34b2010-10-19 17:21:58 +000091 PeepholeOptimizer() : MachineFunctionPass(ID) {
92 initializePeepholeOptimizerPass(*PassRegistry::getPassRegistry());
93 }
Bill Wendling6cdb1ab2010-08-09 23:59:04 +000094
95 virtual bool runOnMachineFunction(MachineFunction &MF);
96
97 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
98 AU.setPreservesCFG();
99 MachineFunctionPass::getAnalysisUsage(AU);
100 if (Aggressive) {
101 AU.addRequired<MachineDominatorTree>();
102 AU.addPreserved<MachineDominatorTree>();
103 }
104 }
105
106 private:
Jim Grosbach39cc5132012-05-01 23:21:41 +0000107 bool optimizeBitcastInstr(MachineInstr *MI, MachineBasicBlock *MBB);
108 bool optimizeCmpInstr(MachineInstr *MI, MachineBasicBlock *MBB);
109 bool optimizeExtInstr(MachineInstr *MI, MachineBasicBlock *MBB,
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000110 SmallPtrSet<MachineInstr*, 8> &LocalMIs);
Evan Chengc4af4632010-11-17 20:13:28 +0000111 bool isMoveImmediate(MachineInstr *MI,
112 SmallSet<unsigned, 4> &ImmDefRegs,
113 DenseMap<unsigned, MachineInstr*> &ImmDefMIs);
Jim Grosbach39cc5132012-05-01 23:21:41 +0000114 bool foldImmediate(MachineInstr *MI, MachineBasicBlock *MBB,
Evan Chengc4af4632010-11-17 20:13:28 +0000115 SmallSet<unsigned, 4> &ImmDefRegs,
116 DenseMap<unsigned, MachineInstr*> &ImmDefMIs);
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000117 };
118}
119
120char PeepholeOptimizer::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +0000121char &llvm::PeepholeOptimizerID = PeepholeOptimizer::ID;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000122INITIALIZE_PASS_BEGIN(PeepholeOptimizer, "peephole-opts",
123 "Peephole Optimizations", false, false)
124INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
125INITIALIZE_PASS_END(PeepholeOptimizer, "peephole-opts",
Owen Andersonce665bd2010-10-07 22:25:06 +0000126 "Peephole Optimizations", false, false)
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000127
Jim Grosbach39cc5132012-05-01 23:21:41 +0000128/// optimizeExtInstr - If instruction is a copy-like instruction, i.e. it reads
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000129/// a single register and writes a single register and it does not modify the
130/// source, and if the source value is preserved as a sub-register of the
131/// result, then replace all reachable uses of the source with the subreg of the
132/// result.
Andrew Trick1df91b02012-02-08 21:22:43 +0000133///
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000134/// Do not generate an EXTRACT that is used only in a debug use, as this changes
135/// the code. Since this code does not currently share EXTRACTs, just ignore all
136/// debug uses.
137bool PeepholeOptimizer::
Jim Grosbach39cc5132012-05-01 23:21:41 +0000138optimizeExtInstr(MachineInstr *MI, MachineBasicBlock *MBB,
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000139 SmallPtrSet<MachineInstr*, 8> &LocalMIs) {
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000140 unsigned SrcReg, DstReg, SubIdx;
141 if (!TII->isCoalescableExtInstr(*MI, SrcReg, DstReg, SubIdx))
142 return false;
Andrew Trick1df91b02012-02-08 21:22:43 +0000143
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000144 if (TargetRegisterInfo::isPhysicalRegister(DstReg) ||
145 TargetRegisterInfo::isPhysicalRegister(SrcReg))
146 return false;
147
148 MachineRegisterInfo::use_nodbg_iterator UI = MRI->use_nodbg_begin(SrcReg);
149 if (++UI == MRI->use_nodbg_end())
150 // No other uses.
151 return false;
152
153 // The source has other uses. See if we can replace the other uses with use of
154 // the result of the extension.
155 SmallPtrSet<MachineBasicBlock*, 4> ReachedBBs;
156 UI = MRI->use_nodbg_begin(DstReg);
157 for (MachineRegisterInfo::use_nodbg_iterator UE = MRI->use_nodbg_end();
158 UI != UE; ++UI)
159 ReachedBBs.insert(UI->getParent());
160
161 // Uses that are in the same BB of uses of the result of the instruction.
162 SmallVector<MachineOperand*, 8> Uses;
163
164 // Uses that the result of the instruction can reach.
165 SmallVector<MachineOperand*, 8> ExtendedUses;
166
167 bool ExtendLife = true;
168 UI = MRI->use_nodbg_begin(SrcReg);
169 for (MachineRegisterInfo::use_nodbg_iterator UE = MRI->use_nodbg_end();
170 UI != UE; ++UI) {
171 MachineOperand &UseMO = UI.getOperand();
172 MachineInstr *UseMI = &*UI;
173 if (UseMI == MI)
174 continue;
175
176 if (UseMI->isPHI()) {
177 ExtendLife = false;
178 continue;
179 }
180
181 // It's an error to translate this:
182 //
183 // %reg1025 = <sext> %reg1024
184 // ...
185 // %reg1026 = SUBREG_TO_REG 0, %reg1024, 4
186 //
187 // into this:
188 //
189 // %reg1025 = <sext> %reg1024
190 // ...
191 // %reg1027 = COPY %reg1025:4
192 // %reg1026 = SUBREG_TO_REG 0, %reg1027, 4
193 //
194 // The problem here is that SUBREG_TO_REG is there to assert that an
195 // implicit zext occurs. It doesn't insert a zext instruction. If we allow
196 // the COPY here, it will give us the value after the <sext>, not the
197 // original value of %reg1024 before <sext>.
198 if (UseMI->getOpcode() == TargetOpcode::SUBREG_TO_REG)
199 continue;
200
201 MachineBasicBlock *UseMBB = UseMI->getParent();
202 if (UseMBB == MBB) {
203 // Local uses that come after the extension.
204 if (!LocalMIs.count(UseMI))
205 Uses.push_back(&UseMO);
206 } else if (ReachedBBs.count(UseMBB)) {
207 // Non-local uses where the result of the extension is used. Always
208 // replace these unless it's a PHI.
209 Uses.push_back(&UseMO);
210 } else if (Aggressive && DT->dominates(MBB, UseMBB)) {
211 // We may want to extend the live range of the extension result in order
212 // to replace these uses.
213 ExtendedUses.push_back(&UseMO);
214 } else {
215 // Both will be live out of the def MBB anyway. Don't extend live range of
216 // the extension result.
217 ExtendLife = false;
218 break;
219 }
220 }
221
222 if (ExtendLife && !ExtendedUses.empty())
223 // Extend the liveness of the extension result.
224 std::copy(ExtendedUses.begin(), ExtendedUses.end(),
225 std::back_inserter(Uses));
226
227 // Now replace all uses.
228 bool Changed = false;
229 if (!Uses.empty()) {
230 SmallPtrSet<MachineBasicBlock*, 4> PHIBBs;
231
232 // Look for PHI uses of the extended result, we don't want to extend the
233 // liveness of a PHI input. It breaks all kinds of assumptions down
234 // stream. A PHI use is expected to be the kill of its source values.
235 UI = MRI->use_nodbg_begin(DstReg);
236 for (MachineRegisterInfo::use_nodbg_iterator
237 UE = MRI->use_nodbg_end(); UI != UE; ++UI)
238 if (UI->isPHI())
239 PHIBBs.insert(UI->getParent());
240
241 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
242 for (unsigned i = 0, e = Uses.size(); i != e; ++i) {
243 MachineOperand *UseMO = Uses[i];
244 MachineInstr *UseMI = UseMO->getParent();
245 MachineBasicBlock *UseMBB = UseMI->getParent();
246 if (PHIBBs.count(UseMBB))
247 continue;
248
Lang Hamesc69cbd02012-02-25 02:01:00 +0000249 // About to add uses of DstReg, clear DstReg's kill flags.
250 if (!Changed)
251 MRI->clearKillFlags(DstReg);
252
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000253 unsigned NewVR = MRI->createVirtualRegister(RC);
254 BuildMI(*UseMBB, UseMI, UseMI->getDebugLoc(),
255 TII->get(TargetOpcode::COPY), NewVR)
256 .addReg(DstReg, 0, SubIdx);
257
258 UseMO->setReg(NewVR);
259 ++NumReuse;
260 Changed = true;
261 }
262 }
263
264 return Changed;
265}
266
Jim Grosbach39cc5132012-05-01 23:21:41 +0000267/// optimizeBitcastInstr - If the instruction is a bitcast instruction A that
Evan Chengd158fba2011-03-15 05:13:13 +0000268/// cannot be optimized away during isel (e.g. ARM::VMOVSR, which bitcast
269/// a value cross register classes), and the source is defined by another
270/// bitcast instruction B. And if the register class of source of B matches
271/// the register class of instruction A, then it is legal to replace all uses
272/// of the def of A with source of B. e.g.
273/// %vreg0<def> = VMOVSR %vreg1
274/// %vreg3<def> = VMOVRS %vreg0
275/// Replace all uses of vreg3 with vreg1.
276
Jim Grosbach39cc5132012-05-01 23:21:41 +0000277bool PeepholeOptimizer::optimizeBitcastInstr(MachineInstr *MI,
Evan Chengd158fba2011-03-15 05:13:13 +0000278 MachineBasicBlock *MBB) {
279 unsigned NumDefs = MI->getDesc().getNumDefs();
280 unsigned NumSrcs = MI->getDesc().getNumOperands() - NumDefs;
281 if (NumDefs != 1)
282 return false;
283
284 unsigned Def = 0;
285 unsigned Src = 0;
286 for (unsigned i = 0, e = NumDefs + NumSrcs; i != e; ++i) {
287 const MachineOperand &MO = MI->getOperand(i);
288 if (!MO.isReg())
289 continue;
290 unsigned Reg = MO.getReg();
291 if (!Reg)
292 continue;
293 if (MO.isDef())
294 Def = Reg;
295 else if (Src)
296 // Multiple sources?
297 return false;
298 else
299 Src = Reg;
300 }
301
302 assert(Def && Src && "Malformed bitcast instruction!");
303
304 MachineInstr *DefMI = MRI->getVRegDef(Src);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000305 if (!DefMI || !DefMI->isBitcast())
Evan Chengd158fba2011-03-15 05:13:13 +0000306 return false;
307
Evan Chengd158fba2011-03-15 05:13:13 +0000308 unsigned SrcSrc = 0;
309 NumDefs = DefMI->getDesc().getNumDefs();
310 NumSrcs = DefMI->getDesc().getNumOperands() - NumDefs;
311 if (NumDefs != 1)
312 return false;
313 for (unsigned i = 0, e = NumDefs + NumSrcs; i != e; ++i) {
314 const MachineOperand &MO = DefMI->getOperand(i);
315 if (!MO.isReg() || MO.isDef())
316 continue;
317 unsigned Reg = MO.getReg();
318 if (!Reg)
319 continue;
Duncan Sands7becbc42011-07-26 15:05:06 +0000320 if (!MO.isDef()) {
321 if (SrcSrc)
322 // Multiple sources?
323 return false;
324 else
325 SrcSrc = Reg;
326 }
Evan Chengd158fba2011-03-15 05:13:13 +0000327 }
328
329 if (MRI->getRegClass(SrcSrc) != MRI->getRegClass(Def))
330 return false;
331
332 MRI->replaceRegWith(Def, SrcSrc);
333 MRI->clearKillFlags(SrcSrc);
334 MI->eraseFromParent();
335 ++NumBitcasts;
336 return true;
337}
338
Jim Grosbach39cc5132012-05-01 23:21:41 +0000339/// optimizeCmpInstr - If the instruction is a compare and the previous
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000340/// instruction it's comparing against all ready sets (or could be modified to
341/// set) the same flag as the compare, then we can remove the comparison and use
342/// the flag from the previous instruction.
Jim Grosbach39cc5132012-05-01 23:21:41 +0000343bool PeepholeOptimizer::optimizeCmpInstr(MachineInstr *MI,
Evan Chengd158fba2011-03-15 05:13:13 +0000344 MachineBasicBlock *MBB) {
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000345 // If this instruction is a comparison against zero and isn't comparing a
346 // physical register, we can try to optimize it.
347 unsigned SrcReg;
Gabor Greif04ac81d2010-09-21 12:01:15 +0000348 int CmpMask, CmpValue;
349 if (!TII->AnalyzeCompare(MI, SrcReg, CmpMask, CmpValue) ||
Bill Wendling92ad57f2010-09-10 23:34:19 +0000350 TargetRegisterInfo::isPhysicalRegister(SrcReg))
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000351 return false;
352
Bill Wendlinga6556862010-09-11 00:13:50 +0000353 // Attempt to optimize the comparison instruction.
Evan Chengeb96a2f2010-11-15 21:20:45 +0000354 if (TII->OptimizeCompareInstr(MI, SrcReg, CmpMask, CmpValue, MRI)) {
Evan Chengd158fba2011-03-15 05:13:13 +0000355 ++NumCmps;
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000356 return true;
357 }
358
359 return false;
360}
361
Evan Chengc4af4632010-11-17 20:13:28 +0000362bool PeepholeOptimizer::isMoveImmediate(MachineInstr *MI,
363 SmallSet<unsigned, 4> &ImmDefRegs,
364 DenseMap<unsigned, MachineInstr*> &ImmDefMIs) {
Evan Chenge837dea2011-06-28 19:10:37 +0000365 const MCInstrDesc &MCID = MI->getDesc();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000366 if (!MI->isMoveImmediate())
Evan Chengc4af4632010-11-17 20:13:28 +0000367 return false;
Evan Chenge837dea2011-06-28 19:10:37 +0000368 if (MCID.getNumDefs() != 1)
Evan Chengc4af4632010-11-17 20:13:28 +0000369 return false;
370 unsigned Reg = MI->getOperand(0).getReg();
371 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
372 ImmDefMIs.insert(std::make_pair(Reg, MI));
373 ImmDefRegs.insert(Reg);
374 return true;
375 }
Andrew Trick1df91b02012-02-08 21:22:43 +0000376
Evan Chengc4af4632010-11-17 20:13:28 +0000377 return false;
378}
379
Jim Grosbach39cc5132012-05-01 23:21:41 +0000380/// foldImmediate - Try folding register operands that are defined by move
Evan Chengc4af4632010-11-17 20:13:28 +0000381/// immediate instructions, i.e. a trivial constant folding optimization, if
382/// and only if the def and use are in the same BB.
Jim Grosbach39cc5132012-05-01 23:21:41 +0000383bool PeepholeOptimizer::foldImmediate(MachineInstr *MI, MachineBasicBlock *MBB,
Evan Chengc4af4632010-11-17 20:13:28 +0000384 SmallSet<unsigned, 4> &ImmDefRegs,
385 DenseMap<unsigned, MachineInstr*> &ImmDefMIs) {
386 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
387 MachineOperand &MO = MI->getOperand(i);
388 if (!MO.isReg() || MO.isDef())
389 continue;
390 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000391 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Evan Chengc4af4632010-11-17 20:13:28 +0000392 continue;
393 if (ImmDefRegs.count(Reg) == 0)
394 continue;
395 DenseMap<unsigned, MachineInstr*>::iterator II = ImmDefMIs.find(Reg);
396 assert(II != ImmDefMIs.end());
397 if (TII->FoldImmediate(MI, II->second, Reg, MRI)) {
398 ++NumImmFold;
399 return true;
400 }
401 }
402 return false;
403}
404
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000405bool PeepholeOptimizer::runOnMachineFunction(MachineFunction &MF) {
Evan Chengeb96a2f2010-11-15 21:20:45 +0000406 if (DisablePeephole)
407 return false;
Andrew Trick1df91b02012-02-08 21:22:43 +0000408
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000409 TM = &MF.getTarget();
410 TII = TM->getInstrInfo();
411 MRI = &MF.getRegInfo();
412 DT = Aggressive ? &getAnalysis<MachineDominatorTree>() : 0;
413
414 bool Changed = false;
415
416 SmallPtrSet<MachineInstr*, 8> LocalMIs;
Evan Chengc4af4632010-11-17 20:13:28 +0000417 SmallSet<unsigned, 4> ImmDefRegs;
418 DenseMap<unsigned, MachineInstr*> ImmDefMIs;
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000419 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
420 MachineBasicBlock *MBB = &*I;
Andrew Trick1df91b02012-02-08 21:22:43 +0000421
Evan Chengc4af4632010-11-17 20:13:28 +0000422 bool SeenMoveImm = false;
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000423 LocalMIs.clear();
Evan Chengc4af4632010-11-17 20:13:28 +0000424 ImmDefRegs.clear();
425 ImmDefMIs.clear();
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000426
Evan Cheng326d9762011-02-15 05:00:24 +0000427 bool First = true;
428 MachineBasicBlock::iterator PMII;
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000429 for (MachineBasicBlock::iterator
Bill Wendling220e2402010-09-10 21:55:43 +0000430 MII = I->begin(), MIE = I->end(); MII != MIE; ) {
Evan Chengcf75ab52011-02-14 21:50:37 +0000431 MachineInstr *MI = &*MII;
Evan Chengeb96a2f2010-11-15 21:20:45 +0000432 LocalMIs.insert(MI);
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000433
Evan Cheng30a343a2011-01-07 21:08:26 +0000434 if (MI->isLabel() || MI->isPHI() || MI->isImplicitDef() ||
435 MI->isKill() || MI->isInlineAsm() || MI->isDebugValue() ||
Evan Chengcf75ab52011-02-14 21:50:37 +0000436 MI->hasUnmodeledSideEffects()) {
437 ++MII;
Evan Chengeb96a2f2010-11-15 21:20:45 +0000438 continue;
Evan Chengcf75ab52011-02-14 21:50:37 +0000439 }
Evan Chengeb96a2f2010-11-15 21:20:45 +0000440
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000441 if (MI->isBitcast()) {
Jim Grosbach39cc5132012-05-01 23:21:41 +0000442 if (optimizeBitcastInstr(MI, MBB)) {
Evan Chengd158fba2011-03-15 05:13:13 +0000443 // MI is deleted.
Nick Lewyckydec1b102011-10-13 02:16:18 +0000444 LocalMIs.erase(MI);
Evan Chengd158fba2011-03-15 05:13:13 +0000445 Changed = true;
446 MII = First ? I->begin() : llvm::next(PMII);
447 continue;
Andrew Trick1df91b02012-02-08 21:22:43 +0000448 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000449 } else if (MI->isCompare()) {
Jim Grosbach39cc5132012-05-01 23:21:41 +0000450 if (optimizeCmpInstr(MI, MBB)) {
Evan Chengcf75ab52011-02-14 21:50:37 +0000451 // MI is deleted.
Nick Lewyckydec1b102011-10-13 02:16:18 +0000452 LocalMIs.erase(MI);
Evan Chengcf75ab52011-02-14 21:50:37 +0000453 Changed = true;
Evan Cheng326d9762011-02-15 05:00:24 +0000454 MII = First ? I->begin() : llvm::next(PMII);
Evan Chengcf75ab52011-02-14 21:50:37 +0000455 continue;
456 }
457 }
458
459 if (isMoveImmediate(MI, ImmDefRegs, ImmDefMIs)) {
Evan Chengc4af4632010-11-17 20:13:28 +0000460 SeenMoveImm = true;
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000461 } else {
Jim Grosbach39cc5132012-05-01 23:21:41 +0000462 Changed |= optimizeExtInstr(MI, MBB, LocalMIs);
Evan Chengc4af4632010-11-17 20:13:28 +0000463 if (SeenMoveImm)
Jim Grosbach39cc5132012-05-01 23:21:41 +0000464 Changed |= foldImmediate(MI, MBB, ImmDefRegs, ImmDefMIs);
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000465 }
Evan Cheng326d9762011-02-15 05:00:24 +0000466
467 First = false;
Evan Chengcf75ab52011-02-14 21:50:37 +0000468 PMII = MII;
469 ++MII;
Bill Wendling6cdb1ab2010-08-09 23:59:04 +0000470 }
471 }
472
473 return Changed;
474}