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