Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1 | //===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass performs loop invariant code motion on machine instructions. We |
| 11 | // attempt to remove as much code from the body of a loop as possible. |
| 12 | // |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 13 | // This pass does not attempt to throttle itself to limit register pressure. |
| 14 | // The register allocation phases are expected to perform rematerialization |
| 15 | // to recover when register pressure is high. |
| 16 | // |
| 17 | // This pass is not intended to be a replacement or a complete alternative |
| 18 | // for the LLVM-IR-level LICM pass. It is only designed to hoist simple |
| 19 | // constructs that are not exposed before lowering and instruction selection. |
| 20 | // |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | #define DEBUG_TYPE "machine-licm" |
Chris Lattner | ac69582 | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/Passes.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineDominators.h" |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineMemOperand.h" |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCInstrItineraries.h" |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetRegisterInfo.h" |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetInstrInfo.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/AliasAnalysis.h" |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/DenseMap.h" |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | ac69582 | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ac69582 | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 44 | STATISTIC(NumHoisted, |
| 45 | "Number of machine instructions hoisted out of loops"); |
| 46 | STATISTIC(NumLowRP, |
| 47 | "Number of instructions hoisted in low reg pressure situation"); |
| 48 | STATISTIC(NumHighLatency, |
| 49 | "Number of high latency instructions hoisted"); |
| 50 | STATISTIC(NumCSEed, |
| 51 | "Number of hoisted machine instructions CSEed"); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 52 | STATISTIC(NumPostRAHoisted, |
| 53 | "Number of machine instructions hoisted out of loops post regalloc"); |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 54 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 55 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 56 | class MachineLICM : public MachineFunctionPass { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 57 | bool PreRegAlloc; |
| 58 | |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 59 | const TargetMachine *TM; |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 60 | const TargetInstrInfo *TII; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 61 | const TargetLowering *TLI; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 62 | const TargetRegisterInfo *TRI; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 63 | const MachineFrameInfo *MFI; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 64 | MachineRegisterInfo *MRI; |
| 65 | const InstrItineraryData *InstrItins; |
Bill Wendling | 12ebf14 | 2007-12-11 19:40:06 +0000 | [diff] [blame] | 66 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 67 | // Various analyses that we use... |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 68 | AliasAnalysis *AA; // Alias analysis info. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 69 | MachineLoopInfo *MLI; // Current MachineLoopInfo |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 70 | MachineDominatorTree *DT; // Machine dominator tree for the cur loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 71 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 72 | // State that is updated as we process loops |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 73 | bool Changed; // True if a loop is changed. |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 74 | bool FirstInLoop; // True if it's the first LICM in the loop. |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 75 | MachineLoop *CurLoop; // The current loop we are working on. |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 76 | MachineBasicBlock *CurPreheader; // The preheader for CurLoop. |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 77 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 78 | BitVector AllocatableSet; |
| 79 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 80 | // Track 'estimated' register pressure. |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 81 | SmallSet<unsigned, 32> RegSeen; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 82 | SmallVector<unsigned, 8> RegPressure; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 83 | |
| 84 | // Register pressure "limit" per register class. If the pressure |
| 85 | // is higher than the limit, then it's considered high. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 86 | SmallVector<unsigned, 8> RegLimit; |
| 87 | |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 88 | // Register pressure on path leading from loop preheader to current BB. |
| 89 | SmallVector<SmallVector<unsigned, 8>, 16> BackTrace; |
| 90 | |
Dale Johannesen | c46a5f2 | 2010-07-29 17:45:24 +0000 | [diff] [blame] | 91 | // For each opcode, keep a list of potential CSE instructions. |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 92 | DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 93 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 94 | public: |
| 95 | static char ID; // Pass identification, replacement for typeid |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 96 | MachineLICM() : |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 97 | MachineFunctionPass(ID), PreRegAlloc(true) { |
| 98 | initializeMachineLICMPass(*PassRegistry::getPassRegistry()); |
| 99 | } |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 100 | |
| 101 | explicit MachineLICM(bool PreRA) : |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 102 | MachineFunctionPass(ID), PreRegAlloc(PreRA) { |
| 103 | initializeMachineLICMPass(*PassRegistry::getPassRegistry()); |
| 104 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 105 | |
| 106 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 107 | |
Dan Gohman | 7224170 | 2008-12-18 01:37:56 +0000 | [diff] [blame] | 108 | const char *getPassName() const { return "Machine Instruction LICM"; } |
| 109 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 110 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 111 | AU.addRequired<MachineLoopInfo>(); |
| 112 | AU.addRequired<MachineDominatorTree>(); |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 113 | AU.addRequired<AliasAnalysis>(); |
Bill Wendling | d5da704 | 2008-01-04 08:48:49 +0000 | [diff] [blame] | 114 | AU.addPreserved<MachineLoopInfo>(); |
| 115 | AU.addPreserved<MachineDominatorTree>(); |
| 116 | MachineFunctionPass::getAnalysisUsage(AU); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 117 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 118 | |
| 119 | virtual void releaseMemory() { |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 120 | RegSeen.clear(); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 121 | RegPressure.clear(); |
| 122 | RegLimit.clear(); |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 123 | BackTrace.clear(); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 124 | for (DenseMap<unsigned,std::vector<const MachineInstr*> >::iterator |
| 125 | CI = CSEMap.begin(), CE = CSEMap.end(); CI != CE; ++CI) |
| 126 | CI->second.clear(); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 127 | CSEMap.clear(); |
| 128 | } |
| 129 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 130 | private: |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 131 | /// CandidateInfo - Keep track of information about hoisting candidates. |
| 132 | struct CandidateInfo { |
| 133 | MachineInstr *MI; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 134 | unsigned Def; |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 135 | int FI; |
| 136 | CandidateInfo(MachineInstr *mi, unsigned def, int fi) |
| 137 | : MI(mi), Def(def), FI(fi) {} |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop |
| 141 | /// invariants out to the preheader. |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 142 | void HoistRegionPostRA(); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 143 | |
| 144 | /// HoistPostRA - When an instruction is found to only use loop invariant |
| 145 | /// operands that is safe to hoist, this instruction is called to do the |
| 146 | /// dirty work. |
| 147 | void HoistPostRA(MachineInstr *MI, unsigned Def); |
| 148 | |
| 149 | /// ProcessMI - Examine the instruction for potentai LICM candidate. Also |
| 150 | /// gather register def and frame object update information. |
| 151 | void ProcessMI(MachineInstr *MI, unsigned *PhysRegDefs, |
| 152 | SmallSet<int, 32> &StoredFIs, |
| 153 | SmallVector<CandidateInfo, 32> &Candidates); |
| 154 | |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 155 | /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the |
| 156 | /// current loop. |
| 157 | void AddToLiveIns(unsigned Reg); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 158 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 159 | /// IsLICMCandidate - Returns true if the instruction may be a suitable |
Chris Lattner | 7791080 | 2010-07-12 00:00:35 +0000 | [diff] [blame] | 160 | /// candidate for LICM. e.g. If the instruction is a call, then it's |
| 161 | /// obviously not safe to hoist it. |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 162 | bool IsLICMCandidate(MachineInstr &I); |
| 163 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 164 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 165 | /// invariant. I.e., all virtual register operands are defined outside of |
| 166 | /// the loop, physical registers aren't accessed (explicitly or implicitly), |
| 167 | /// and the instruction is hoistable. |
| 168 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 169 | bool IsLoopInvariantInst(MachineInstr &I); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 170 | |
Evan Cheng | d67705f | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 171 | /// HasAnyPHIUse - Return true if the specified register is used by any |
| 172 | /// phi node. |
| 173 | bool HasAnyPHIUse(unsigned Reg) const; |
| 174 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 175 | /// HasHighOperandLatency - Compute operand latency between a def of 'Reg' |
| 176 | /// and an use in the current loop, return true if the target considered |
| 177 | /// it 'high'. |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 178 | bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx, |
| 179 | unsigned Reg) const; |
| 180 | |
| 181 | bool IsCheapInstruction(MachineInstr &MI) const; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 182 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 183 | /// CanCauseHighRegPressure - Visit BBs from header to current BB, |
| 184 | /// check if hoisting an instruction of the given cost matrix can cause high |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 185 | /// register pressure. |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 186 | bool CanCauseHighRegPressure(DenseMap<unsigned, int> &Cost); |
| 187 | |
| 188 | /// UpdateBackTraceRegPressure - Traverse the back trace from header to |
| 189 | /// the current block and update their register pressures to reflect the |
| 190 | /// effect of hoisting MI from the current block to the preheader. |
| 191 | void UpdateBackTraceRegPressure(const MachineInstr *MI); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 192 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 193 | /// IsProfitableToHoist - Return true if it is potentially profitable to |
| 194 | /// hoist the given loop invariant. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 195 | bool IsProfitableToHoist(MachineInstr &MI); |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 196 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 197 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 198 | /// blocks dominated by the specified block, and that are in the current |
| 199 | /// loop) in depth first order w.r.t the DominatorTree. This allows us to |
| 200 | /// visit definitions before uses, allowing us to hoist a loop body in one |
| 201 | /// pass without iteration. |
| 202 | /// |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 203 | void HoistRegion(MachineDomTreeNode *N, bool IsHeader = false); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 204 | |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 205 | /// getRegisterClassIDAndCost - For a given MI, register, and the operand |
| 206 | /// index, return the ID and cost of its representative register class by |
| 207 | /// reference. |
| 208 | void getRegisterClassIDAndCost(const MachineInstr *MI, |
| 209 | unsigned Reg, unsigned OpIdx, |
| 210 | unsigned &RCId, unsigned &RCCost) const; |
| 211 | |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 212 | /// InitRegPressure - Find all virtual register references that are liveout |
| 213 | /// of the preheader to initialize the starting "register pressure". Note |
| 214 | /// this does not count live through (livein but not used) registers. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 215 | void InitRegPressure(MachineBasicBlock *BB); |
| 216 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 217 | /// UpdateRegPressure - Update estimate of register pressure after the |
| 218 | /// specified instruction. |
| 219 | void UpdateRegPressure(const MachineInstr *MI); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 220 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 221 | /// ExtractHoistableLoad - Unfold a load from the given machineinstr if |
| 222 | /// the load itself could be hoisted. Return the unfolded and hoistable |
| 223 | /// load, or null if the load couldn't be unfolded or if it wouldn't |
| 224 | /// be hoistable. |
| 225 | MachineInstr *ExtractHoistableLoad(MachineInstr *MI); |
| 226 | |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 227 | /// LookForDuplicate - Find an instruction amount PrevMIs that is a |
| 228 | /// duplicate of MI. Return this instruction if it's found. |
| 229 | const MachineInstr *LookForDuplicate(const MachineInstr *MI, |
| 230 | std::vector<const MachineInstr*> &PrevMIs); |
| 231 | |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 232 | /// EliminateCSE - Given a LICM'ed instruction, look for an instruction on |
| 233 | /// the preheader that compute the same value. If it's found, do a RAU on |
| 234 | /// with the definition of the existing instruction rather than hoisting |
| 235 | /// the instruction to the preheader. |
| 236 | bool EliminateCSE(MachineInstr *MI, |
| 237 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI); |
| 238 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 239 | /// Hoist - When an instruction is found to only use loop invariant operands |
| 240 | /// that is safe to hoist, this instruction is called to do the dirty work. |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 241 | /// It returns true if the instruction is hoisted. |
| 242 | bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 243 | |
| 244 | /// InitCSEMap - Initialize the CSE map with instructions that are in the |
| 245 | /// current loop preheader that may become duplicates of instructions that |
| 246 | /// are hoisted out of the loop. |
| 247 | void InitCSEMap(MachineBasicBlock *BB); |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 248 | |
| 249 | /// getCurPreheader - Get the preheader for the current loop, splitting |
| 250 | /// a critical edge if needed. |
| 251 | MachineBasicBlock *getCurPreheader(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 252 | }; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 253 | } // end anonymous namespace |
| 254 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 255 | char MachineLICM::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 256 | INITIALIZE_PASS_BEGIN(MachineLICM, "machinelicm", |
| 257 | "Machine Loop Invariant Code Motion", false, false) |
| 258 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 259 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 260 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 261 | INITIALIZE_PASS_END(MachineLICM, "machinelicm", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 262 | "Machine Loop Invariant Code Motion", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 263 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 264 | FunctionPass *llvm::createMachineLICMPass(bool PreRegAlloc) { |
| 265 | return new MachineLICM(PreRegAlloc); |
| 266 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 267 | |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 268 | /// LoopIsOuterMostWithPredecessor - Test if the given loop is the outer-most |
| 269 | /// loop that has a unique predecessor. |
| 270 | static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) { |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 271 | // Check whether this loop even has a unique predecessor. |
| 272 | if (!CurLoop->getLoopPredecessor()) |
| 273 | return false; |
| 274 | // Ok, now check to see if any of its outer loops do. |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 275 | for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop()) |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 276 | if (L->getLoopPredecessor()) |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 277 | return false; |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 278 | // None of them did, so this is the outermost with a unique predecessor. |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 279 | return true; |
| 280 | } |
| 281 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 282 | bool MachineLICM::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 283 | if (PreRegAlloc) |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 284 | DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: "); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 285 | else |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 286 | DEBUG(dbgs() << "******** Post-regalloc Machine LICM: "); |
| 287 | DEBUG(dbgs() << MF.getFunction()->getName() << " ********\n"); |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 288 | |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 289 | Changed = FirstInLoop = false; |
Bill Wendling | acb04ec | 2008-08-31 02:30:23 +0000 | [diff] [blame] | 290 | TM = &MF.getTarget(); |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 291 | TII = TM->getInstrInfo(); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 292 | TLI = TM->getTargetLowering(); |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 293 | TRI = TM->getRegisterInfo(); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 294 | MFI = MF.getFrameInfo(); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 295 | MRI = &MF.getRegInfo(); |
| 296 | InstrItins = TM->getInstrItineraryData(); |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 297 | AllocatableSet = TRI->getAllocatableSet(MF); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 298 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 299 | if (PreRegAlloc) { |
| 300 | // Estimate register pressure during pre-regalloc pass. |
| 301 | unsigned NumRC = TRI->getNumRegClasses(); |
| 302 | RegPressure.resize(NumRC); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 303 | std::fill(RegPressure.begin(), RegPressure.end(), 0); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 304 | RegLimit.resize(NumRC); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 305 | for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(), |
| 306 | E = TRI->regclass_end(); I != E; ++I) |
Cameron Zwarich | be2119e | 2011-03-07 21:56:36 +0000 | [diff] [blame] | 307 | RegLimit[(*I)->getID()] = TRI->getRegPressureLimit(*I, MF); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 310 | // Get our Loop information... |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 311 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 312 | DT = &getAnalysis<MachineDominatorTree>(); |
| 313 | AA = &getAnalysis<AliasAnalysis>(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 314 | |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 315 | SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end()); |
| 316 | while (!Worklist.empty()) { |
| 317 | CurLoop = Worklist.pop_back_val(); |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 318 | CurPreheader = 0; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 319 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 320 | // If this is done before regalloc, only visit outer-most preheader-sporting |
| 321 | // loops. |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 322 | if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) { |
| 323 | Worklist.append(CurLoop->begin(), CurLoop->end()); |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 324 | continue; |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 325 | } |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 326 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 327 | if (!PreRegAlloc) |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 328 | HoistRegionPostRA(); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 329 | else { |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 330 | // CSEMap is initialized for loop header when the first instruction is |
| 331 | // being hoisted. |
| 332 | MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader()); |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 333 | FirstInLoop = true; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 334 | HoistRegion(N, true); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 335 | CSEMap.clear(); |
| 336 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | return Changed; |
| 340 | } |
| 341 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 342 | /// InstructionStoresToFI - Return true if instruction stores to the |
| 343 | /// specified frame. |
| 344 | static bool InstructionStoresToFI(const MachineInstr *MI, int FI) { |
| 345 | for (MachineInstr::mmo_iterator o = MI->memoperands_begin(), |
| 346 | oe = MI->memoperands_end(); o != oe; ++o) { |
| 347 | if (!(*o)->isStore() || !(*o)->getValue()) |
| 348 | continue; |
| 349 | if (const FixedStackPseudoSourceValue *Value = |
| 350 | dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) { |
| 351 | if (Value->getFrameIndex() == FI) |
| 352 | return true; |
| 353 | } |
| 354 | } |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | /// ProcessMI - Examine the instruction for potentai LICM candidate. Also |
| 359 | /// gather register def and frame object update information. |
| 360 | void MachineLICM::ProcessMI(MachineInstr *MI, |
| 361 | unsigned *PhysRegDefs, |
| 362 | SmallSet<int, 32> &StoredFIs, |
| 363 | SmallVector<CandidateInfo, 32> &Candidates) { |
| 364 | bool RuledOut = false; |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 365 | bool HasNonInvariantUse = false; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 366 | unsigned Def = 0; |
| 367 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 368 | const MachineOperand &MO = MI->getOperand(i); |
| 369 | if (MO.isFI()) { |
| 370 | // Remember if the instruction stores to the frame index. |
| 371 | int FI = MO.getIndex(); |
| 372 | if (!StoredFIs.count(FI) && |
| 373 | MFI->isSpillSlotObjectIndex(FI) && |
| 374 | InstructionStoresToFI(MI, FI)) |
| 375 | StoredFIs.insert(FI); |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 376 | HasNonInvariantUse = true; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 377 | continue; |
| 378 | } |
| 379 | |
| 380 | if (!MO.isReg()) |
| 381 | continue; |
| 382 | unsigned Reg = MO.getReg(); |
| 383 | if (!Reg) |
| 384 | continue; |
| 385 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 386 | "Not expecting virtual register!"); |
| 387 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 388 | if (!MO.isDef()) { |
Evan Cheng | 6327537 | 2010-04-13 22:13:34 +0000 | [diff] [blame] | 389 | if (Reg && PhysRegDefs[Reg]) |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 390 | // If it's using a non-loop-invariant register, then it's obviously not |
| 391 | // safe to hoist. |
| 392 | HasNonInvariantUse = true; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 393 | continue; |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 394 | } |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 395 | |
| 396 | if (MO.isImplicit()) { |
| 397 | ++PhysRegDefs[Reg]; |
| 398 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 399 | ++PhysRegDefs[*AS]; |
| 400 | if (!MO.isDead()) |
| 401 | // Non-dead implicit def? This cannot be hoisted. |
| 402 | RuledOut = true; |
| 403 | // No need to check if a dead implicit def is also defined by |
| 404 | // another instruction. |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | // FIXME: For now, avoid instructions with multiple defs, unless |
| 409 | // it's a dead implicit def. |
| 410 | if (Def) |
| 411 | RuledOut = true; |
| 412 | else |
| 413 | Def = Reg; |
| 414 | |
| 415 | // If we have already seen another instruction that defines the same |
| 416 | // register, then this is not safe. |
| 417 | if (++PhysRegDefs[Reg] > 1) |
| 418 | // MI defined register is seen defined by another instruction in |
| 419 | // the loop, it cannot be a LICM candidate. |
| 420 | RuledOut = true; |
| 421 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 422 | if (++PhysRegDefs[*AS] > 1) |
| 423 | RuledOut = true; |
| 424 | } |
| 425 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 426 | // Only consider reloads for now and remats which do not have register |
| 427 | // operands. FIXME: Consider unfold load folding instructions. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 428 | if (Def && !RuledOut) { |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 429 | int FI = INT_MIN; |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 430 | if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) || |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 431 | (TII->isLoadFromStackSlot(MI, FI) && MFI->isSpillSlotObjectIndex(FI))) |
| 432 | Candidates.push_back(CandidateInfo(MI, Def, FI)); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
| 436 | /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop |
| 437 | /// invariants out to the preheader. |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 438 | void MachineLICM::HoistRegionPostRA() { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 439 | unsigned NumRegs = TRI->getNumRegs(); |
| 440 | unsigned *PhysRegDefs = new unsigned[NumRegs]; |
| 441 | std::fill(PhysRegDefs, PhysRegDefs + NumRegs, 0); |
| 442 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 443 | SmallVector<CandidateInfo, 32> Candidates; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 444 | SmallSet<int, 32> StoredFIs; |
| 445 | |
| 446 | // Walk the entire region, count number of defs for each register, and |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 447 | // collect potential LICM candidates. |
| 448 | const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks(); |
| 449 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 450 | MachineBasicBlock *BB = Blocks[i]; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 451 | // Conservatively treat live-in's as an external def. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 452 | // FIXME: That means a reload that're reused in successor block(s) will not |
| 453 | // be LICM'ed. |
Dan Gohman | 81bf03e | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 454 | for (MachineBasicBlock::livein_iterator I = BB->livein_begin(), |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 455 | E = BB->livein_end(); I != E; ++I) { |
| 456 | unsigned Reg = *I; |
| 457 | ++PhysRegDefs[Reg]; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 458 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 459 | ++PhysRegDefs[*AS]; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | for (MachineBasicBlock::iterator |
| 463 | MII = BB->begin(), E = BB->end(); MII != E; ++MII) { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 464 | MachineInstr *MI = &*MII; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 465 | ProcessMI(MI, PhysRegDefs, StoredFIs, Candidates); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 466 | } |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 467 | } |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 468 | |
| 469 | // Now evaluate whether the potential candidates qualify. |
| 470 | // 1. Check if the candidate defined register is defined by another |
| 471 | // instruction in the loop. |
| 472 | // 2. If the candidate is a load from stack slot (always true for now), |
| 473 | // check if the slot is stored anywhere in the loop. |
| 474 | for (unsigned i = 0, e = Candidates.size(); i != e; ++i) { |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 475 | if (Candidates[i].FI != INT_MIN && |
| 476 | StoredFIs.count(Candidates[i].FI)) |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 477 | continue; |
| 478 | |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 479 | if (PhysRegDefs[Candidates[i].Def] == 1) { |
| 480 | bool Safe = true; |
| 481 | MachineInstr *MI = Candidates[i].MI; |
Evan Cheng | c15d913 | 2010-04-13 20:25:29 +0000 | [diff] [blame] | 482 | for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) { |
| 483 | const MachineOperand &MO = MI->getOperand(j); |
Evan Cheng | 6327537 | 2010-04-13 22:13:34 +0000 | [diff] [blame] | 484 | if (!MO.isReg() || MO.isDef() || !MO.getReg()) |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 485 | continue; |
| 486 | if (PhysRegDefs[MO.getReg()]) { |
| 487 | // If it's using a non-loop-invariant register, then it's obviously |
| 488 | // not safe to hoist. |
| 489 | Safe = false; |
| 490 | break; |
| 491 | } |
| 492 | } |
| 493 | if (Safe) |
| 494 | HoistPostRA(MI, Candidates[i].Def); |
| 495 | } |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 496 | } |
Benjamin Kramer | 678d9b7 | 2010-04-12 11:38:35 +0000 | [diff] [blame] | 497 | |
| 498 | delete[] PhysRegDefs; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Jakob Stoklund Olesen | 9196ab6 | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 501 | /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the current |
| 502 | /// loop, and make sure it is not killed by any instructions in the loop. |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 503 | void MachineLICM::AddToLiveIns(unsigned Reg) { |
| 504 | const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks(); |
Jakob Stoklund Olesen | 9196ab6 | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 505 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 506 | MachineBasicBlock *BB = Blocks[i]; |
| 507 | if (!BB->isLiveIn(Reg)) |
| 508 | BB->addLiveIn(Reg); |
| 509 | for (MachineBasicBlock::iterator |
| 510 | MII = BB->begin(), E = BB->end(); MII != E; ++MII) { |
| 511 | MachineInstr *MI = &*MII; |
| 512 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 513 | MachineOperand &MO = MI->getOperand(i); |
| 514 | if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue; |
| 515 | if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg())) |
| 516 | MO.setIsKill(false); |
| 517 | } |
| 518 | } |
| 519 | } |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /// HoistPostRA - When an instruction is found to only use loop invariant |
| 523 | /// operands that is safe to hoist, this instruction is called to do the |
| 524 | /// dirty work. |
| 525 | void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) { |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 526 | MachineBasicBlock *Preheader = getCurPreheader(); |
| 527 | if (!Preheader) return; |
| 528 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 529 | // Now move the instructions to the predecessor, inserting it before any |
| 530 | // terminator instructions. |
| 531 | DEBUG({ |
| 532 | dbgs() << "Hoisting " << *MI; |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 533 | if (Preheader->getBasicBlock()) |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 534 | dbgs() << " to MachineBasicBlock " |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 535 | << Preheader->getName(); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 536 | if (MI->getParent()->getBasicBlock()) |
| 537 | dbgs() << " from MachineBasicBlock " |
| 538 | << MI->getParent()->getName(); |
| 539 | dbgs() << "\n"; |
| 540 | }); |
| 541 | |
| 542 | // Splice the instruction to the preheader. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 543 | MachineBasicBlock *MBB = MI->getParent(); |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 544 | Preheader->splice(Preheader->getFirstTerminator(), MBB, MI); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 545 | |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 546 | // Add register to livein list to all the BBs in the current loop since a |
| 547 | // loop invariant must be kept live throughout the whole loop. This is |
| 548 | // important to ensure later passes do not scavenge the def register. |
| 549 | AddToLiveIns(Def); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 550 | |
| 551 | ++NumPostRAHoisted; |
| 552 | Changed = true; |
| 553 | } |
| 554 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 555 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 556 | /// dominated by the specified block, and that are in the current loop) in depth |
| 557 | /// first order w.r.t the DominatorTree. This allows us to visit definitions |
| 558 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 559 | /// |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 560 | void MachineLICM::HoistRegion(MachineDomTreeNode *N, bool IsHeader) { |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 561 | assert(N != 0 && "Null dominator tree node?"); |
| 562 | MachineBasicBlock *BB = N->getBlock(); |
| 563 | |
| 564 | // If this subregion is not in the top level loop at all, exit. |
| 565 | if (!CurLoop->contains(BB)) return; |
| 566 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 567 | MachineBasicBlock *Preheader = getCurPreheader(); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 568 | if (!Preheader) |
| 569 | return; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 570 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 571 | if (IsHeader) { |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 572 | // Compute registers which are livein into the loop headers. |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 573 | RegSeen.clear(); |
| 574 | BackTrace.clear(); |
| 575 | InitRegPressure(Preheader); |
Daniel Dunbar | 9869413 | 2010-10-19 17:14:24 +0000 | [diff] [blame] | 576 | } |
Evan Cheng | 11e8b74 | 2010-10-19 00:55:07 +0000 | [diff] [blame] | 577 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 578 | // Remember livein register pressure. |
| 579 | BackTrace.push_back(RegPressure); |
| 580 | |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 581 | for (MachineBasicBlock::iterator |
| 582 | MII = BB->begin(), E = BB->end(); MII != E; ) { |
| 583 | MachineBasicBlock::iterator NextMII = MII; ++NextMII; |
| 584 | MachineInstr *MI = &*MII; |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 585 | if (!Hoist(MI, Preheader)) |
| 586 | UpdateRegPressure(MI); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 587 | MII = NextMII; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 588 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 589 | |
Dale Johannesen | bf1ae5e | 2010-07-20 00:50:13 +0000 | [diff] [blame] | 590 | // Don't hoist things out of a large switch statement. This often causes |
| 591 | // code to be hoisted that wasn't going to be executed, and increases |
| 592 | // register pressure in a situation where it's likely to matter. |
Dale Johannesen | 21d35c1 | 2010-07-20 21:29:12 +0000 | [diff] [blame] | 593 | if (BB->succ_size() < 25) { |
| 594 | const std::vector<MachineDomTreeNode*> &Children = N->getChildren(); |
Dale Johannesen | bf1ae5e | 2010-07-20 00:50:13 +0000 | [diff] [blame] | 595 | for (unsigned I = 0, E = Children.size(); I != E; ++I) |
| 596 | HoistRegion(Children[I]); |
Dale Johannesen | 21d35c1 | 2010-07-20 21:29:12 +0000 | [diff] [blame] | 597 | } |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 598 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 599 | BackTrace.pop_back(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 602 | static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) { |
| 603 | return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg()); |
| 604 | } |
| 605 | |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 606 | /// getRegisterClassIDAndCost - For a given MI, register, and the operand |
| 607 | /// index, return the ID and cost of its representative register class. |
| 608 | void |
| 609 | MachineLICM::getRegisterClassIDAndCost(const MachineInstr *MI, |
| 610 | unsigned Reg, unsigned OpIdx, |
| 611 | unsigned &RCId, unsigned &RCCost) const { |
| 612 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
| 613 | EVT VT = *RC->vt_begin(); |
| 614 | if (VT == MVT::untyped) { |
| 615 | RCId = RC->getID(); |
| 616 | RCCost = 1; |
| 617 | } else { |
| 618 | RCId = TLI->getRepRegClassFor(VT)->getID(); |
| 619 | RCCost = TLI->getRepRegClassCostFor(VT); |
| 620 | } |
| 621 | } |
| 622 | |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 623 | /// InitRegPressure - Find all virtual register references that are liveout of |
| 624 | /// the preheader to initialize the starting "register pressure". Note this |
| 625 | /// does not count live through (livein but not used) registers. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 626 | void MachineLICM::InitRegPressure(MachineBasicBlock *BB) { |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 627 | std::fill(RegPressure.begin(), RegPressure.end(), 0); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 628 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 629 | // If the preheader has only a single predecessor and it ends with a |
| 630 | // fallthrough or an unconditional branch, then scan its predecessor for live |
| 631 | // defs as well. This happens whenever the preheader is created by splitting |
| 632 | // the critical edge from the loop predecessor to the loop header. |
| 633 | if (BB->pred_size() == 1) { |
| 634 | MachineBasicBlock *TBB = 0, *FBB = 0; |
| 635 | SmallVector<MachineOperand, 4> Cond; |
| 636 | if (!TII->AnalyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty()) |
| 637 | InitRegPressure(*BB->pred_begin()); |
| 638 | } |
| 639 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 640 | for (MachineBasicBlock::iterator MII = BB->begin(), E = BB->end(); |
| 641 | MII != E; ++MII) { |
| 642 | MachineInstr *MI = &*MII; |
| 643 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { |
| 644 | const MachineOperand &MO = MI->getOperand(i); |
| 645 | if (!MO.isReg() || MO.isImplicit()) |
| 646 | continue; |
| 647 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 648 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 649 | continue; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 650 | |
Andrew Trick | dc986d2 | 2010-10-19 02:50:50 +0000 | [diff] [blame] | 651 | bool isNew = RegSeen.insert(Reg); |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 652 | unsigned RCId, RCCost; |
| 653 | getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 654 | if (MO.isDef()) |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 655 | RegPressure[RCId] += RCCost; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 656 | else { |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 657 | bool isKill = isOperandKill(MO, MRI); |
| 658 | if (isNew && !isKill) |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 659 | // Haven't seen this, it must be a livein. |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 660 | RegPressure[RCId] += RCCost; |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 661 | else if (!isNew && isKill) |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 662 | RegPressure[RCId] -= RCCost; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 663 | } |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 668 | /// UpdateRegPressure - Update estimate of register pressure after the |
| 669 | /// specified instruction. |
| 670 | void MachineLICM::UpdateRegPressure(const MachineInstr *MI) { |
| 671 | if (MI->isImplicitDef()) |
| 672 | return; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 673 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 674 | SmallVector<unsigned, 4> Defs; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 675 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { |
| 676 | const MachineOperand &MO = MI->getOperand(i); |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 677 | if (!MO.isReg() || MO.isImplicit()) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 678 | continue; |
| 679 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 680 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 681 | continue; |
| 682 | |
Andrew Trick | dc986d2 | 2010-10-19 02:50:50 +0000 | [diff] [blame] | 683 | bool isNew = RegSeen.insert(Reg); |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 684 | if (MO.isDef()) |
| 685 | Defs.push_back(Reg); |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 686 | else if (!isNew && isOperandKill(MO, MRI)) { |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 687 | unsigned RCId, RCCost; |
| 688 | getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost); |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 689 | if (RCCost > RegPressure[RCId]) |
| 690 | RegPressure[RCId] = 0; |
| 691 | else |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 692 | RegPressure[RCId] -= RCCost; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 693 | } |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 694 | } |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 695 | |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 696 | unsigned Idx = 0; |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 697 | while (!Defs.empty()) { |
| 698 | unsigned Reg = Defs.pop_back_val(); |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 699 | unsigned RCId, RCCost; |
| 700 | getRegisterClassIDAndCost(MI, Reg, Idx, RCId, RCCost); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 701 | RegPressure[RCId] += RCCost; |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 702 | ++Idx; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 706 | /// IsLICMCandidate - Returns true if the instruction may be a suitable |
| 707 | /// candidate for LICM. e.g. If the instruction is a call, then it's obviously |
| 708 | /// not safe to hoist it. |
| 709 | bool MachineLICM::IsLICMCandidate(MachineInstr &I) { |
Chris Lattner | 7791080 | 2010-07-12 00:00:35 +0000 | [diff] [blame] | 710 | // Check if it's safe to move the instruction. |
| 711 | bool DontMoveAcrossStore = true; |
| 712 | if (!I.isSafeToMove(TII, AA, DontMoveAcrossStore)) |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 713 | return false; |
Chris Lattner | 7791080 | 2010-07-12 00:00:35 +0000 | [diff] [blame] | 714 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 715 | return true; |
| 716 | } |
| 717 | |
| 718 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
| 719 | /// invariant. I.e., all virtual register operands are defined outside of the |
| 720 | /// loop, physical registers aren't accessed explicitly, and there are no side |
| 721 | /// effects that aren't captured by the operands or other flags. |
| 722 | /// |
| 723 | bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) { |
| 724 | if (!IsLICMCandidate(I)) |
| 725 | return false; |
Bill Wendling | 074223a | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 726 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 727 | // The instruction is loop invariant if all of its operands are. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 728 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 729 | const MachineOperand &MO = I.getOperand(i); |
| 730 | |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 731 | if (!MO.isReg()) |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 732 | continue; |
| 733 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 734 | unsigned Reg = MO.getReg(); |
| 735 | if (Reg == 0) continue; |
| 736 | |
| 737 | // Don't hoist an instruction that uses or defines a physical register. |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 738 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 739 | if (MO.isUse()) { |
| 740 | // If the physreg has no defs anywhere, it's just an ambient register |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 741 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 742 | // it could get allocated to something with a def during allocation. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 743 | if (!MRI->def_empty(Reg)) |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 744 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 745 | if (AllocatableSet.test(Reg)) |
| 746 | return false; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 747 | // Check for a def among the register's aliases too. |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 748 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 749 | unsigned AliasReg = *Alias; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 750 | if (!MRI->def_empty(AliasReg)) |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 751 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 752 | if (AllocatableSet.test(AliasReg)) |
| 753 | return false; |
| 754 | } |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 755 | // Otherwise it's safe to move. |
| 756 | continue; |
| 757 | } else if (!MO.isDead()) { |
| 758 | // A def that isn't dead. We can't move it. |
| 759 | return false; |
Dan Gohman | a363a9b | 2010-02-28 00:08:44 +0000 | [diff] [blame] | 760 | } else if (CurLoop->getHeader()->isLiveIn(Reg)) { |
| 761 | // If the reg is live into the loop, we can't hoist an instruction |
| 762 | // which would clobber it. |
| 763 | return false; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 766 | |
| 767 | if (!MO.isUse()) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 768 | continue; |
| 769 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 770 | assert(MRI->getVRegDef(Reg) && |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 771 | "Machine instr not mapped for this vreg?!"); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 772 | |
| 773 | // If the loop contains the definition of an operand, then the instruction |
| 774 | // isn't loop invariant. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 775 | if (CurLoop->contains(MRI->getVRegDef(Reg))) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 776 | return false; |
| 777 | } |
| 778 | |
| 779 | // If we got this far, the instruction is loop invariant! |
| 780 | return true; |
| 781 | } |
| 782 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 783 | |
Evan Cheng | d67705f | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 784 | /// HasAnyPHIUse - Return true if the specified register is used by any |
| 785 | /// phi node. |
| 786 | bool MachineLICM::HasAnyPHIUse(unsigned Reg) const { |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 787 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg), |
| 788 | UE = MRI->use_end(); UI != UE; ++UI) { |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 789 | MachineInstr *UseMI = &*UI; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 790 | if (UseMI->isPHI()) |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 791 | return true; |
Evan Cheng | d67705f | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 792 | // Look pass copies as well. |
| 793 | if (UseMI->isCopy()) { |
| 794 | unsigned Def = UseMI->getOperand(0).getReg(); |
| 795 | if (TargetRegisterInfo::isVirtualRegister(Def) && |
| 796 | HasAnyPHIUse(Def)) |
| 797 | return true; |
| 798 | } |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 799 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 800 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 803 | /// HasHighOperandLatency - Compute operand latency between a def of 'Reg' |
| 804 | /// and an use in the current loop, return true if the target considered |
| 805 | /// it 'high'. |
| 806 | bool MachineLICM::HasHighOperandLatency(MachineInstr &MI, |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 807 | unsigned DefIdx, unsigned Reg) const { |
| 808 | if (!InstrItins || InstrItins->isEmpty() || MRI->use_nodbg_empty(Reg)) |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 809 | return false; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 810 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 811 | for (MachineRegisterInfo::use_nodbg_iterator I = MRI->use_nodbg_begin(Reg), |
| 812 | E = MRI->use_nodbg_end(); I != E; ++I) { |
| 813 | MachineInstr *UseMI = &*I; |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 814 | if (UseMI->isCopyLike()) |
| 815 | continue; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 816 | if (!CurLoop->contains(UseMI->getParent())) |
| 817 | continue; |
| 818 | for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) { |
| 819 | const MachineOperand &MO = UseMI->getOperand(i); |
| 820 | if (!MO.isReg() || !MO.isUse()) |
| 821 | continue; |
| 822 | unsigned MOReg = MO.getReg(); |
| 823 | if (MOReg != Reg) |
| 824 | continue; |
| 825 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 826 | if (TII->hasHighOperandLatency(InstrItins, MRI, &MI, DefIdx, UseMI, i)) |
| 827 | return true; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 830 | // Only look at the first in loop use. |
| 831 | break; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 834 | return false; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 837 | /// IsCheapInstruction - Return true if the instruction is marked "cheap" or |
| 838 | /// the operand latency between its def and a use is one or less. |
| 839 | bool MachineLICM::IsCheapInstruction(MachineInstr &MI) const { |
| 840 | if (MI.getDesc().isAsCheapAsAMove() || MI.isCopyLike()) |
| 841 | return true; |
| 842 | if (!InstrItins || InstrItins->isEmpty()) |
| 843 | return false; |
| 844 | |
| 845 | bool isCheap = false; |
| 846 | unsigned NumDefs = MI.getDesc().getNumDefs(); |
| 847 | for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) { |
| 848 | MachineOperand &DefMO = MI.getOperand(i); |
| 849 | if (!DefMO.isReg() || !DefMO.isDef()) |
| 850 | continue; |
| 851 | --NumDefs; |
| 852 | unsigned Reg = DefMO.getReg(); |
| 853 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 854 | continue; |
| 855 | |
| 856 | if (!TII->hasLowDefLatency(InstrItins, &MI, i)) |
| 857 | return false; |
| 858 | isCheap = true; |
| 859 | } |
| 860 | |
| 861 | return isCheap; |
| 862 | } |
| 863 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 864 | /// CanCauseHighRegPressure - Visit BBs from header to current BB, check |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 865 | /// if hoisting an instruction of the given cost matrix can cause high |
| 866 | /// register pressure. |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 867 | bool MachineLICM::CanCauseHighRegPressure(DenseMap<unsigned, int> &Cost) { |
| 868 | for (DenseMap<unsigned, int>::iterator CI = Cost.begin(), CE = Cost.end(); |
| 869 | CI != CE; ++CI) { |
| 870 | if (CI->second <= 0) |
| 871 | continue; |
| 872 | |
| 873 | unsigned RCId = CI->first; |
| 874 | for (unsigned i = BackTrace.size(); i != 0; --i) { |
| 875 | SmallVector<unsigned, 8> &RP = BackTrace[i-1]; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 876 | if (RP[RCId] + CI->second >= RegLimit[RCId]) |
| 877 | return true; |
| 878 | } |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | return false; |
| 882 | } |
| 883 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 884 | /// UpdateBackTraceRegPressure - Traverse the back trace from header to the |
| 885 | /// current block and update their register pressures to reflect the effect |
| 886 | /// of hoisting MI from the current block to the preheader. |
| 887 | void MachineLICM::UpdateBackTraceRegPressure(const MachineInstr *MI) { |
| 888 | if (MI->isImplicitDef()) |
| 889 | return; |
| 890 | |
| 891 | // First compute the 'cost' of the instruction, i.e. its contribution |
| 892 | // to register pressure. |
| 893 | DenseMap<unsigned, int> Cost; |
| 894 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { |
| 895 | const MachineOperand &MO = MI->getOperand(i); |
| 896 | if (!MO.isReg() || MO.isImplicit()) |
| 897 | continue; |
| 898 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 899 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 900 | continue; |
| 901 | |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 902 | unsigned RCId, RCCost; |
| 903 | getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost); |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 904 | if (MO.isDef()) { |
| 905 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); |
| 906 | if (CI != Cost.end()) |
| 907 | CI->second += RCCost; |
| 908 | else |
| 909 | Cost.insert(std::make_pair(RCId, RCCost)); |
| 910 | } else if (isOperandKill(MO, MRI)) { |
| 911 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); |
| 912 | if (CI != Cost.end()) |
| 913 | CI->second -= RCCost; |
| 914 | else |
| 915 | Cost.insert(std::make_pair(RCId, -RCCost)); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | // Update register pressure of blocks from loop header to current block. |
| 920 | for (unsigned i = 0, e = BackTrace.size(); i != e; ++i) { |
| 921 | SmallVector<unsigned, 8> &RP = BackTrace[i]; |
| 922 | for (DenseMap<unsigned, int>::iterator CI = Cost.begin(), CE = Cost.end(); |
| 923 | CI != CE; ++CI) { |
| 924 | unsigned RCId = CI->first; |
| 925 | RP[RCId] += CI->second; |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 930 | /// IsProfitableToHoist - Return true if it is potentially profitable to hoist |
| 931 | /// the given loop invariant. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 932 | bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) { |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 933 | if (MI.isImplicitDef()) |
| 934 | return true; |
| 935 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 936 | // If the instruction is cheap, only hoist if it is re-materilizable. LICM |
| 937 | // will increase register pressure. It's probably not worth it if the |
| 938 | // instruction is cheap. |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 939 | // Also hoist loads from constant memory, e.g. load from stubs, GOT. Hoisting |
| 940 | // these tend to help performance in low register pressure situation. The |
| 941 | // trade off is it may cause spill in high pressure situation. It will end up |
| 942 | // adding a store in the loop preheader. But the reload is no more expensive. |
| 943 | // The side benefit is these loads are frequently CSE'ed. |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 944 | if (IsCheapInstruction(MI)) { |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 945 | if (!TII->isTriviallyReMaterializable(&MI, AA)) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 946 | return false; |
| 947 | } else { |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 948 | // Estimate register pressure to determine whether to LICM the instruction. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 949 | // In low register pressure situation, we can be more aggressive about |
| 950 | // hoisting. Also, favors hoisting long latency instructions even in |
| 951 | // moderately high pressure situation. |
Dan Gohman | fca0b10 | 2010-11-11 18:08:43 +0000 | [diff] [blame] | 952 | // FIXME: If there are long latency loop-invariant instructions inside the |
| 953 | // loop at this point, why didn't the optimizer's LICM hoist them? |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 954 | DenseMap<unsigned, int> Cost; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 955 | for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) { |
| 956 | const MachineOperand &MO = MI.getOperand(i); |
| 957 | if (!MO.isReg() || MO.isImplicit()) |
| 958 | continue; |
| 959 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 960 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 961 | continue; |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame^] | 962 | |
| 963 | unsigned RCId, RCCost; |
| 964 | getRegisterClassIDAndCost(&MI, Reg, i, RCId, RCCost); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 965 | if (MO.isDef()) { |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 966 | if (HasHighOperandLatency(MI, i, Reg)) { |
| 967 | ++NumHighLatency; |
| 968 | return true; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 969 | } |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 970 | |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 971 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 972 | if (CI != Cost.end()) |
| 973 | CI->second += RCCost; |
| 974 | else |
| 975 | Cost.insert(std::make_pair(RCId, RCCost)); |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 976 | } else if (isOperandKill(MO, MRI)) { |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 977 | // Is a virtual register use is a kill, hoisting it out of the loop |
| 978 | // may actually reduce register pressure or be register pressure |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 979 | // neutral. |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 980 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); |
| 981 | if (CI != Cost.end()) |
| 982 | CI->second -= RCCost; |
| 983 | else |
| 984 | Cost.insert(std::make_pair(RCId, -RCCost)); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 988 | // Visit BBs from header to current BB, if hoisting this doesn't cause |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 989 | // high register pressure, then it's safe to proceed. |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 990 | if (!CanCauseHighRegPressure(Cost)) { |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 991 | ++NumLowRP; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 992 | return true; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 993 | } |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 994 | |
| 995 | // High register pressure situation, only hoist if the instruction is going to |
| 996 | // be remat'ed. |
| 997 | if (!TII->isTriviallyReMaterializable(&MI, AA) && |
Evan Cheng | 9fe2009 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 998 | !MI.isInvariantLoad(AA)) |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 999 | return false; |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1000 | } |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1001 | |
Evan Cheng | d67705f | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 1002 | // If result(s) of this instruction is used by PHIs outside of the loop, then |
| 1003 | // don't hoist it if the instruction because it will introduce an extra copy. |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1004 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1005 | const MachineOperand &MO = MI.getOperand(i); |
| 1006 | if (!MO.isReg() || !MO.isDef()) |
| 1007 | continue; |
Evan Cheng | d67705f | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 1008 | if (HasAnyPHIUse(MO.getReg())) |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1009 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1010 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1011 | |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1015 | MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) { |
Evan Cheng | e95f319 | 2010-10-08 18:59:19 +0000 | [diff] [blame] | 1016 | // Don't unfold simple loads. |
| 1017 | if (MI->getDesc().canFoldAsLoad()) |
| 1018 | return 0; |
| 1019 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1020 | // If not, we may be able to unfold a load and hoist that. |
| 1021 | // First test whether the instruction is loading from an amenable |
| 1022 | // memory location. |
Evan Cheng | 9fe2009 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 1023 | if (!MI->isInvariantLoad(AA)) |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1024 | return 0; |
| 1025 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1026 | // Next determine the register class for a temporary register. |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 1027 | unsigned LoadRegIndex; |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1028 | unsigned NewOpc = |
| 1029 | TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(), |
| 1030 | /*UnfoldLoad=*/true, |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 1031 | /*UnfoldStore=*/false, |
| 1032 | &LoadRegIndex); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1033 | if (NewOpc == 0) return 0; |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1034 | const MCInstrDesc &MID = TII->get(NewOpc); |
| 1035 | if (MID.getNumDefs() != 1) return 0; |
| 1036 | const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1037 | // Ok, we're unfolding. Create a temporary register and do the unfold. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1038 | unsigned Reg = MRI->createVirtualRegister(RC); |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1039 | |
| 1040 | MachineFunction &MF = *MI->getParent()->getParent(); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1041 | SmallVector<MachineInstr *, 2> NewMIs; |
| 1042 | bool Success = |
| 1043 | TII->unfoldMemoryOperand(MF, MI, Reg, |
| 1044 | /*UnfoldLoad=*/true, /*UnfoldStore=*/false, |
| 1045 | NewMIs); |
| 1046 | (void)Success; |
| 1047 | assert(Success && |
| 1048 | "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold " |
| 1049 | "succeeded!"); |
| 1050 | assert(NewMIs.size() == 2 && |
| 1051 | "Unfolded a load into multiple instructions!"); |
| 1052 | MachineBasicBlock *MBB = MI->getParent(); |
| 1053 | MBB->insert(MI, NewMIs[0]); |
| 1054 | MBB->insert(MI, NewMIs[1]); |
| 1055 | // If unfolding produced a load that wasn't loop-invariant or profitable to |
| 1056 | // hoist, discard the new instructions and bail. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 1057 | if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) { |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1058 | NewMIs[0]->eraseFromParent(); |
| 1059 | NewMIs[1]->eraseFromParent(); |
| 1060 | return 0; |
| 1061 | } |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1062 | |
| 1063 | // Update register pressure for the unfolded instruction. |
| 1064 | UpdateRegPressure(NewMIs[1]); |
| 1065 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1066 | // Otherwise we successfully unfolded a load that we can hoist. |
| 1067 | MI->eraseFromParent(); |
| 1068 | return NewMIs[0]; |
| 1069 | } |
| 1070 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1071 | void MachineLICM::InitCSEMap(MachineBasicBlock *BB) { |
| 1072 | for (MachineBasicBlock::iterator I = BB->begin(),E = BB->end(); I != E; ++I) { |
| 1073 | const MachineInstr *MI = &*I; |
Evan Cheng | 9fe2009 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 1074 | unsigned Opcode = MI->getOpcode(); |
| 1075 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator |
| 1076 | CI = CSEMap.find(Opcode); |
| 1077 | if (CI != CSEMap.end()) |
| 1078 | CI->second.push_back(MI); |
| 1079 | else { |
| 1080 | std::vector<const MachineInstr*> CSEMIs; |
| 1081 | CSEMIs.push_back(MI); |
| 1082 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1087 | const MachineInstr* |
| 1088 | MachineLICM::LookForDuplicate(const MachineInstr *MI, |
| 1089 | std::vector<const MachineInstr*> &PrevMIs) { |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1090 | for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) { |
| 1091 | const MachineInstr *PrevMI = PrevMIs[i]; |
Evan Cheng | 9fe2009 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 1092 | if (TII->produceSameValue(MI, PrevMI, (PreRegAlloc ? MRI : 0))) |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1093 | return PrevMI; |
| 1094 | } |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | bool MachineLICM::EliminateCSE(MachineInstr *MI, |
| 1099 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) { |
Evan Cheng | db89809 | 2010-07-14 01:22:19 +0000 | [diff] [blame] | 1100 | // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate |
| 1101 | // the undef property onto uses. |
| 1102 | if (CI == CSEMap.end() || MI->isImplicitDef()) |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1103 | return false; |
| 1104 | |
| 1105 | if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) { |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1106 | DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup); |
Dan Gohman | 6ac33b4 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 1107 | |
| 1108 | // Replace virtual registers defined by MI by their counterparts defined |
| 1109 | // by Dup. |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1110 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1111 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 6ac33b4 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Physical registers may not differ here. |
| 1114 | assert((!MO.isReg() || MO.getReg() == 0 || |
| 1115 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || |
| 1116 | MO.getReg() == Dup->getOperand(i).getReg()) && |
| 1117 | "Instructions with different phys regs are not identical!"); |
| 1118 | |
| 1119 | if (MO.isReg() && MO.isDef() && |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1120 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg())) { |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1121 | MRI->replaceRegWith(MO.getReg(), Dup->getOperand(i).getReg()); |
| 1122 | MRI->clearKillFlags(Dup->getOperand(i).getReg()); |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1123 | } |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1124 | } |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1125 | MI->eraseFromParent(); |
| 1126 | ++NumCSEed; |
| 1127 | return true; |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1128 | } |
| 1129 | return false; |
| 1130 | } |
| 1131 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 1132 | /// Hoist - When an instruction is found to use only loop invariant operands |
| 1133 | /// that are safe to hoist, this instruction is called to do the dirty work. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1134 | /// |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1135 | bool MachineLICM::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) { |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1136 | // First check whether we should hoist this instruction. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 1137 | if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) { |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1138 | // If not, try unfolding a hoistable load. |
| 1139 | MI = ExtractHoistableLoad(MI); |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1140 | if (!MI) return false; |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1141 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1142 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1143 | // Now move the instructions to the predecessor, inserting it before any |
| 1144 | // terminator instructions. |
| 1145 | DEBUG({ |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1146 | dbgs() << "Hoisting " << *MI; |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1147 | if (Preheader->getBasicBlock()) |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1148 | dbgs() << " to MachineBasicBlock " |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1149 | << Preheader->getName(); |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1150 | if (MI->getParent()->getBasicBlock()) |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1151 | dbgs() << " from MachineBasicBlock " |
Jakob Stoklund Olesen | 324da76 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 1152 | << MI->getParent()->getName(); |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1153 | dbgs() << "\n"; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1154 | }); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1155 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1156 | // If this is the first instruction being hoisted to the preheader, |
| 1157 | // initialize the CSE map with potential common expressions. |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 1158 | if (FirstInLoop) { |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1159 | InitCSEMap(Preheader); |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 1160 | FirstInLoop = false; |
| 1161 | } |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1162 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1163 | // Look for opportunity to CSE the hoisted instruction. |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1164 | unsigned Opcode = MI->getOpcode(); |
| 1165 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator |
| 1166 | CI = CSEMap.find(Opcode); |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1167 | if (!EliminateCSE(MI, CI)) { |
| 1168 | // Otherwise, splice the instruction to the preheader. |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1169 | Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1170 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1171 | // Update register pressure for BBs from header to this block. |
| 1172 | UpdateBackTraceRegPressure(MI); |
| 1173 | |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1174 | // Clear the kill flags of any register this instruction defines, |
| 1175 | // since they may need to be live throughout the entire loop |
| 1176 | // rather than just live for part of it. |
| 1177 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1178 | MachineOperand &MO = MI->getOperand(i); |
| 1179 | if (MO.isReg() && MO.isDef() && !MO.isDead()) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1180 | MRI->clearKillFlags(MO.getReg()); |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1183 | // Add to the CSE map. |
| 1184 | if (CI != CSEMap.end()) |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1185 | CI->second.push_back(MI); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1186 | else { |
| 1187 | std::vector<const MachineInstr*> CSEMIs; |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1188 | CSEMIs.push_back(MI); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1189 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1190 | } |
| 1191 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1192 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1193 | ++NumHoisted; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1194 | Changed = true; |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1195 | |
| 1196 | return true; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1197 | } |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1198 | |
| 1199 | MachineBasicBlock *MachineLICM::getCurPreheader() { |
| 1200 | // Determine the block to which to hoist instructions. If we can't find a |
| 1201 | // suitable loop predecessor, we can't do any hoisting. |
| 1202 | |
| 1203 | // If we've tried to get a preheader and failed, don't try again. |
| 1204 | if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1)) |
| 1205 | return 0; |
| 1206 | |
| 1207 | if (!CurPreheader) { |
| 1208 | CurPreheader = CurLoop->getLoopPreheader(); |
| 1209 | if (!CurPreheader) { |
| 1210 | MachineBasicBlock *Pred = CurLoop->getLoopPredecessor(); |
| 1211 | if (!Pred) { |
| 1212 | CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1); |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), this); |
| 1217 | if (!CurPreheader) { |
| 1218 | CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1); |
| 1219 | return 0; |
| 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | return CurPreheader; |
| 1224 | } |