Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 1 | //===-- 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 Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 33 | // |
Manman Ren | 247c5ab | 2012-05-11 01:30:47 +0000 | [diff] [blame] | 34 | // 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 Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 43 | // - Optimize Bitcast pairs: |
| 44 | // |
| 45 | // v1 = bitcast v0 |
| 46 | // v2 = bitcast v1 |
| 47 | // = v2 |
| 48 | // => |
| 49 | // v1 = bitcast v0 |
| 50 | // = v0 |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 51 | // |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
| 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 Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 62 | #include "llvm/ADT/DenseMap.h" |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 63 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 64 | #include "llvm/ADT/SmallSet.h" |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 65 | #include "llvm/ADT/Statistic.h" |
| 66 | using namespace llvm; |
| 67 | |
| 68 | // Optimize Extensions |
| 69 | static cl::opt<bool> |
| 70 | Aggressive("aggressive-ext-opt", cl::Hidden, |
| 71 | cl::desc("Aggressive extension optimization")); |
| 72 | |
Bill Wendling | 40a5eb1 | 2010-11-01 20:41:43 +0000 | [diff] [blame] | 73 | static cl::opt<bool> |
| 74 | DisablePeephole("disable-peephole", cl::Hidden, cl::init(false), |
| 75 | cl::desc("Disable the peephole optimizer")); |
| 76 | |
Bill Wendling | 69c5eb5 | 2010-08-27 20:39:09 +0000 | [diff] [blame] | 77 | STATISTIC(NumReuse, "Number of extension results reused"); |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 78 | STATISTIC(NumBitcasts, "Number of bitcasts eliminated"); |
| 79 | STATISTIC(NumCmps, "Number of compares eliminated"); |
Lang Hames | 3b26eb6 | 2012-02-25 00:46:38 +0000 | [diff] [blame] | 80 | STATISTIC(NumImmFold, "Number of move immediate folded"); |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 81 | |
| 82 | namespace { |
| 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 Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 91 | PeepholeOptimizer() : MachineFunctionPass(ID) { |
| 92 | initializePeepholeOptimizerPass(*PassRegistry::getPassRegistry()); |
| 93 | } |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 94 | |
| 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 Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 107 | bool optimizeBitcastInstr(MachineInstr *MI, MachineBasicBlock *MBB); |
| 108 | bool optimizeCmpInstr(MachineInstr *MI, MachineBasicBlock *MBB); |
| 109 | bool optimizeExtInstr(MachineInstr *MI, MachineBasicBlock *MBB, |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 110 | SmallPtrSet<MachineInstr*, 8> &LocalMIs); |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 111 | bool isMoveImmediate(MachineInstr *MI, |
| 112 | SmallSet<unsigned, 4> &ImmDefRegs, |
| 113 | DenseMap<unsigned, MachineInstr*> &ImmDefMIs); |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 114 | bool foldImmediate(MachineInstr *MI, MachineBasicBlock *MBB, |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 115 | SmallSet<unsigned, 4> &ImmDefRegs, |
| 116 | DenseMap<unsigned, MachineInstr*> &ImmDefMIs); |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 117 | }; |
| 118 | } |
| 119 | |
| 120 | char PeepholeOptimizer::ID = 0; |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 121 | char &llvm::PeepholeOptimizerID = PeepholeOptimizer::ID; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 122 | INITIALIZE_PASS_BEGIN(PeepholeOptimizer, "peephole-opts", |
| 123 | "Peephole Optimizations", false, false) |
| 124 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 125 | INITIALIZE_PASS_END(PeepholeOptimizer, "peephole-opts", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 126 | "Peephole Optimizations", false, false) |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 127 | |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 128 | /// optimizeExtInstr - If instruction is a copy-like instruction, i.e. it reads |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 129 | /// 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 Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 133 | /// |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 134 | /// 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. |
| 137 | bool PeepholeOptimizer:: |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 138 | optimizeExtInstr(MachineInstr *MI, MachineBasicBlock *MBB, |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 139 | SmallPtrSet<MachineInstr*, 8> &LocalMIs) { |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 140 | unsigned SrcReg, DstReg, SubIdx; |
| 141 | if (!TII->isCoalescableExtInstr(*MI, SrcReg, DstReg, SubIdx)) |
| 142 | return false; |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 143 | |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 144 | 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 | |
Jakob Stoklund Olesen | 418a363 | 2012-05-20 18:42:55 +0000 | [diff] [blame^] | 153 | // Ensure DstReg can get a register class that actually supports |
| 154 | // sub-registers. Don't change the class until we commit. |
| 155 | const TargetRegisterClass *DstRC = MRI->getRegClass(DstReg); |
| 156 | DstRC = TM->getRegisterInfo()->getSubClassWithSubReg(DstRC, SubIdx); |
| 157 | if (!DstRC) |
| 158 | return false; |
| 159 | |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 160 | // The source has other uses. See if we can replace the other uses with use of |
| 161 | // the result of the extension. |
| 162 | SmallPtrSet<MachineBasicBlock*, 4> ReachedBBs; |
| 163 | UI = MRI->use_nodbg_begin(DstReg); |
| 164 | for (MachineRegisterInfo::use_nodbg_iterator UE = MRI->use_nodbg_end(); |
| 165 | UI != UE; ++UI) |
| 166 | ReachedBBs.insert(UI->getParent()); |
| 167 | |
| 168 | // Uses that are in the same BB of uses of the result of the instruction. |
| 169 | SmallVector<MachineOperand*, 8> Uses; |
| 170 | |
| 171 | // Uses that the result of the instruction can reach. |
| 172 | SmallVector<MachineOperand*, 8> ExtendedUses; |
| 173 | |
| 174 | bool ExtendLife = true; |
| 175 | UI = MRI->use_nodbg_begin(SrcReg); |
| 176 | for (MachineRegisterInfo::use_nodbg_iterator UE = MRI->use_nodbg_end(); |
| 177 | UI != UE; ++UI) { |
| 178 | MachineOperand &UseMO = UI.getOperand(); |
| 179 | MachineInstr *UseMI = &*UI; |
| 180 | if (UseMI == MI) |
| 181 | continue; |
| 182 | |
| 183 | if (UseMI->isPHI()) { |
| 184 | ExtendLife = false; |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | // It's an error to translate this: |
| 189 | // |
| 190 | // %reg1025 = <sext> %reg1024 |
| 191 | // ... |
| 192 | // %reg1026 = SUBREG_TO_REG 0, %reg1024, 4 |
| 193 | // |
| 194 | // into this: |
| 195 | // |
| 196 | // %reg1025 = <sext> %reg1024 |
| 197 | // ... |
| 198 | // %reg1027 = COPY %reg1025:4 |
| 199 | // %reg1026 = SUBREG_TO_REG 0, %reg1027, 4 |
| 200 | // |
| 201 | // The problem here is that SUBREG_TO_REG is there to assert that an |
| 202 | // implicit zext occurs. It doesn't insert a zext instruction. If we allow |
| 203 | // the COPY here, it will give us the value after the <sext>, not the |
| 204 | // original value of %reg1024 before <sext>. |
| 205 | if (UseMI->getOpcode() == TargetOpcode::SUBREG_TO_REG) |
| 206 | continue; |
| 207 | |
| 208 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
| 209 | if (UseMBB == MBB) { |
| 210 | // Local uses that come after the extension. |
| 211 | if (!LocalMIs.count(UseMI)) |
| 212 | Uses.push_back(&UseMO); |
| 213 | } else if (ReachedBBs.count(UseMBB)) { |
| 214 | // Non-local uses where the result of the extension is used. Always |
| 215 | // replace these unless it's a PHI. |
| 216 | Uses.push_back(&UseMO); |
| 217 | } else if (Aggressive && DT->dominates(MBB, UseMBB)) { |
| 218 | // We may want to extend the live range of the extension result in order |
| 219 | // to replace these uses. |
| 220 | ExtendedUses.push_back(&UseMO); |
| 221 | } else { |
| 222 | // Both will be live out of the def MBB anyway. Don't extend live range of |
| 223 | // the extension result. |
| 224 | ExtendLife = false; |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (ExtendLife && !ExtendedUses.empty()) |
| 230 | // Extend the liveness of the extension result. |
| 231 | std::copy(ExtendedUses.begin(), ExtendedUses.end(), |
| 232 | std::back_inserter(Uses)); |
| 233 | |
| 234 | // Now replace all uses. |
| 235 | bool Changed = false; |
| 236 | if (!Uses.empty()) { |
| 237 | SmallPtrSet<MachineBasicBlock*, 4> PHIBBs; |
| 238 | |
| 239 | // Look for PHI uses of the extended result, we don't want to extend the |
| 240 | // liveness of a PHI input. It breaks all kinds of assumptions down |
| 241 | // stream. A PHI use is expected to be the kill of its source values. |
| 242 | UI = MRI->use_nodbg_begin(DstReg); |
| 243 | for (MachineRegisterInfo::use_nodbg_iterator |
| 244 | UE = MRI->use_nodbg_end(); UI != UE; ++UI) |
| 245 | if (UI->isPHI()) |
| 246 | PHIBBs.insert(UI->getParent()); |
| 247 | |
| 248 | const TargetRegisterClass *RC = MRI->getRegClass(SrcReg); |
| 249 | for (unsigned i = 0, e = Uses.size(); i != e; ++i) { |
| 250 | MachineOperand *UseMO = Uses[i]; |
| 251 | MachineInstr *UseMI = UseMO->getParent(); |
| 252 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
| 253 | if (PHIBBs.count(UseMBB)) |
| 254 | continue; |
| 255 | |
Lang Hames | c69cbd0 | 2012-02-25 02:01:00 +0000 | [diff] [blame] | 256 | // About to add uses of DstReg, clear DstReg's kill flags. |
Jakob Stoklund Olesen | 418a363 | 2012-05-20 18:42:55 +0000 | [diff] [blame^] | 257 | if (!Changed) { |
Lang Hames | c69cbd0 | 2012-02-25 02:01:00 +0000 | [diff] [blame] | 258 | MRI->clearKillFlags(DstReg); |
Jakob Stoklund Olesen | 418a363 | 2012-05-20 18:42:55 +0000 | [diff] [blame^] | 259 | MRI->constrainRegClass(DstReg, DstRC); |
| 260 | } |
Lang Hames | c69cbd0 | 2012-02-25 02:01:00 +0000 | [diff] [blame] | 261 | |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 262 | unsigned NewVR = MRI->createVirtualRegister(RC); |
| 263 | BuildMI(*UseMBB, UseMI, UseMI->getDebugLoc(), |
| 264 | TII->get(TargetOpcode::COPY), NewVR) |
| 265 | .addReg(DstReg, 0, SubIdx); |
| 266 | |
| 267 | UseMO->setReg(NewVR); |
| 268 | ++NumReuse; |
| 269 | Changed = true; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return Changed; |
| 274 | } |
| 275 | |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 276 | /// optimizeBitcastInstr - If the instruction is a bitcast instruction A that |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 277 | /// cannot be optimized away during isel (e.g. ARM::VMOVSR, which bitcast |
| 278 | /// a value cross register classes), and the source is defined by another |
| 279 | /// bitcast instruction B. And if the register class of source of B matches |
| 280 | /// the register class of instruction A, then it is legal to replace all uses |
| 281 | /// of the def of A with source of B. e.g. |
| 282 | /// %vreg0<def> = VMOVSR %vreg1 |
| 283 | /// %vreg3<def> = VMOVRS %vreg0 |
| 284 | /// Replace all uses of vreg3 with vreg1. |
| 285 | |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 286 | bool PeepholeOptimizer::optimizeBitcastInstr(MachineInstr *MI, |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 287 | MachineBasicBlock *MBB) { |
| 288 | unsigned NumDefs = MI->getDesc().getNumDefs(); |
| 289 | unsigned NumSrcs = MI->getDesc().getNumOperands() - NumDefs; |
| 290 | if (NumDefs != 1) |
| 291 | return false; |
| 292 | |
| 293 | unsigned Def = 0; |
| 294 | unsigned Src = 0; |
| 295 | for (unsigned i = 0, e = NumDefs + NumSrcs; i != e; ++i) { |
| 296 | const MachineOperand &MO = MI->getOperand(i); |
| 297 | if (!MO.isReg()) |
| 298 | continue; |
| 299 | unsigned Reg = MO.getReg(); |
| 300 | if (!Reg) |
| 301 | continue; |
| 302 | if (MO.isDef()) |
| 303 | Def = Reg; |
| 304 | else if (Src) |
| 305 | // Multiple sources? |
| 306 | return false; |
| 307 | else |
| 308 | Src = Reg; |
| 309 | } |
| 310 | |
| 311 | assert(Def && Src && "Malformed bitcast instruction!"); |
| 312 | |
| 313 | MachineInstr *DefMI = MRI->getVRegDef(Src); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 314 | if (!DefMI || !DefMI->isBitcast()) |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 315 | return false; |
| 316 | |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 317 | unsigned SrcSrc = 0; |
| 318 | NumDefs = DefMI->getDesc().getNumDefs(); |
| 319 | NumSrcs = DefMI->getDesc().getNumOperands() - NumDefs; |
| 320 | if (NumDefs != 1) |
| 321 | return false; |
| 322 | for (unsigned i = 0, e = NumDefs + NumSrcs; i != e; ++i) { |
| 323 | const MachineOperand &MO = DefMI->getOperand(i); |
| 324 | if (!MO.isReg() || MO.isDef()) |
| 325 | continue; |
| 326 | unsigned Reg = MO.getReg(); |
| 327 | if (!Reg) |
| 328 | continue; |
Duncan Sands | 7becbc4 | 2011-07-26 15:05:06 +0000 | [diff] [blame] | 329 | if (!MO.isDef()) { |
| 330 | if (SrcSrc) |
| 331 | // Multiple sources? |
| 332 | return false; |
| 333 | else |
| 334 | SrcSrc = Reg; |
| 335 | } |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | if (MRI->getRegClass(SrcSrc) != MRI->getRegClass(Def)) |
| 339 | return false; |
| 340 | |
| 341 | MRI->replaceRegWith(Def, SrcSrc); |
| 342 | MRI->clearKillFlags(SrcSrc); |
| 343 | MI->eraseFromParent(); |
| 344 | ++NumBitcasts; |
| 345 | return true; |
| 346 | } |
| 347 | |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 348 | /// optimizeCmpInstr - If the instruction is a compare and the previous |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 349 | /// instruction it's comparing against all ready sets (or could be modified to |
| 350 | /// set) the same flag as the compare, then we can remove the comparison and use |
| 351 | /// the flag from the previous instruction. |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 352 | bool PeepholeOptimizer::optimizeCmpInstr(MachineInstr *MI, |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 353 | MachineBasicBlock *MBB) { |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 354 | // If this instruction is a comparison against zero and isn't comparing a |
| 355 | // physical register, we can try to optimize it. |
| 356 | unsigned SrcReg; |
Gabor Greif | 04ac81d | 2010-09-21 12:01:15 +0000 | [diff] [blame] | 357 | int CmpMask, CmpValue; |
| 358 | if (!TII->AnalyzeCompare(MI, SrcReg, CmpMask, CmpValue) || |
Bill Wendling | 92ad57f | 2010-09-10 23:34:19 +0000 | [diff] [blame] | 359 | TargetRegisterInfo::isPhysicalRegister(SrcReg)) |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 360 | return false; |
| 361 | |
Bill Wendling | a655686 | 2010-09-11 00:13:50 +0000 | [diff] [blame] | 362 | // Attempt to optimize the comparison instruction. |
Evan Cheng | eb96a2f | 2010-11-15 21:20:45 +0000 | [diff] [blame] | 363 | if (TII->OptimizeCompareInstr(MI, SrcReg, CmpMask, CmpValue, MRI)) { |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 364 | ++NumCmps; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 365 | return true; |
| 366 | } |
| 367 | |
| 368 | return false; |
| 369 | } |
| 370 | |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 371 | bool PeepholeOptimizer::isMoveImmediate(MachineInstr *MI, |
| 372 | SmallSet<unsigned, 4> &ImmDefRegs, |
| 373 | DenseMap<unsigned, MachineInstr*> &ImmDefMIs) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 374 | const MCInstrDesc &MCID = MI->getDesc(); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 375 | if (!MI->isMoveImmediate()) |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 376 | return false; |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 377 | if (MCID.getNumDefs() != 1) |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 378 | return false; |
| 379 | unsigned Reg = MI->getOperand(0).getReg(); |
| 380 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 381 | ImmDefMIs.insert(std::make_pair(Reg, MI)); |
| 382 | ImmDefRegs.insert(Reg); |
| 383 | return true; |
| 384 | } |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 385 | |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 389 | /// foldImmediate - Try folding register operands that are defined by move |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 390 | /// immediate instructions, i.e. a trivial constant folding optimization, if |
| 391 | /// and only if the def and use are in the same BB. |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 392 | bool PeepholeOptimizer::foldImmediate(MachineInstr *MI, MachineBasicBlock *MBB, |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 393 | SmallSet<unsigned, 4> &ImmDefRegs, |
| 394 | DenseMap<unsigned, MachineInstr*> &ImmDefMIs) { |
| 395 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { |
| 396 | MachineOperand &MO = MI->getOperand(i); |
| 397 | if (!MO.isReg() || MO.isDef()) |
| 398 | continue; |
| 399 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 400 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 401 | continue; |
| 402 | if (ImmDefRegs.count(Reg) == 0) |
| 403 | continue; |
| 404 | DenseMap<unsigned, MachineInstr*>::iterator II = ImmDefMIs.find(Reg); |
| 405 | assert(II != ImmDefMIs.end()); |
| 406 | if (TII->FoldImmediate(MI, II->second, Reg, MRI)) { |
| 407 | ++NumImmFold; |
| 408 | return true; |
| 409 | } |
| 410 | } |
| 411 | return false; |
| 412 | } |
| 413 | |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 414 | bool PeepholeOptimizer::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | eb96a2f | 2010-11-15 21:20:45 +0000 | [diff] [blame] | 415 | if (DisablePeephole) |
| 416 | return false; |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 417 | |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 418 | TM = &MF.getTarget(); |
| 419 | TII = TM->getInstrInfo(); |
| 420 | MRI = &MF.getRegInfo(); |
| 421 | DT = Aggressive ? &getAnalysis<MachineDominatorTree>() : 0; |
| 422 | |
| 423 | bool Changed = false; |
| 424 | |
| 425 | SmallPtrSet<MachineInstr*, 8> LocalMIs; |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 426 | SmallSet<unsigned, 4> ImmDefRegs; |
| 427 | DenseMap<unsigned, MachineInstr*> ImmDefMIs; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 428 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 429 | MachineBasicBlock *MBB = &*I; |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 430 | |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 431 | bool SeenMoveImm = false; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 432 | LocalMIs.clear(); |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 433 | ImmDefRegs.clear(); |
| 434 | ImmDefMIs.clear(); |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 435 | |
Evan Cheng | 326d976 | 2011-02-15 05:00:24 +0000 | [diff] [blame] | 436 | bool First = true; |
| 437 | MachineBasicBlock::iterator PMII; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 438 | for (MachineBasicBlock::iterator |
Bill Wendling | 220e240 | 2010-09-10 21:55:43 +0000 | [diff] [blame] | 439 | MII = I->begin(), MIE = I->end(); MII != MIE; ) { |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 440 | MachineInstr *MI = &*MII; |
Evan Cheng | eb96a2f | 2010-11-15 21:20:45 +0000 | [diff] [blame] | 441 | LocalMIs.insert(MI); |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 442 | |
Evan Cheng | 30a343a | 2011-01-07 21:08:26 +0000 | [diff] [blame] | 443 | if (MI->isLabel() || MI->isPHI() || MI->isImplicitDef() || |
| 444 | MI->isKill() || MI->isInlineAsm() || MI->isDebugValue() || |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 445 | MI->hasUnmodeledSideEffects()) { |
| 446 | ++MII; |
Evan Cheng | eb96a2f | 2010-11-15 21:20:45 +0000 | [diff] [blame] | 447 | continue; |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 448 | } |
Evan Cheng | eb96a2f | 2010-11-15 21:20:45 +0000 | [diff] [blame] | 449 | |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 450 | if (MI->isBitcast()) { |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 451 | if (optimizeBitcastInstr(MI, MBB)) { |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 452 | // MI is deleted. |
Nick Lewycky | dec1b10 | 2011-10-13 02:16:18 +0000 | [diff] [blame] | 453 | LocalMIs.erase(MI); |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 454 | Changed = true; |
| 455 | MII = First ? I->begin() : llvm::next(PMII); |
| 456 | continue; |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 457 | } |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 458 | } else if (MI->isCompare()) { |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 459 | if (optimizeCmpInstr(MI, MBB)) { |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 460 | // MI is deleted. |
Nick Lewycky | dec1b10 | 2011-10-13 02:16:18 +0000 | [diff] [blame] | 461 | LocalMIs.erase(MI); |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 462 | Changed = true; |
Evan Cheng | 326d976 | 2011-02-15 05:00:24 +0000 | [diff] [blame] | 463 | MII = First ? I->begin() : llvm::next(PMII); |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 464 | continue; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | if (isMoveImmediate(MI, ImmDefRegs, ImmDefMIs)) { |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 469 | SeenMoveImm = true; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 470 | } else { |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 471 | Changed |= optimizeExtInstr(MI, MBB, LocalMIs); |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 472 | if (SeenMoveImm) |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 473 | Changed |= foldImmediate(MI, MBB, ImmDefRegs, ImmDefMIs); |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 474 | } |
Evan Cheng | 326d976 | 2011-02-15 05:00:24 +0000 | [diff] [blame] | 475 | |
| 476 | First = false; |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 477 | PMII = MII; |
| 478 | ++MII; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | return Changed; |
| 483 | } |