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