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"); |
Manman Ren | d7d003c | 2012-08-02 00:56:42 +0000 | [diff] [blame^] | 81 | STATISTIC(NumLoadFold, "Number of loads folded"); |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 82 | |
| 83 | namespace { |
| 84 | class PeepholeOptimizer : public MachineFunctionPass { |
| 85 | const TargetMachine *TM; |
| 86 | const TargetInstrInfo *TII; |
| 87 | MachineRegisterInfo *MRI; |
| 88 | MachineDominatorTree *DT; // Machine dominator tree |
| 89 | |
| 90 | public: |
| 91 | static char ID; // Pass identification |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 92 | PeepholeOptimizer() : MachineFunctionPass(ID) { |
| 93 | initializePeepholeOptimizerPass(*PassRegistry::getPassRegistry()); |
| 94 | } |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 95 | |
| 96 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 97 | |
| 98 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 99 | AU.setPreservesCFG(); |
| 100 | MachineFunctionPass::getAnalysisUsage(AU); |
| 101 | if (Aggressive) { |
| 102 | AU.addRequired<MachineDominatorTree>(); |
| 103 | AU.addPreserved<MachineDominatorTree>(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private: |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 108 | bool optimizeBitcastInstr(MachineInstr *MI, MachineBasicBlock *MBB); |
| 109 | bool optimizeCmpInstr(MachineInstr *MI, MachineBasicBlock *MBB); |
| 110 | bool optimizeExtInstr(MachineInstr *MI, MachineBasicBlock *MBB, |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 111 | SmallPtrSet<MachineInstr*, 8> &LocalMIs); |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 112 | bool isMoveImmediate(MachineInstr *MI, |
| 113 | SmallSet<unsigned, 4> &ImmDefRegs, |
| 114 | DenseMap<unsigned, MachineInstr*> &ImmDefMIs); |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 115 | bool foldImmediate(MachineInstr *MI, MachineBasicBlock *MBB, |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 116 | SmallSet<unsigned, 4> &ImmDefRegs, |
| 117 | DenseMap<unsigned, MachineInstr*> &ImmDefMIs); |
Manman Ren | d7d003c | 2012-08-02 00:56:42 +0000 | [diff] [blame^] | 118 | bool isLoadFoldable(MachineInstr *MI, unsigned &FoldAsLoadDefReg); |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 119 | }; |
| 120 | } |
| 121 | |
| 122 | char PeepholeOptimizer::ID = 0; |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 123 | char &llvm::PeepholeOptimizerID = PeepholeOptimizer::ID; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 124 | INITIALIZE_PASS_BEGIN(PeepholeOptimizer, "peephole-opts", |
| 125 | "Peephole Optimizations", false, false) |
| 126 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 127 | INITIALIZE_PASS_END(PeepholeOptimizer, "peephole-opts", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 128 | "Peephole Optimizations", false, false) |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 129 | |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 130 | /// optimizeExtInstr - If instruction is a copy-like instruction, i.e. it reads |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 131 | /// a single register and writes a single register and it does not modify the |
| 132 | /// source, and if the source value is preserved as a sub-register of the |
| 133 | /// result, then replace all reachable uses of the source with the subreg of the |
| 134 | /// result. |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 135 | /// |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 136 | /// Do not generate an EXTRACT that is used only in a debug use, as this changes |
| 137 | /// the code. Since this code does not currently share EXTRACTs, just ignore all |
| 138 | /// debug uses. |
| 139 | bool PeepholeOptimizer:: |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 140 | optimizeExtInstr(MachineInstr *MI, MachineBasicBlock *MBB, |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 141 | SmallPtrSet<MachineInstr*, 8> &LocalMIs) { |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 142 | unsigned SrcReg, DstReg, SubIdx; |
| 143 | if (!TII->isCoalescableExtInstr(*MI, SrcReg, DstReg, SubIdx)) |
| 144 | return false; |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 145 | |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 146 | if (TargetRegisterInfo::isPhysicalRegister(DstReg) || |
| 147 | TargetRegisterInfo::isPhysicalRegister(SrcReg)) |
| 148 | return false; |
| 149 | |
Jakob Stoklund Olesen | d8d0279 | 2012-06-19 21:10:18 +0000 | [diff] [blame] | 150 | if (MRI->hasOneNonDBGUse(SrcReg)) |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 151 | // No other uses. |
| 152 | return false; |
| 153 | |
Jakob Stoklund Olesen | 418a363 | 2012-05-20 18:42:55 +0000 | [diff] [blame] | 154 | // Ensure DstReg can get a register class that actually supports |
| 155 | // sub-registers. Don't change the class until we commit. |
| 156 | const TargetRegisterClass *DstRC = MRI->getRegClass(DstReg); |
| 157 | DstRC = TM->getRegisterInfo()->getSubClassWithSubReg(DstRC, SubIdx); |
| 158 | if (!DstRC) |
| 159 | return false; |
| 160 | |
Jakob Stoklund Olesen | 7164288 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 161 | // The ext instr may be operating on a sub-register of SrcReg as well. |
| 162 | // PPC::EXTSW is a 32 -> 64-bit sign extension, but it reads a 64-bit |
| 163 | // register. |
| 164 | // If UseSrcSubIdx is Set, SubIdx also applies to SrcReg, and only uses of |
| 165 | // SrcReg:SubIdx should be replaced. |
| 166 | bool UseSrcSubIdx = TM->getRegisterInfo()-> |
| 167 | getSubClassWithSubReg(MRI->getRegClass(SrcReg), SubIdx) != 0; |
| 168 | |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 169 | // The source has other uses. See if we can replace the other uses with use of |
| 170 | // the result of the extension. |
| 171 | SmallPtrSet<MachineBasicBlock*, 4> ReachedBBs; |
Jakob Stoklund Olesen | d8d0279 | 2012-06-19 21:10:18 +0000 | [diff] [blame] | 172 | for (MachineRegisterInfo::use_nodbg_iterator |
| 173 | UI = MRI->use_nodbg_begin(DstReg), UE = MRI->use_nodbg_end(); |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 174 | UI != UE; ++UI) |
| 175 | ReachedBBs.insert(UI->getParent()); |
| 176 | |
| 177 | // Uses that are in the same BB of uses of the result of the instruction. |
| 178 | SmallVector<MachineOperand*, 8> Uses; |
| 179 | |
| 180 | // Uses that the result of the instruction can reach. |
| 181 | SmallVector<MachineOperand*, 8> ExtendedUses; |
| 182 | |
| 183 | bool ExtendLife = true; |
Jakob Stoklund Olesen | d8d0279 | 2012-06-19 21:10:18 +0000 | [diff] [blame] | 184 | for (MachineRegisterInfo::use_nodbg_iterator |
| 185 | UI = MRI->use_nodbg_begin(SrcReg), UE = MRI->use_nodbg_end(); |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 186 | UI != UE; ++UI) { |
| 187 | MachineOperand &UseMO = UI.getOperand(); |
| 188 | MachineInstr *UseMI = &*UI; |
| 189 | if (UseMI == MI) |
| 190 | continue; |
| 191 | |
| 192 | if (UseMI->isPHI()) { |
| 193 | ExtendLife = false; |
| 194 | continue; |
| 195 | } |
| 196 | |
Jakob Stoklund Olesen | 7164288 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 197 | // Only accept uses of SrcReg:SubIdx. |
| 198 | if (UseSrcSubIdx && UseMO.getSubReg() != SubIdx) |
| 199 | continue; |
| 200 | |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 201 | // It's an error to translate this: |
| 202 | // |
| 203 | // %reg1025 = <sext> %reg1024 |
| 204 | // ... |
| 205 | // %reg1026 = SUBREG_TO_REG 0, %reg1024, 4 |
| 206 | // |
| 207 | // into this: |
| 208 | // |
| 209 | // %reg1025 = <sext> %reg1024 |
| 210 | // ... |
| 211 | // %reg1027 = COPY %reg1025:4 |
| 212 | // %reg1026 = SUBREG_TO_REG 0, %reg1027, 4 |
| 213 | // |
| 214 | // The problem here is that SUBREG_TO_REG is there to assert that an |
| 215 | // implicit zext occurs. It doesn't insert a zext instruction. If we allow |
| 216 | // the COPY here, it will give us the value after the <sext>, not the |
| 217 | // original value of %reg1024 before <sext>. |
| 218 | if (UseMI->getOpcode() == TargetOpcode::SUBREG_TO_REG) |
| 219 | continue; |
| 220 | |
| 221 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
| 222 | if (UseMBB == MBB) { |
| 223 | // Local uses that come after the extension. |
| 224 | if (!LocalMIs.count(UseMI)) |
| 225 | Uses.push_back(&UseMO); |
| 226 | } else if (ReachedBBs.count(UseMBB)) { |
| 227 | // Non-local uses where the result of the extension is used. Always |
| 228 | // replace these unless it's a PHI. |
| 229 | Uses.push_back(&UseMO); |
| 230 | } else if (Aggressive && DT->dominates(MBB, UseMBB)) { |
| 231 | // We may want to extend the live range of the extension result in order |
| 232 | // to replace these uses. |
| 233 | ExtendedUses.push_back(&UseMO); |
| 234 | } else { |
| 235 | // Both will be live out of the def MBB anyway. Don't extend live range of |
| 236 | // the extension result. |
| 237 | ExtendLife = false; |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (ExtendLife && !ExtendedUses.empty()) |
| 243 | // Extend the liveness of the extension result. |
| 244 | std::copy(ExtendedUses.begin(), ExtendedUses.end(), |
| 245 | std::back_inserter(Uses)); |
| 246 | |
| 247 | // Now replace all uses. |
| 248 | bool Changed = false; |
| 249 | if (!Uses.empty()) { |
| 250 | SmallPtrSet<MachineBasicBlock*, 4> PHIBBs; |
| 251 | |
| 252 | // Look for PHI uses of the extended result, we don't want to extend the |
| 253 | // liveness of a PHI input. It breaks all kinds of assumptions down |
| 254 | // stream. A PHI use is expected to be the kill of its source values. |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 255 | for (MachineRegisterInfo::use_nodbg_iterator |
Jakob Stoklund Olesen | d8d0279 | 2012-06-19 21:10:18 +0000 | [diff] [blame] | 256 | UI = MRI->use_nodbg_begin(DstReg), UE = MRI->use_nodbg_end(); |
| 257 | UI != UE; ++UI) |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 258 | if (UI->isPHI()) |
| 259 | PHIBBs.insert(UI->getParent()); |
| 260 | |
| 261 | const TargetRegisterClass *RC = MRI->getRegClass(SrcReg); |
| 262 | for (unsigned i = 0, e = Uses.size(); i != e; ++i) { |
| 263 | MachineOperand *UseMO = Uses[i]; |
| 264 | MachineInstr *UseMI = UseMO->getParent(); |
| 265 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
| 266 | if (PHIBBs.count(UseMBB)) |
| 267 | continue; |
| 268 | |
Lang Hames | c69cbd0 | 2012-02-25 02:01:00 +0000 | [diff] [blame] | 269 | // About to add uses of DstReg, clear DstReg's kill flags. |
Jakob Stoklund Olesen | 418a363 | 2012-05-20 18:42:55 +0000 | [diff] [blame] | 270 | if (!Changed) { |
Lang Hames | c69cbd0 | 2012-02-25 02:01:00 +0000 | [diff] [blame] | 271 | MRI->clearKillFlags(DstReg); |
Jakob Stoklund Olesen | 418a363 | 2012-05-20 18:42:55 +0000 | [diff] [blame] | 272 | MRI->constrainRegClass(DstReg, DstRC); |
| 273 | } |
Lang Hames | c69cbd0 | 2012-02-25 02:01:00 +0000 | [diff] [blame] | 274 | |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 275 | unsigned NewVR = MRI->createVirtualRegister(RC); |
Jakob Stoklund Olesen | 7164288 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 276 | MachineInstr *Copy = BuildMI(*UseMBB, UseMI, UseMI->getDebugLoc(), |
| 277 | TII->get(TargetOpcode::COPY), NewVR) |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 278 | .addReg(DstReg, 0, SubIdx); |
Jakob Stoklund Olesen | 7164288 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 279 | // SubIdx applies to both SrcReg and DstReg when UseSrcSubIdx is set. |
| 280 | if (UseSrcSubIdx) { |
| 281 | Copy->getOperand(0).setSubReg(SubIdx); |
| 282 | Copy->getOperand(0).setIsUndef(); |
| 283 | } |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 284 | UseMO->setReg(NewVR); |
| 285 | ++NumReuse; |
| 286 | Changed = true; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return Changed; |
| 291 | } |
| 292 | |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 293 | /// optimizeBitcastInstr - If the instruction is a bitcast instruction A that |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 294 | /// cannot be optimized away during isel (e.g. ARM::VMOVSR, which bitcast |
| 295 | /// a value cross register classes), and the source is defined by another |
| 296 | /// bitcast instruction B. And if the register class of source of B matches |
| 297 | /// the register class of instruction A, then it is legal to replace all uses |
| 298 | /// of the def of A with source of B. e.g. |
| 299 | /// %vreg0<def> = VMOVSR %vreg1 |
| 300 | /// %vreg3<def> = VMOVRS %vreg0 |
| 301 | /// Replace all uses of vreg3 with vreg1. |
| 302 | |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 303 | bool PeepholeOptimizer::optimizeBitcastInstr(MachineInstr *MI, |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 304 | MachineBasicBlock *MBB) { |
| 305 | unsigned NumDefs = MI->getDesc().getNumDefs(); |
| 306 | unsigned NumSrcs = MI->getDesc().getNumOperands() - NumDefs; |
| 307 | if (NumDefs != 1) |
| 308 | return false; |
| 309 | |
| 310 | unsigned Def = 0; |
| 311 | unsigned Src = 0; |
| 312 | for (unsigned i = 0, e = NumDefs + NumSrcs; i != e; ++i) { |
| 313 | const MachineOperand &MO = MI->getOperand(i); |
| 314 | if (!MO.isReg()) |
| 315 | continue; |
| 316 | unsigned Reg = MO.getReg(); |
| 317 | if (!Reg) |
| 318 | continue; |
| 319 | if (MO.isDef()) |
| 320 | Def = Reg; |
| 321 | else if (Src) |
| 322 | // Multiple sources? |
| 323 | return false; |
| 324 | else |
| 325 | Src = Reg; |
| 326 | } |
| 327 | |
| 328 | assert(Def && Src && "Malformed bitcast instruction!"); |
| 329 | |
| 330 | MachineInstr *DefMI = MRI->getVRegDef(Src); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 331 | if (!DefMI || !DefMI->isBitcast()) |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 332 | return false; |
| 333 | |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 334 | unsigned SrcSrc = 0; |
| 335 | NumDefs = DefMI->getDesc().getNumDefs(); |
| 336 | NumSrcs = DefMI->getDesc().getNumOperands() - NumDefs; |
| 337 | if (NumDefs != 1) |
| 338 | return false; |
| 339 | for (unsigned i = 0, e = NumDefs + NumSrcs; i != e; ++i) { |
| 340 | const MachineOperand &MO = DefMI->getOperand(i); |
| 341 | if (!MO.isReg() || MO.isDef()) |
| 342 | continue; |
| 343 | unsigned Reg = MO.getReg(); |
| 344 | if (!Reg) |
| 345 | continue; |
Duncan Sands | 7becbc4 | 2011-07-26 15:05:06 +0000 | [diff] [blame] | 346 | if (!MO.isDef()) { |
| 347 | if (SrcSrc) |
| 348 | // Multiple sources? |
| 349 | return false; |
| 350 | else |
| 351 | SrcSrc = Reg; |
| 352 | } |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | if (MRI->getRegClass(SrcSrc) != MRI->getRegClass(Def)) |
| 356 | return false; |
| 357 | |
| 358 | MRI->replaceRegWith(Def, SrcSrc); |
| 359 | MRI->clearKillFlags(SrcSrc); |
| 360 | MI->eraseFromParent(); |
| 361 | ++NumBitcasts; |
| 362 | return true; |
| 363 | } |
| 364 | |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 365 | /// optimizeCmpInstr - If the instruction is a compare and the previous |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 366 | /// instruction it's comparing against all ready sets (or could be modified to |
| 367 | /// set) the same flag as the compare, then we can remove the comparison and use |
| 368 | /// the flag from the previous instruction. |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 369 | bool PeepholeOptimizer::optimizeCmpInstr(MachineInstr *MI, |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 370 | MachineBasicBlock *MBB) { |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 371 | // If this instruction is a comparison against zero and isn't comparing a |
| 372 | // physical register, we can try to optimize it. |
Manman Ren | de7266c | 2012-06-29 21:33:59 +0000 | [diff] [blame] | 373 | unsigned SrcReg, SrcReg2; |
Gabor Greif | 04ac81d | 2010-09-21 12:01:15 +0000 | [diff] [blame] | 374 | int CmpMask, CmpValue; |
Manman Ren | de7266c | 2012-06-29 21:33:59 +0000 | [diff] [blame] | 375 | if (!TII->analyzeCompare(MI, SrcReg, SrcReg2, CmpMask, CmpValue) || |
| 376 | TargetRegisterInfo::isPhysicalRegister(SrcReg) || |
| 377 | (SrcReg2 != 0 && TargetRegisterInfo::isPhysicalRegister(SrcReg2))) |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 378 | return false; |
| 379 | |
Bill Wendling | a655686 | 2010-09-11 00:13:50 +0000 | [diff] [blame] | 380 | // Attempt to optimize the comparison instruction. |
Manman Ren | de7266c | 2012-06-29 21:33:59 +0000 | [diff] [blame] | 381 | if (TII->optimizeCompareInstr(MI, SrcReg, SrcReg2, CmpMask, CmpValue, MRI)) { |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 382 | ++NumCmps; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 383 | return true; |
| 384 | } |
| 385 | |
| 386 | return false; |
| 387 | } |
| 388 | |
Manman Ren | d7d003c | 2012-08-02 00:56:42 +0000 | [diff] [blame^] | 389 | /// isLoadFoldable - Check whether MI is a candidate for folding into a later |
| 390 | /// instruction. We only fold loads to virtual registers and the virtual |
| 391 | /// register defined has a single use. |
| 392 | bool PeepholeOptimizer::isLoadFoldable(MachineInstr *MI, |
| 393 | unsigned &FoldAsLoadDefReg) { |
| 394 | if (MI->canFoldAsLoad()) { |
| 395 | const MCInstrDesc &MCID = MI->getDesc(); |
| 396 | if (MCID.getNumDefs() == 1) { |
| 397 | unsigned Reg = MI->getOperand(0).getReg(); |
| 398 | // To reduce compilation time, we check MRI->hasOneUse when inserting |
| 399 | // loads. It should be checked when processing uses of the load, since |
| 400 | // uses can be removed during peephole. |
| 401 | if (!MI->getOperand(0).getSubReg() && |
| 402 | TargetRegisterInfo::isVirtualRegister(Reg) && |
| 403 | MRI->hasOneUse(Reg)) { |
| 404 | FoldAsLoadDefReg = Reg; |
| 405 | return true; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | return false; |
| 410 | } |
| 411 | |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 412 | bool PeepholeOptimizer::isMoveImmediate(MachineInstr *MI, |
| 413 | SmallSet<unsigned, 4> &ImmDefRegs, |
| 414 | DenseMap<unsigned, MachineInstr*> &ImmDefMIs) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 415 | const MCInstrDesc &MCID = MI->getDesc(); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 416 | if (!MI->isMoveImmediate()) |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 417 | return false; |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 418 | if (MCID.getNumDefs() != 1) |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 419 | return false; |
| 420 | unsigned Reg = MI->getOperand(0).getReg(); |
| 421 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 422 | ImmDefMIs.insert(std::make_pair(Reg, MI)); |
| 423 | ImmDefRegs.insert(Reg); |
| 424 | return true; |
| 425 | } |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 426 | |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 427 | return false; |
| 428 | } |
| 429 | |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 430 | /// foldImmediate - Try folding register operands that are defined by move |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 431 | /// immediate instructions, i.e. a trivial constant folding optimization, if |
| 432 | /// and only if the def and use are in the same BB. |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 433 | bool PeepholeOptimizer::foldImmediate(MachineInstr *MI, MachineBasicBlock *MBB, |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 434 | SmallSet<unsigned, 4> &ImmDefRegs, |
| 435 | DenseMap<unsigned, MachineInstr*> &ImmDefMIs) { |
| 436 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { |
| 437 | MachineOperand &MO = MI->getOperand(i); |
| 438 | if (!MO.isReg() || MO.isDef()) |
| 439 | continue; |
| 440 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 441 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 442 | continue; |
| 443 | if (ImmDefRegs.count(Reg) == 0) |
| 444 | continue; |
| 445 | DenseMap<unsigned, MachineInstr*>::iterator II = ImmDefMIs.find(Reg); |
| 446 | assert(II != ImmDefMIs.end()); |
| 447 | if (TII->FoldImmediate(MI, II->second, Reg, MRI)) { |
| 448 | ++NumImmFold; |
| 449 | return true; |
| 450 | } |
| 451 | } |
| 452 | return false; |
| 453 | } |
| 454 | |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 455 | bool PeepholeOptimizer::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | eb96a2f | 2010-11-15 21:20:45 +0000 | [diff] [blame] | 456 | if (DisablePeephole) |
| 457 | return false; |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 458 | |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 459 | TM = &MF.getTarget(); |
| 460 | TII = TM->getInstrInfo(); |
| 461 | MRI = &MF.getRegInfo(); |
| 462 | DT = Aggressive ? &getAnalysis<MachineDominatorTree>() : 0; |
| 463 | |
| 464 | bool Changed = false; |
| 465 | |
| 466 | SmallPtrSet<MachineInstr*, 8> LocalMIs; |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 467 | SmallSet<unsigned, 4> ImmDefRegs; |
| 468 | DenseMap<unsigned, MachineInstr*> ImmDefMIs; |
Manman Ren | d7d003c | 2012-08-02 00:56:42 +0000 | [diff] [blame^] | 469 | unsigned FoldAsLoadDefReg; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 470 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 471 | MachineBasicBlock *MBB = &*I; |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 472 | |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 473 | bool SeenMoveImm = false; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 474 | LocalMIs.clear(); |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 475 | ImmDefRegs.clear(); |
| 476 | ImmDefMIs.clear(); |
Manman Ren | d7d003c | 2012-08-02 00:56:42 +0000 | [diff] [blame^] | 477 | FoldAsLoadDefReg = 0; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 478 | |
Evan Cheng | 326d976 | 2011-02-15 05:00:24 +0000 | [diff] [blame] | 479 | bool First = true; |
| 480 | MachineBasicBlock::iterator PMII; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 481 | for (MachineBasicBlock::iterator |
Bill Wendling | 220e240 | 2010-09-10 21:55:43 +0000 | [diff] [blame] | 482 | MII = I->begin(), MIE = I->end(); MII != MIE; ) { |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 483 | MachineInstr *MI = &*MII; |
Evan Cheng | eb96a2f | 2010-11-15 21:20:45 +0000 | [diff] [blame] | 484 | LocalMIs.insert(MI); |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 485 | |
Manman Ren | d7d003c | 2012-08-02 00:56:42 +0000 | [diff] [blame^] | 486 | // If there exists an instruction which belongs to the following |
| 487 | // categories, we will discard the load candidate. |
Evan Cheng | 30a343a | 2011-01-07 21:08:26 +0000 | [diff] [blame] | 488 | if (MI->isLabel() || MI->isPHI() || MI->isImplicitDef() || |
| 489 | MI->isKill() || MI->isInlineAsm() || MI->isDebugValue() || |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 490 | MI->hasUnmodeledSideEffects()) { |
Manman Ren | d7d003c | 2012-08-02 00:56:42 +0000 | [diff] [blame^] | 491 | FoldAsLoadDefReg = 0; |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 492 | ++MII; |
Evan Cheng | eb96a2f | 2010-11-15 21:20:45 +0000 | [diff] [blame] | 493 | continue; |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 494 | } |
Manman Ren | d7d003c | 2012-08-02 00:56:42 +0000 | [diff] [blame^] | 495 | if (MI->mayStore() || MI->isCall()) |
| 496 | FoldAsLoadDefReg = 0; |
Evan Cheng | eb96a2f | 2010-11-15 21:20:45 +0000 | [diff] [blame] | 497 | |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 498 | if (MI->isBitcast()) { |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 499 | if (optimizeBitcastInstr(MI, MBB)) { |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 500 | // MI is deleted. |
Nick Lewycky | dec1b10 | 2011-10-13 02:16:18 +0000 | [diff] [blame] | 501 | LocalMIs.erase(MI); |
Evan Cheng | d158fba | 2011-03-15 05:13:13 +0000 | [diff] [blame] | 502 | Changed = true; |
| 503 | MII = First ? I->begin() : llvm::next(PMII); |
| 504 | continue; |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 505 | } |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 506 | } else if (MI->isCompare()) { |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 507 | if (optimizeCmpInstr(MI, MBB)) { |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 508 | // MI is deleted. |
Nick Lewycky | dec1b10 | 2011-10-13 02:16:18 +0000 | [diff] [blame] | 509 | LocalMIs.erase(MI); |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 510 | Changed = true; |
Evan Cheng | 326d976 | 2011-02-15 05:00:24 +0000 | [diff] [blame] | 511 | MII = First ? I->begin() : llvm::next(PMII); |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 512 | continue; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if (isMoveImmediate(MI, ImmDefRegs, ImmDefMIs)) { |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 517 | SeenMoveImm = true; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 518 | } else { |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 519 | Changed |= optimizeExtInstr(MI, MBB, LocalMIs); |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 520 | if (SeenMoveImm) |
Jim Grosbach | 39cc513 | 2012-05-01 23:21:41 +0000 | [diff] [blame] | 521 | Changed |= foldImmediate(MI, MBB, ImmDefRegs, ImmDefMIs); |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 522 | } |
Evan Cheng | 326d976 | 2011-02-15 05:00:24 +0000 | [diff] [blame] | 523 | |
Manman Ren | d7d003c | 2012-08-02 00:56:42 +0000 | [diff] [blame^] | 524 | // Check whether MI is a load candidate for folding into a later |
| 525 | // instruction. If MI is not a candidate, check whether we can fold an |
| 526 | // earlier load into MI. |
| 527 | if (!isLoadFoldable(MI, FoldAsLoadDefReg) && FoldAsLoadDefReg) { |
| 528 | // We need to fold load after optimizeCmpInstr, since optimizeCmpInstr |
| 529 | // can enable folding by converting SUB to CMP. |
| 530 | MachineInstr *DefMI = 0; |
| 531 | MachineInstr *FoldMI = TII->optimizeLoadInstr(MI, MRI, |
| 532 | FoldAsLoadDefReg, DefMI); |
| 533 | if (FoldMI) { |
| 534 | // Update LocalMIs since we replaced MI with FoldMI and deleted DefMI. |
| 535 | LocalMIs.erase(MI); |
| 536 | LocalMIs.erase(DefMI); |
| 537 | LocalMIs.insert(FoldMI); |
| 538 | MI->eraseFromParent(); |
| 539 | DefMI->eraseFromParent(); |
| 540 | ++NumLoadFold; |
| 541 | |
| 542 | // MI is replaced with FoldMI. |
| 543 | Changed = true; |
| 544 | PMII = FoldMI; |
| 545 | MII = llvm::next(PMII); |
| 546 | continue; |
| 547 | } |
| 548 | } |
Evan Cheng | 326d976 | 2011-02-15 05:00:24 +0000 | [diff] [blame] | 549 | First = false; |
Evan Cheng | cf75ab5 | 2011-02-14 21:50:37 +0000 | [diff] [blame] | 550 | PMII = MII; |
| 551 | ++MII; |
Bill Wendling | 6cdb1ab | 2010-08-09 23:59:04 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | return Changed; |
| 556 | } |