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