| Bill Wendling | fb706bc | 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 | f3ebc3f | 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 | fb706bc | 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 | 79618d1 | 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 | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// | 
|  | 22 |  | 
|  | 23 | #define DEBUG_TYPE "machine-licm" | 
| Chris Lattner | b5c1d9b | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/Passes.h" | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineDominators.h" | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFrameInfo.h" | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineLoopInfo.h" | 
| Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineMemOperand.h" | 
| Bill Wendling | 5da1945 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" | 
| Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/PseudoSourceValue.h" | 
| Evan Cheng | 8264e27 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCInstrItineraries.h" | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetLowering.h" | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetRegisterInfo.h" | 
| Bill Wendling | 38236ef | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetInstrInfo.h" | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetMachine.h" | 
| Dan Gohman | be8137b | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/AliasAnalysis.h" | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/DenseMap.h" | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallSet.h" | 
| Chris Lattner | b5c1d9b | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/Statistic.h" | 
| Evan Cheng | b35afca | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" | 
| Chris Lattner | b5c1d9b | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" | 
| Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 43 | using namespace llvm; | 
|  | 44 |  | 
| Evan Cheng | b35afca | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 45 | static cl::opt<bool> | 
|  | 46 | AvoidSpeculation("avoid-speculation", | 
|  | 47 | cl::desc("MachineLICM should avoid speculation"), | 
| Evan Cheng | 7313337 | 2011-10-26 01:26:57 +0000 | [diff] [blame] | 48 | cl::init(true), cl::Hidden); | 
| Evan Cheng | b35afca | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 49 |  | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 50 | STATISTIC(NumHoisted, | 
|  | 51 | "Number of machine instructions hoisted out of loops"); | 
|  | 52 | STATISTIC(NumLowRP, | 
|  | 53 | "Number of instructions hoisted in low reg pressure situation"); | 
|  | 54 | STATISTIC(NumHighLatency, | 
|  | 55 | "Number of high latency instructions hoisted"); | 
|  | 56 | STATISTIC(NumCSEed, | 
|  | 57 | "Number of hoisted machine instructions CSEed"); | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 58 | STATISTIC(NumPostRAHoisted, | 
|  | 59 | "Number of machine instructions hoisted out of loops post regalloc"); | 
| Bill Wendling | 4375173 | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 60 |  | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 61 | namespace { | 
| Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 62 | class MachineLICM : public MachineFunctionPass { | 
| Bill Wendling | 5da1945 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 63 | const TargetMachine   *TM; | 
| Bill Wendling | 38236ef | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 64 | const TargetInstrInfo *TII; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 65 | const TargetLowering *TLI; | 
| Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 66 | const TargetRegisterInfo *TRI; | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 67 | const MachineFrameInfo *MFI; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 68 | MachineRegisterInfo *MRI; | 
|  | 69 | const InstrItineraryData *InstrItins; | 
| Andrew Trick | c40815d | 2012-02-08 21:23:03 +0000 | [diff] [blame] | 70 | bool PreRegAlloc; | 
| Bill Wendling | b678ae7 | 2007-12-11 19:40:06 +0000 | [diff] [blame] | 71 |  | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 72 | // Various analyses that we use... | 
| Dan Gohman | be8137b | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 73 | AliasAnalysis        *AA;      // Alias analysis info. | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 74 | MachineLoopInfo      *MLI;     // Current MachineLoopInfo | 
| Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 75 | MachineDominatorTree *DT;      // Machine dominator tree for the cur loop | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 76 |  | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 77 | // State that is updated as we process loops | 
| Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 78 | bool         Changed;          // True if a loop is changed. | 
| Evan Cheng | 032f326 | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 79 | bool         FirstInLoop;      // True if it's the first LICM in the loop. | 
| Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 80 | MachineLoop *CurLoop;          // The current loop we are working on. | 
| Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 81 | MachineBasicBlock *CurPreheader; // The preheader for CurLoop. | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 82 |  | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 83 | // Track 'estimated' register pressure. | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 84 | SmallSet<unsigned, 32> RegSeen; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 85 | SmallVector<unsigned, 8> RegPressure; | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 86 |  | 
|  | 87 | // Register pressure "limit" per register class. If the pressure | 
|  | 88 | // is higher than the limit, then it's considered high. | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 89 | SmallVector<unsigned, 8> RegLimit; | 
|  | 90 |  | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 91 | // Register pressure on path leading from loop preheader to current BB. | 
|  | 92 | SmallVector<SmallVector<unsigned, 8>, 16> BackTrace; | 
|  | 93 |  | 
| Dale Johannesen | 329d474 | 2010-07-29 17:45:24 +0000 | [diff] [blame] | 94 | // For each opcode, keep a list of potential CSE instructions. | 
| Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 95 | DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap; | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 96 |  | 
| Evan Cheng | f192ca0 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 97 | enum { | 
|  | 98 | SpeculateFalse   = 0, | 
|  | 99 | SpeculateTrue    = 1, | 
|  | 100 | SpeculateUnknown = 2 | 
|  | 101 | }; | 
|  | 102 |  | 
| Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 103 | // If a MBB does not dominate loop exiting blocks then it may not safe | 
|  | 104 | // to hoist loads from this block. | 
| Evan Cheng | f192ca0 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 105 | // Tri-state: 0 - false, 1 - true, 2 - unknown | 
|  | 106 | unsigned SpeculationState; | 
| Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 107 |  | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 108 | public: | 
|  | 109 | static char ID; // Pass identification, replacement for typeid | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 110 | MachineLICM() : | 
| Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 111 | MachineFunctionPass(ID), PreRegAlloc(true) { | 
|  | 112 | initializeMachineLICMPass(*PassRegistry::getPassRegistry()); | 
|  | 113 | } | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 114 |  | 
|  | 115 | explicit MachineLICM(bool PreRA) : | 
| Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 116 | MachineFunctionPass(ID), PreRegAlloc(PreRA) { | 
|  | 117 | initializeMachineLICMPass(*PassRegistry::getPassRegistry()); | 
|  | 118 | } | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 119 |  | 
|  | 120 | virtual bool runOnMachineFunction(MachineFunction &MF); | 
|  | 121 |  | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 122 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 123 | AU.addRequired<MachineLoopInfo>(); | 
|  | 124 | AU.addRequired<MachineDominatorTree>(); | 
| Dan Gohman | be8137b | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 125 | AU.addRequired<AliasAnalysis>(); | 
| Bill Wendling | 3bf5603 | 2008-01-04 08:48:49 +0000 | [diff] [blame] | 126 | AU.addPreserved<MachineLoopInfo>(); | 
|  | 127 | AU.addPreserved<MachineDominatorTree>(); | 
|  | 128 | MachineFunctionPass::getAnalysisUsage(AU); | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 129 | } | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 130 |  | 
|  | 131 | virtual void releaseMemory() { | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 132 | RegSeen.clear(); | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 133 | RegPressure.clear(); | 
|  | 134 | RegLimit.clear(); | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 135 | BackTrace.clear(); | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 136 | for (DenseMap<unsigned,std::vector<const MachineInstr*> >::iterator | 
|  | 137 | CI = CSEMap.begin(), CE = CSEMap.end(); CI != CE; ++CI) | 
|  | 138 | CI->second.clear(); | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 139 | CSEMap.clear(); | 
|  | 140 | } | 
|  | 141 |  | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 142 | private: | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 143 | /// CandidateInfo - Keep track of information about hoisting candidates. | 
|  | 144 | struct CandidateInfo { | 
|  | 145 | MachineInstr *MI; | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 146 | unsigned      Def; | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 147 | int           FI; | 
|  | 148 | CandidateInfo(MachineInstr *mi, unsigned def, int fi) | 
|  | 149 | : MI(mi), Def(def), FI(fi) {} | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 150 | }; | 
|  | 151 |  | 
|  | 152 | /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop | 
|  | 153 | /// invariants out to the preheader. | 
| Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 154 | void HoistRegionPostRA(); | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 155 |  | 
|  | 156 | /// HoistPostRA - When an instruction is found to only use loop invariant | 
|  | 157 | /// operands that is safe to hoist, this instruction is called to do the | 
|  | 158 | /// dirty work. | 
|  | 159 | void HoistPostRA(MachineInstr *MI, unsigned Def); | 
|  | 160 |  | 
|  | 161 | /// ProcessMI - Examine the instruction for potentai LICM candidate. Also | 
|  | 162 | /// gather register def and frame object update information. | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 163 | void ProcessMI(MachineInstr *MI, | 
|  | 164 | BitVector &PhysRegDefs, | 
|  | 165 | BitVector &PhysRegClobbers, | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 166 | SmallSet<int, 32> &StoredFIs, | 
|  | 167 | SmallVector<CandidateInfo, 32> &Candidates); | 
|  | 168 |  | 
| Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 169 | /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the | 
|  | 170 | /// current loop. | 
|  | 171 | void AddToLiveIns(unsigned Reg); | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 172 |  | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 173 | /// IsLICMCandidate - Returns true if the instruction may be a suitable | 
| Chris Lattner | 0b7ae20 | 2010-07-12 00:00:35 +0000 | [diff] [blame] | 174 | /// candidate for LICM. e.g. If the instruction is a call, then it's | 
|  | 175 | /// obviously not safe to hoist it. | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 176 | bool IsLICMCandidate(MachineInstr &I); | 
|  | 177 |  | 
| Bill Wendling | 3f19dfe7 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 178 | /// IsLoopInvariantInst - Returns true if the instruction is loop | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 179 | /// invariant. I.e., all virtual register operands are defined outside of | 
|  | 180 | /// the loop, physical registers aren't accessed (explicitly or implicitly), | 
|  | 181 | /// and the instruction is hoistable. | 
| Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 182 | /// | 
| Bill Wendling | 3f19dfe7 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 183 | bool IsLoopInvariantInst(MachineInstr &I); | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 184 |  | 
| Evan Cheng | ef42bea | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 185 | /// HasAnyPHIUse - Return true if the specified register is used by any | 
|  | 186 | /// phi node. | 
|  | 187 | bool HasAnyPHIUse(unsigned Reg) const; | 
|  | 188 |  | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 189 | /// HasHighOperandLatency - Compute operand latency between a def of 'Reg' | 
|  | 190 | /// and an use in the current loop, return true if the target considered | 
|  | 191 | /// it 'high'. | 
| Evan Cheng | e96b8d7 | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 192 | bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx, | 
|  | 193 | unsigned Reg) const; | 
|  | 194 |  | 
|  | 195 | bool IsCheapInstruction(MachineInstr &MI) const; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 196 |  | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 197 | /// CanCauseHighRegPressure - Visit BBs from header to current BB, | 
|  | 198 | /// check if hoisting an instruction of the given cost matrix can cause high | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 199 | /// register pressure. | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 200 | bool CanCauseHighRegPressure(DenseMap<unsigned, int> &Cost); | 
|  | 201 |  | 
|  | 202 | /// UpdateBackTraceRegPressure - Traverse the back trace from header to | 
|  | 203 | /// the current block and update their register pressures to reflect the | 
|  | 204 | /// effect of hoisting MI from the current block to the preheader. | 
|  | 205 | void UpdateBackTraceRegPressure(const MachineInstr *MI); | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 206 |  | 
| Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 207 | /// IsProfitableToHoist - Return true if it is potentially profitable to | 
|  | 208 | /// hoist the given loop invariant. | 
| Evan Cheng | 73f9a9e | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 209 | bool IsProfitableToHoist(MachineInstr &MI); | 
| Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 210 |  | 
| Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 211 | /// IsGuaranteedToExecute - Check if this mbb is guaranteed to execute. | 
|  | 212 | /// If not then a load from this mbb may not be safe to hoist. | 
|  | 213 | bool IsGuaranteedToExecute(MachineBasicBlock *BB); | 
|  | 214 |  | 
| Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 215 | void EnterScope(MachineBasicBlock *MBB); | 
|  | 216 |  | 
|  | 217 | void ExitScope(MachineBasicBlock *MBB); | 
|  | 218 |  | 
|  | 219 | /// ExitScopeIfDone - Destroy scope for the MBB that corresponds to given | 
|  | 220 | /// dominator tree node if its a leaf or all of its children are done. Walk | 
|  | 221 | /// up the dominator tree to destroy ancestors which are now done. | 
|  | 222 | void ExitScopeIfDone(MachineDomTreeNode *Node, | 
|  | 223 | DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren, | 
|  | 224 | DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap); | 
|  | 225 |  | 
|  | 226 | /// HoistOutOfLoop - Walk the specified loop in the CFG (defined by all | 
|  | 227 | /// blocks dominated by the specified header block, and that are in the | 
|  | 228 | /// current loop) in depth first order w.r.t the DominatorTree. This allows | 
|  | 229 | /// us to visit definitions before uses, allowing us to hoist a loop body in | 
|  | 230 | /// one pass without iteration. | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 231 | /// | 
| Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 232 | void HoistOutOfLoop(MachineDomTreeNode *LoopHeaderNode); | 
|  | 233 | void HoistRegion(MachineDomTreeNode *N, bool IsHeader); | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 234 |  | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 235 | /// getRegisterClassIDAndCost - For a given MI, register, and the operand | 
|  | 236 | /// index, return the ID and cost of its representative register class by | 
|  | 237 | /// reference. | 
|  | 238 | void getRegisterClassIDAndCost(const MachineInstr *MI, | 
|  | 239 | unsigned Reg, unsigned OpIdx, | 
|  | 240 | unsigned &RCId, unsigned &RCCost) const; | 
|  | 241 |  | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 242 | /// InitRegPressure - Find all virtual register references that are liveout | 
|  | 243 | /// of the preheader to initialize the starting "register pressure". Note | 
|  | 244 | /// this does not count live through (livein but not used) registers. | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 245 | void InitRegPressure(MachineBasicBlock *BB); | 
|  | 246 |  | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 247 | /// UpdateRegPressure - Update estimate of register pressure after the | 
|  | 248 | /// specified instruction. | 
|  | 249 | void UpdateRegPressure(const MachineInstr *MI); | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 250 |  | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 251 | /// ExtractHoistableLoad - Unfold a load from the given machineinstr if | 
|  | 252 | /// the load itself could be hoisted. Return the unfolded and hoistable | 
|  | 253 | /// load, or null if the load couldn't be unfolded or if it wouldn't | 
|  | 254 | /// be hoistable. | 
|  | 255 | MachineInstr *ExtractHoistableLoad(MachineInstr *MI); | 
|  | 256 |  | 
| Evan Cheng | 7ff8319 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 257 | /// LookForDuplicate - Find an instruction amount PrevMIs that is a | 
|  | 258 | /// duplicate of MI. Return this instruction if it's found. | 
|  | 259 | const MachineInstr *LookForDuplicate(const MachineInstr *MI, | 
|  | 260 | std::vector<const MachineInstr*> &PrevMIs); | 
|  | 261 |  | 
| Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 262 | /// EliminateCSE - Given a LICM'ed instruction, look for an instruction on | 
|  | 263 | /// the preheader that compute the same value. If it's found, do a RAU on | 
|  | 264 | /// with the definition of the existing instruction rather than hoisting | 
|  | 265 | /// the instruction to the preheader. | 
|  | 266 | bool EliminateCSE(MachineInstr *MI, | 
|  | 267 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI); | 
|  | 268 |  | 
| Evan Cheng | af13895 | 2011-10-12 00:09:14 +0000 | [diff] [blame] | 269 | /// MayCSE - Return true if the given instruction will be CSE'd if it's | 
|  | 270 | /// hoisted out of the loop. | 
|  | 271 | bool MayCSE(MachineInstr *MI); | 
|  | 272 |  | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 273 | /// Hoist - When an instruction is found to only use loop invariant operands | 
|  | 274 | /// that is safe to hoist, this instruction is called to do the dirty work. | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 275 | /// It returns true if the instruction is hoisted. | 
|  | 276 | bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader); | 
| Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 277 |  | 
|  | 278 | /// InitCSEMap - Initialize the CSE map with instructions that are in the | 
|  | 279 | /// current loop preheader that may become duplicates of instructions that | 
|  | 280 | /// are hoisted out of the loop. | 
|  | 281 | void InitCSEMap(MachineBasicBlock *BB); | 
| Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 282 |  | 
|  | 283 | /// getCurPreheader - Get the preheader for the current loop, splitting | 
|  | 284 | /// a critical edge if needed. | 
|  | 285 | MachineBasicBlock *getCurPreheader(); | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 286 | }; | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 287 | } // end anonymous namespace | 
|  | 288 |  | 
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 289 | char MachineLICM::ID = 0; | 
| Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 290 | char &llvm::MachineLICMID = MachineLICM::ID; | 
| Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 291 | INITIALIZE_PASS_BEGIN(MachineLICM, "machinelicm", | 
|  | 292 | "Machine Loop Invariant Code Motion", false, false) | 
|  | 293 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) | 
|  | 294 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) | 
|  | 295 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) | 
|  | 296 | INITIALIZE_PASS_END(MachineLICM, "machinelicm", | 
| Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 297 | "Machine Loop Invariant Code Motion", false, false) | 
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 298 |  | 
| Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 299 | /// LoopIsOuterMostWithPredecessor - Test if the given loop is the outer-most | 
|  | 300 | /// loop that has a unique predecessor. | 
|  | 301 | static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) { | 
| Dan Gohman | 7929c44 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 302 | // Check whether this loop even has a unique predecessor. | 
|  | 303 | if (!CurLoop->getLoopPredecessor()) | 
|  | 304 | return false; | 
|  | 305 | // Ok, now check to see if any of its outer loops do. | 
| Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 306 | for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop()) | 
| Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 307 | if (L->getLoopPredecessor()) | 
| Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 308 | return false; | 
| Dan Gohman | 7929c44 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 309 | // None of them did, so this is the outermost with a unique predecessor. | 
| Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 310 | return true; | 
|  | 311 | } | 
|  | 312 |  | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 313 | bool MachineLICM::runOnMachineFunction(MachineFunction &MF) { | 
| Evan Cheng | 032f326 | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 314 | Changed = FirstInLoop = false; | 
| Bill Wendling | 58bb4f1 | 2008-08-31 02:30:23 +0000 | [diff] [blame] | 315 | TM = &MF.getTarget(); | 
| Bill Wendling | 5da1945 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 316 | TII = TM->getInstrInfo(); | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 317 | TLI = TM->getTargetLowering(); | 
| Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 318 | TRI = TM->getRegisterInfo(); | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 319 | MFI = MF.getFrameInfo(); | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 320 | MRI = &MF.getRegInfo(); | 
|  | 321 | InstrItins = TM->getInstrItineraryData(); | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 322 |  | 
| Andrew Trick | c40815d | 2012-02-08 21:23:03 +0000 | [diff] [blame] | 323 | PreRegAlloc = MRI->isSSA(); | 
|  | 324 |  | 
| Jakob Stoklund Olesen | c8046c0 | 2012-02-11 00:40:36 +0000 | [diff] [blame] | 325 | if (PreRegAlloc) | 
|  | 326 | DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: "); | 
|  | 327 | else | 
|  | 328 | DEBUG(dbgs() << "******** Post-regalloc Machine LICM: "); | 
|  | 329 | DEBUG(dbgs() << MF.getFunction()->getName() << " ********\n"); | 
|  | 330 |  | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 331 | if (PreRegAlloc) { | 
|  | 332 | // Estimate register pressure during pre-regalloc pass. | 
|  | 333 | unsigned NumRC = TRI->getNumRegClasses(); | 
|  | 334 | RegPressure.resize(NumRC); | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 335 | std::fill(RegPressure.begin(), RegPressure.end(), 0); | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 336 | RegLimit.resize(NumRC); | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 337 | for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(), | 
|  | 338 | E = TRI->regclass_end(); I != E; ++I) | 
| Cameron Zwarich | df61694 | 2011-03-07 21:56:36 +0000 | [diff] [blame] | 339 | RegLimit[(*I)->getID()] = TRI->getRegPressureLimit(*I, MF); | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 340 | } | 
|  | 341 |  | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 342 | // Get our Loop information... | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 343 | MLI = &getAnalysis<MachineLoopInfo>(); | 
|  | 344 | DT  = &getAnalysis<MachineDominatorTree>(); | 
|  | 345 | AA  = &getAnalysis<AliasAnalysis>(); | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 346 |  | 
| Dan Gohman | 7929c44 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 347 | SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end()); | 
|  | 348 | while (!Worklist.empty()) { | 
|  | 349 | CurLoop = Worklist.pop_back_val(); | 
| Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 350 | CurPreheader = 0; | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 351 |  | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 352 | // If this is done before regalloc, only visit outer-most preheader-sporting | 
|  | 353 | // loops. | 
| Dan Gohman | 7929c44 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 354 | if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) { | 
|  | 355 | Worklist.append(CurLoop->begin(), CurLoop->end()); | 
| Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 356 | continue; | 
| Dan Gohman | 7929c44 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 357 | } | 
| Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 358 |  | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 359 | if (!PreRegAlloc) | 
| Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 360 | HoistRegionPostRA(); | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 361 | else { | 
| Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 362 | // CSEMap is initialized for loop header when the first instruction is | 
|  | 363 | // being hoisted. | 
|  | 364 | MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader()); | 
| Evan Cheng | 032f326 | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 365 | FirstInLoop = true; | 
| Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 366 | HoistOutOfLoop(N); | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 367 | CSEMap.clear(); | 
|  | 368 | } | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 369 | } | 
|  | 370 |  | 
|  | 371 | return Changed; | 
|  | 372 | } | 
|  | 373 |  | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 374 | /// InstructionStoresToFI - Return true if instruction stores to the | 
|  | 375 | /// specified frame. | 
|  | 376 | static bool InstructionStoresToFI(const MachineInstr *MI, int FI) { | 
|  | 377 | for (MachineInstr::mmo_iterator o = MI->memoperands_begin(), | 
|  | 378 | oe = MI->memoperands_end(); o != oe; ++o) { | 
|  | 379 | if (!(*o)->isStore() || !(*o)->getValue()) | 
|  | 380 | continue; | 
|  | 381 | if (const FixedStackPseudoSourceValue *Value = | 
|  | 382 | dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) { | 
|  | 383 | if (Value->getFrameIndex() == FI) | 
|  | 384 | return true; | 
|  | 385 | } | 
|  | 386 | } | 
|  | 387 | return false; | 
|  | 388 | } | 
|  | 389 |  | 
|  | 390 | /// ProcessMI - Examine the instruction for potentai LICM candidate. Also | 
|  | 391 | /// gather register def and frame object update information. | 
|  | 392 | void MachineLICM::ProcessMI(MachineInstr *MI, | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 393 | BitVector &PhysRegDefs, | 
|  | 394 | BitVector &PhysRegClobbers, | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 395 | SmallSet<int, 32> &StoredFIs, | 
|  | 396 | SmallVector<CandidateInfo, 32> &Candidates) { | 
|  | 397 | bool RuledOut = false; | 
| Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 398 | bool HasNonInvariantUse = false; | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 399 | unsigned Def = 0; | 
|  | 400 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { | 
|  | 401 | const MachineOperand &MO = MI->getOperand(i); | 
|  | 402 | if (MO.isFI()) { | 
|  | 403 | // Remember if the instruction stores to the frame index. | 
|  | 404 | int FI = MO.getIndex(); | 
|  | 405 | if (!StoredFIs.count(FI) && | 
|  | 406 | MFI->isSpillSlotObjectIndex(FI) && | 
|  | 407 | InstructionStoresToFI(MI, FI)) | 
|  | 408 | StoredFIs.insert(FI); | 
| Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 409 | HasNonInvariantUse = true; | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 410 | continue; | 
|  | 411 | } | 
|  | 412 |  | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 413 | // We can't hoist an instruction defining a physreg that is clobbered in | 
|  | 414 | // the loop. | 
|  | 415 | if (MO.isRegMask()) { | 
| Jakob Stoklund Olesen | 5e1ac45 | 2012-02-02 23:52:57 +0000 | [diff] [blame] | 416 | PhysRegClobbers.setBitsNotInMask(MO.getRegMask()); | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 417 | continue; | 
|  | 418 | } | 
|  | 419 |  | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 420 | if (!MO.isReg()) | 
|  | 421 | continue; | 
|  | 422 | unsigned Reg = MO.getReg(); | 
|  | 423 | if (!Reg) | 
|  | 424 | continue; | 
|  | 425 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && | 
|  | 426 | "Not expecting virtual register!"); | 
|  | 427 |  | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 428 | if (!MO.isDef()) { | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 429 | if (Reg && (PhysRegDefs.test(Reg) || PhysRegClobbers.test(Reg))) | 
| Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 430 | // If it's using a non-loop-invariant register, then it's obviously not | 
|  | 431 | // safe to hoist. | 
|  | 432 | HasNonInvariantUse = true; | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 433 | continue; | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 434 | } | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 435 |  | 
|  | 436 | if (MO.isImplicit()) { | 
| Craig Topper | 1d32658 | 2012-03-04 10:43:23 +0000 | [diff] [blame] | 437 | for (const uint16_t *AS = TRI->getOverlaps(Reg); *AS; ++AS) | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 438 | PhysRegClobbers.set(*AS); | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 439 | if (!MO.isDead()) | 
|  | 440 | // Non-dead implicit def? This cannot be hoisted. | 
|  | 441 | RuledOut = true; | 
|  | 442 | // No need to check if a dead implicit def is also defined by | 
|  | 443 | // another instruction. | 
|  | 444 | continue; | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | // FIXME: For now, avoid instructions with multiple defs, unless | 
|  | 448 | // it's a dead implicit def. | 
|  | 449 | if (Def) | 
|  | 450 | RuledOut = true; | 
|  | 451 | else | 
|  | 452 | Def = Reg; | 
|  | 453 |  | 
|  | 454 | // If we have already seen another instruction that defines the same | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 455 | // register, then this is not safe.  Two defs is indicated by setting a | 
|  | 456 | // PhysRegClobbers bit. | 
| Craig Topper | 1d32658 | 2012-03-04 10:43:23 +0000 | [diff] [blame] | 457 | for (const uint16_t *AS = TRI->getOverlaps(Reg); *AS; ++AS) { | 
| Jakob Stoklund Olesen | 20948fa | 2012-01-23 21:01:15 +0000 | [diff] [blame] | 458 | if (PhysRegDefs.test(*AS)) | 
|  | 459 | PhysRegClobbers.set(*AS); | 
|  | 460 | if (PhysRegClobbers.test(*AS)) | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 461 | // MI defined register is seen defined by another instruction in | 
|  | 462 | // the loop, it cannot be a LICM candidate. | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 463 | RuledOut = true; | 
| Jakob Stoklund Olesen | 20948fa | 2012-01-23 21:01:15 +0000 | [diff] [blame] | 464 | PhysRegDefs.set(*AS); | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 465 | } | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 466 | } | 
|  | 467 |  | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 468 | // Only consider reloads for now and remats which do not have register | 
|  | 469 | // operands. FIXME: Consider unfold load folding instructions. | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 470 | if (Def && !RuledOut) { | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 471 | int FI = INT_MIN; | 
| Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 472 | if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) || | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 473 | (TII->isLoadFromStackSlot(MI, FI) && MFI->isSpillSlotObjectIndex(FI))) | 
|  | 474 | Candidates.push_back(CandidateInfo(MI, Def, FI)); | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 475 | } | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop | 
|  | 479 | /// invariants out to the preheader. | 
| Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 480 | void MachineLICM::HoistRegionPostRA() { | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 481 | unsigned NumRegs = TRI->getNumRegs(); | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 482 | BitVector PhysRegDefs(NumRegs); // Regs defined once in the loop. | 
|  | 483 | BitVector PhysRegClobbers(NumRegs); // Regs defined more than once. | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 484 |  | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 485 | SmallVector<CandidateInfo, 32> Candidates; | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 486 | SmallSet<int, 32> StoredFIs; | 
|  | 487 |  | 
|  | 488 | // Walk the entire region, count number of defs for each register, and | 
| Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 489 | // collect potential LICM candidates. | 
|  | 490 | const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks(); | 
|  | 491 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { | 
|  | 492 | MachineBasicBlock *BB = Blocks[i]; | 
| Bill Wendling | 918cea2 | 2011-10-12 02:58:01 +0000 | [diff] [blame] | 493 |  | 
|  | 494 | // If the header of the loop containing this basic block is a landing pad, | 
|  | 495 | // then don't try to hoist instructions out of this loop. | 
|  | 496 | const MachineLoop *ML = MLI->getLoopFor(BB); | 
|  | 497 | if (ML && ML->getHeader()->isLandingPad()) continue; | 
|  | 498 |  | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 499 | // Conservatively treat live-in's as an external def. | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 500 | // FIXME: That means a reload that're reused in successor block(s) will not | 
|  | 501 | // be LICM'ed. | 
| Dan Gohman | 9d2d053 | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 502 | for (MachineBasicBlock::livein_iterator I = BB->livein_begin(), | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 503 | E = BB->livein_end(); I != E; ++I) { | 
|  | 504 | unsigned Reg = *I; | 
| Craig Topper | 1d32658 | 2012-03-04 10:43:23 +0000 | [diff] [blame] | 505 | for (const uint16_t *AS = TRI->getOverlaps(Reg); *AS; ++AS) | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 506 | PhysRegDefs.set(*AS); | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 507 | } | 
|  | 508 |  | 
| Evan Cheng | f192ca0 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 509 | SpeculationState = SpeculateUnknown; | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 510 | for (MachineBasicBlock::iterator | 
|  | 511 | MII = BB->begin(), E = BB->end(); MII != E; ++MII) { | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 512 | MachineInstr *MI = &*MII; | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 513 | ProcessMI(MI, PhysRegDefs, PhysRegClobbers, StoredFIs, Candidates); | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 514 | } | 
| Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 515 | } | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 516 |  | 
|  | 517 | // Now evaluate whether the potential candidates qualify. | 
|  | 518 | // 1. Check if the candidate defined register is defined by another | 
|  | 519 | //    instruction in the loop. | 
|  | 520 | // 2. If the candidate is a load from stack slot (always true for now), | 
|  | 521 | //    check if the slot is stored anywhere in the loop. | 
|  | 522 | for (unsigned i = 0, e = Candidates.size(); i != e; ++i) { | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 523 | if (Candidates[i].FI != INT_MIN && | 
|  | 524 | StoredFIs.count(Candidates[i].FI)) | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 525 | continue; | 
|  | 526 |  | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 527 | if (!PhysRegClobbers.test(Candidates[i].Def)) { | 
| Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 528 | bool Safe = true; | 
|  | 529 | MachineInstr *MI = Candidates[i].MI; | 
| Evan Cheng | cce672c | 2010-04-13 20:25:29 +0000 | [diff] [blame] | 530 | for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) { | 
|  | 531 | const MachineOperand &MO = MI->getOperand(j); | 
| Evan Cheng | 87585d7 | 2010-04-13 22:13:34 +0000 | [diff] [blame] | 532 | if (!MO.isReg() || MO.isDef() || !MO.getReg()) | 
| Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 533 | continue; | 
| Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 534 | if (PhysRegDefs.test(MO.getReg()) || | 
|  | 535 | PhysRegClobbers.test(MO.getReg())) { | 
| Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 536 | // If it's using a non-loop-invariant register, then it's obviously | 
|  | 537 | // not safe to hoist. | 
|  | 538 | Safe = false; | 
|  | 539 | break; | 
|  | 540 | } | 
|  | 541 | } | 
|  | 542 | if (Safe) | 
|  | 543 | HoistPostRA(MI, Candidates[i].Def); | 
|  | 544 | } | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 545 | } | 
|  | 546 | } | 
|  | 547 |  | 
| Jakob Stoklund Olesen | 011207a | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 548 | /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the current | 
|  | 549 | /// loop, and make sure it is not killed by any instructions in the loop. | 
| Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 550 | void MachineLICM::AddToLiveIns(unsigned Reg) { | 
|  | 551 | const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks(); | 
| Jakob Stoklund Olesen | 011207a | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 552 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { | 
|  | 553 | MachineBasicBlock *BB = Blocks[i]; | 
|  | 554 | if (!BB->isLiveIn(Reg)) | 
|  | 555 | BB->addLiveIn(Reg); | 
|  | 556 | for (MachineBasicBlock::iterator | 
|  | 557 | MII = BB->begin(), E = BB->end(); MII != E; ++MII) { | 
|  | 558 | MachineInstr *MI = &*MII; | 
|  | 559 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { | 
|  | 560 | MachineOperand &MO = MI->getOperand(i); | 
|  | 561 | if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue; | 
|  | 562 | if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg())) | 
|  | 563 | MO.setIsKill(false); | 
|  | 564 | } | 
|  | 565 | } | 
|  | 566 | } | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 567 | } | 
|  | 568 |  | 
|  | 569 | /// HoistPostRA - When an instruction is found to only use loop invariant | 
|  | 570 | /// operands that is safe to hoist, this instruction is called to do the | 
|  | 571 | /// dirty work. | 
|  | 572 | void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) { | 
| Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 573 | MachineBasicBlock *Preheader = getCurPreheader(); | 
|  | 574 | if (!Preheader) return; | 
|  | 575 |  | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 576 | // Now move the instructions to the predecessor, inserting it before any | 
|  | 577 | // terminator instructions. | 
| Jakob Stoklund Olesen | 9082353 | 2012-01-23 21:01:11 +0000 | [diff] [blame] | 578 | DEBUG(dbgs() << "Hoisting to BB#" << Preheader->getNumber() << " from BB#" | 
|  | 579 | << MI->getParent()->getNumber() << ": " << *MI); | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 580 |  | 
|  | 581 | // Splice the instruction to the preheader. | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 582 | MachineBasicBlock *MBB = MI->getParent(); | 
| Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 583 | Preheader->splice(Preheader->getFirstTerminator(), MBB, MI); | 
| Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 584 |  | 
| Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 585 | // Add register to livein list to all the BBs in the current loop since a | 
| Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 586 | // loop invariant must be kept live throughout the whole loop. This is | 
|  | 587 | // important to ensure later passes do not scavenge the def register. | 
|  | 588 | AddToLiveIns(Def); | 
| Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 589 |  | 
|  | 590 | ++NumPostRAHoisted; | 
|  | 591 | Changed = true; | 
|  | 592 | } | 
|  | 593 |  | 
| Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 594 | // IsGuaranteedToExecute - Check if this mbb is guaranteed to execute. | 
|  | 595 | // If not then a load from this mbb may not be safe to hoist. | 
|  | 596 | bool MachineLICM::IsGuaranteedToExecute(MachineBasicBlock *BB) { | 
| Evan Cheng | f192ca0 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 597 | if (SpeculationState != SpeculateUnknown) | 
|  | 598 | return SpeculationState == SpeculateFalse; | 
| Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 599 |  | 
| Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 600 | if (BB != CurLoop->getHeader()) { | 
|  | 601 | // Check loop exiting blocks. | 
|  | 602 | SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks; | 
|  | 603 | CurLoop->getExitingBlocks(CurrentLoopExitingBlocks); | 
|  | 604 | for (unsigned i = 0, e = CurrentLoopExitingBlocks.size(); i != e; ++i) | 
|  | 605 | if (!DT->dominates(BB, CurrentLoopExitingBlocks[i])) { | 
| Nick Lewycky | 404feb9 | 2011-10-13 01:09:50 +0000 | [diff] [blame] | 606 | SpeculationState = SpeculateTrue; | 
|  | 607 | return false; | 
| Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 608 | } | 
|  | 609 | } | 
|  | 610 |  | 
| Evan Cheng | f192ca0 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 611 | SpeculationState = SpeculateFalse; | 
|  | 612 | return true; | 
| Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 613 | } | 
|  | 614 |  | 
| Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 615 | void MachineLICM::EnterScope(MachineBasicBlock *MBB) { | 
|  | 616 | DEBUG(dbgs() << "Entering: " << MBB->getName() << '\n'); | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 617 |  | 
| Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 618 | // Remember livein register pressure. | 
|  | 619 | BackTrace.push_back(RegPressure); | 
|  | 620 | } | 
| Bill Wendling | 918cea2 | 2011-10-12 02:58:01 +0000 | [diff] [blame] | 621 |  | 
| Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 622 | void MachineLICM::ExitScope(MachineBasicBlock *MBB) { | 
|  | 623 | DEBUG(dbgs() << "Exiting: " << MBB->getName() << '\n'); | 
|  | 624 | BackTrace.pop_back(); | 
|  | 625 | } | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 626 |  | 
| Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 627 | /// ExitScopeIfDone - Destroy scope for the MBB that corresponds to the given | 
|  | 628 | /// dominator tree node if its a leaf or all of its children are done. Walk | 
|  | 629 | /// up the dominator tree to destroy ancestors which are now done. | 
|  | 630 | void MachineLICM::ExitScopeIfDone(MachineDomTreeNode *Node, | 
| Evan Cheng | da46832 | 2012-01-10 22:27:32 +0000 | [diff] [blame] | 631 | DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren, | 
|  | 632 | DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) { | 
| Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 633 | if (OpenChildren[Node]) | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 634 | return; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 635 |  | 
| Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 636 | // Pop scope. | 
|  | 637 | ExitScope(Node->getBlock()); | 
|  | 638 |  | 
|  | 639 | // Now traverse upwards to pop ancestors whose offsprings are all done. | 
|  | 640 | while (MachineDomTreeNode *Parent = ParentMap[Node]) { | 
|  | 641 | unsigned Left = --OpenChildren[Parent]; | 
|  | 642 | if (Left != 0) | 
|  | 643 | break; | 
|  | 644 | ExitScope(Parent->getBlock()); | 
|  | 645 | Node = Parent; | 
|  | 646 | } | 
|  | 647 | } | 
|  | 648 |  | 
|  | 649 | /// HoistOutOfLoop - Walk the specified loop in the CFG (defined by all | 
|  | 650 | /// blocks dominated by the specified header block, and that are in the | 
|  | 651 | /// current loop) in depth first order w.r.t the DominatorTree. This allows | 
|  | 652 | /// us to visit definitions before uses, allowing us to hoist a loop body in | 
|  | 653 | /// one pass without iteration. | 
|  | 654 | /// | 
|  | 655 | void MachineLICM::HoistOutOfLoop(MachineDomTreeNode *HeaderN) { | 
|  | 656 | SmallVector<MachineDomTreeNode*, 32> Scopes; | 
|  | 657 | SmallVector<MachineDomTreeNode*, 8> WorkList; | 
|  | 658 | DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap; | 
|  | 659 | DenseMap<MachineDomTreeNode*, unsigned> OpenChildren; | 
|  | 660 |  | 
|  | 661 | // Perform a DFS walk to determine the order of visit. | 
|  | 662 | WorkList.push_back(HeaderN); | 
|  | 663 | do { | 
|  | 664 | MachineDomTreeNode *Node = WorkList.pop_back_val(); | 
|  | 665 | assert(Node != 0 && "Null dominator tree node?"); | 
|  | 666 | MachineBasicBlock *BB = Node->getBlock(); | 
|  | 667 |  | 
|  | 668 | // If the header of the loop containing this basic block is a landing pad, | 
|  | 669 | // then don't try to hoist instructions out of this loop. | 
|  | 670 | const MachineLoop *ML = MLI->getLoopFor(BB); | 
|  | 671 | if (ML && ML->getHeader()->isLandingPad()) | 
|  | 672 | continue; | 
|  | 673 |  | 
|  | 674 | // If this subregion is not in the top level loop at all, exit. | 
|  | 675 | if (!CurLoop->contains(BB)) | 
|  | 676 | continue; | 
|  | 677 |  | 
|  | 678 | Scopes.push_back(Node); | 
|  | 679 | const std::vector<MachineDomTreeNode*> &Children = Node->getChildren(); | 
|  | 680 | unsigned NumChildren = Children.size(); | 
|  | 681 |  | 
|  | 682 | // Don't hoist things out of a large switch statement.  This often causes | 
|  | 683 | // code to be hoisted that wasn't going to be executed, and increases | 
|  | 684 | // register pressure in a situation where it's likely to matter. | 
|  | 685 | if (BB->succ_size() >= 25) | 
|  | 686 | NumChildren = 0; | 
|  | 687 |  | 
|  | 688 | OpenChildren[Node] = NumChildren; | 
|  | 689 | // Add children in reverse order as then the next popped worklist node is | 
|  | 690 | // the first child of this node.  This means we ultimately traverse the | 
|  | 691 | // DOM tree in exactly the same order as if we'd recursed. | 
|  | 692 | for (int i = (int)NumChildren-1; i >= 0; --i) { | 
|  | 693 | MachineDomTreeNode *Child = Children[i]; | 
|  | 694 | ParentMap[Child] = Node; | 
|  | 695 | WorkList.push_back(Child); | 
|  | 696 | } | 
|  | 697 | } while (!WorkList.empty()); | 
|  | 698 |  | 
|  | 699 | if (Scopes.size() != 0) { | 
|  | 700 | MachineBasicBlock *Preheader = getCurPreheader(); | 
|  | 701 | if (!Preheader) | 
|  | 702 | return; | 
|  | 703 |  | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 704 | // Compute registers which are livein into the loop headers. | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 705 | RegSeen.clear(); | 
|  | 706 | BackTrace.clear(); | 
|  | 707 | InitRegPressure(Preheader); | 
| Daniel Dunbar | 418204e | 2010-10-19 17:14:24 +0000 | [diff] [blame] | 708 | } | 
| Evan Cheng | 8249dfe | 2010-10-19 00:55:07 +0000 | [diff] [blame] | 709 |  | 
| Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 710 | // Now perform LICM. | 
|  | 711 | for (unsigned i = 0, e = Scopes.size(); i != e; ++i) { | 
|  | 712 | MachineDomTreeNode *Node = Scopes[i]; | 
|  | 713 | MachineBasicBlock *MBB = Node->getBlock(); | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 714 |  | 
| Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 715 | MachineBasicBlock *Preheader = getCurPreheader(); | 
|  | 716 | if (!Preheader) | 
|  | 717 | continue; | 
|  | 718 |  | 
|  | 719 | EnterScope(MBB); | 
|  | 720 |  | 
|  | 721 | // Process the block | 
|  | 722 | SpeculationState = SpeculateUnknown; | 
|  | 723 | for (MachineBasicBlock::iterator | 
|  | 724 | MII = MBB->begin(), E = MBB->end(); MII != E; ) { | 
|  | 725 | MachineBasicBlock::iterator NextMII = MII; ++NextMII; | 
|  | 726 | MachineInstr *MI = &*MII; | 
|  | 727 | if (!Hoist(MI, Preheader)) | 
|  | 728 | UpdateRegPressure(MI); | 
|  | 729 | MII = NextMII; | 
|  | 730 | } | 
|  | 731 |  | 
|  | 732 | // If it's a leaf node, it's done. Traverse upwards to pop ancestors. | 
|  | 733 | ExitScopeIfDone(Node, OpenChildren, ParentMap); | 
| Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 734 | } | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 735 | } | 
|  | 736 |  | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 737 | static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) { | 
|  | 738 | return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg()); | 
|  | 739 | } | 
|  | 740 |  | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 741 | /// getRegisterClassIDAndCost - For a given MI, register, and the operand | 
|  | 742 | /// index, return the ID and cost of its representative register class. | 
|  | 743 | void | 
|  | 744 | MachineLICM::getRegisterClassIDAndCost(const MachineInstr *MI, | 
|  | 745 | unsigned Reg, unsigned OpIdx, | 
|  | 746 | unsigned &RCId, unsigned &RCCost) const { | 
|  | 747 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); | 
|  | 748 | EVT VT = *RC->vt_begin(); | 
| Owen Anderson | ca2f78a | 2011-11-16 01:02:57 +0000 | [diff] [blame] | 749 | if (VT == MVT::Untyped) { | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 750 | RCId = RC->getID(); | 
|  | 751 | RCCost = 1; | 
|  | 752 | } else { | 
|  | 753 | RCId = TLI->getRepRegClassFor(VT)->getID(); | 
|  | 754 | RCCost = TLI->getRepRegClassCostFor(VT); | 
|  | 755 | } | 
|  | 756 | } | 
| Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 757 |  | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 758 | /// InitRegPressure - Find all virtual register references that are liveout of | 
|  | 759 | /// the preheader to initialize the starting "register pressure". Note this | 
|  | 760 | /// does not count live through (livein but not used) registers. | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 761 | void MachineLICM::InitRegPressure(MachineBasicBlock *BB) { | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 762 | std::fill(RegPressure.begin(), RegPressure.end(), 0); | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 763 |  | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 764 | // If the preheader has only a single predecessor and it ends with a | 
|  | 765 | // fallthrough or an unconditional branch, then scan its predecessor for live | 
|  | 766 | // defs as well. This happens whenever the preheader is created by splitting | 
|  | 767 | // the critical edge from the loop predecessor to the loop header. | 
|  | 768 | if (BB->pred_size() == 1) { | 
|  | 769 | MachineBasicBlock *TBB = 0, *FBB = 0; | 
|  | 770 | SmallVector<MachineOperand, 4> Cond; | 
|  | 771 | if (!TII->AnalyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty()) | 
|  | 772 | InitRegPressure(*BB->pred_begin()); | 
|  | 773 | } | 
|  | 774 |  | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 775 | for (MachineBasicBlock::iterator MII = BB->begin(), E = BB->end(); | 
|  | 776 | MII != E; ++MII) { | 
|  | 777 | MachineInstr *MI = &*MII; | 
|  | 778 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { | 
|  | 779 | const MachineOperand &MO = MI->getOperand(i); | 
|  | 780 | if (!MO.isReg() || MO.isImplicit()) | 
|  | 781 | continue; | 
|  | 782 | unsigned Reg = MO.getReg(); | 
| Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 783 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 784 | continue; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 785 |  | 
| Andrew Trick | 2006bbe | 2010-10-19 02:50:50 +0000 | [diff] [blame] | 786 | bool isNew = RegSeen.insert(Reg); | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 787 | unsigned RCId, RCCost; | 
|  | 788 | getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost); | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 789 | if (MO.isDef()) | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 790 | RegPressure[RCId] += RCCost; | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 791 | else { | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 792 | bool isKill = isOperandKill(MO, MRI); | 
|  | 793 | if (isNew && !isKill) | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 794 | // Haven't seen this, it must be a livein. | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 795 | RegPressure[RCId] += RCCost; | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 796 | else if (!isNew && isKill) | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 797 | RegPressure[RCId] -= RCCost; | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 798 | } | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 799 | } | 
|  | 800 | } | 
|  | 801 | } | 
|  | 802 |  | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 803 | /// UpdateRegPressure - Update estimate of register pressure after the | 
|  | 804 | /// specified instruction. | 
|  | 805 | void MachineLICM::UpdateRegPressure(const MachineInstr *MI) { | 
|  | 806 | if (MI->isImplicitDef()) | 
|  | 807 | return; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 808 |  | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 809 | SmallVector<unsigned, 4> Defs; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 810 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { | 
|  | 811 | const MachineOperand &MO = MI->getOperand(i); | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 812 | if (!MO.isReg() || MO.isImplicit()) | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 813 | continue; | 
|  | 814 | unsigned Reg = MO.getReg(); | 
| Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 815 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 816 | continue; | 
|  | 817 |  | 
| Andrew Trick | 2006bbe | 2010-10-19 02:50:50 +0000 | [diff] [blame] | 818 | bool isNew = RegSeen.insert(Reg); | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 819 | if (MO.isDef()) | 
|  | 820 | Defs.push_back(Reg); | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 821 | else if (!isNew && isOperandKill(MO, MRI)) { | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 822 | unsigned RCId, RCCost; | 
|  | 823 | getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost); | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 824 | if (RCCost > RegPressure[RCId]) | 
|  | 825 | RegPressure[RCId] = 0; | 
|  | 826 | else | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 827 | RegPressure[RCId] -= RCCost; | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 828 | } | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 829 | } | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 830 |  | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 831 | unsigned Idx = 0; | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 832 | while (!Defs.empty()) { | 
|  | 833 | unsigned Reg = Defs.pop_back_val(); | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 834 | unsigned RCId, RCCost; | 
|  | 835 | getRegisterClassIDAndCost(MI, Reg, Idx, RCId, RCCost); | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 836 | RegPressure[RCId] += RCCost; | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 837 | ++Idx; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 838 | } | 
|  | 839 | } | 
|  | 840 |  | 
| Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 841 | /// isLoadFromGOTOrConstantPool - Return true if this machine instruction | 
| Devang Patel | 1d8ab46 | 2011-10-20 17:42:23 +0000 | [diff] [blame] | 842 | /// loads from global offset table or constant pool. | 
|  | 843 | static bool isLoadFromGOTOrConstantPool(MachineInstr &MI) { | 
| Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 844 | assert (MI.mayLoad() && "Expected MI that loads!"); | 
| Devang Patel | 69a4565 | 2011-10-17 17:35:01 +0000 | [diff] [blame] | 845 | for (MachineInstr::mmo_iterator I = MI.memoperands_begin(), | 
| Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 846 | E = MI.memoperands_end(); I != E; ++I) { | 
| Devang Patel | 69a4565 | 2011-10-17 17:35:01 +0000 | [diff] [blame] | 847 | if (const Value *V = (*I)->getValue()) { | 
|  | 848 | if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) | 
| Devang Patel | 1d8ab46 | 2011-10-20 17:42:23 +0000 | [diff] [blame] | 849 | if (PSV == PSV->getGOT() || PSV == PSV->getConstantPool()) | 
| Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 850 | return true; | 
| Devang Patel | 69a4565 | 2011-10-17 17:35:01 +0000 | [diff] [blame] | 851 | } | 
|  | 852 | } | 
|  | 853 | return false; | 
|  | 854 | } | 
|  | 855 |  | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 856 | /// IsLICMCandidate - Returns true if the instruction may be a suitable | 
|  | 857 | /// candidate for LICM. e.g. If the instruction is a call, then it's obviously | 
|  | 858 | /// not safe to hoist it. | 
|  | 859 | bool MachineLICM::IsLICMCandidate(MachineInstr &I) { | 
| Chris Lattner | 0b7ae20 | 2010-07-12 00:00:35 +0000 | [diff] [blame] | 860 | // Check if it's safe to move the instruction. | 
|  | 861 | bool DontMoveAcrossStore = true; | 
|  | 862 | if (!I.isSafeToMove(TII, AA, DontMoveAcrossStore)) | 
| Chris Lattner | c8226f3 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 863 | return false; | 
| Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 864 |  | 
|  | 865 | // If it is load then check if it is guaranteed to execute by making sure that | 
|  | 866 | // it dominates all exiting blocks. If it doesn't, then there is a path out of | 
| Devang Patel | 830c776 | 2011-10-20 17:31:18 +0000 | [diff] [blame] | 867 | // the loop which does not execute this load, so we can't hoist it. Loads | 
|  | 868 | // from constant memory are not safe to speculate all the time, for example | 
|  | 869 | // indexed load from a jump table. | 
| Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 870 | // Stores and side effects are already checked by isSafeToMove. | 
| Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 871 | if (I.mayLoad() && !isLoadFromGOTOrConstantPool(I) && | 
| Devang Patel | 69a4565 | 2011-10-17 17:35:01 +0000 | [diff] [blame] | 872 | !IsGuaranteedToExecute(I.getParent())) | 
| Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 873 | return false; | 
|  | 874 |  | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 875 | return true; | 
|  | 876 | } | 
|  | 877 |  | 
|  | 878 | /// IsLoopInvariantInst - Returns true if the instruction is loop | 
|  | 879 | /// invariant. I.e., all virtual register operands are defined outside of the | 
|  | 880 | /// loop, physical registers aren't accessed explicitly, and there are no side | 
|  | 881 | /// effects that aren't captured by the operands or other flags. | 
| Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 882 | /// | 
| Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 883 | bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) { | 
|  | 884 | if (!IsLICMCandidate(I)) | 
|  | 885 | return false; | 
| Bill Wendling | 2823eae | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 886 |  | 
| Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 887 | // The instruction is loop invariant if all of its operands are. | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 888 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { | 
|  | 889 | const MachineOperand &MO = I.getOperand(i); | 
|  | 890 |  | 
| Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 891 | if (!MO.isReg()) | 
| Bill Wendling | cd01e89 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 892 | continue; | 
|  | 893 |  | 
| Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 894 | unsigned Reg = MO.getReg(); | 
|  | 895 | if (Reg == 0) continue; | 
|  | 896 |  | 
|  | 897 | // Don't hoist an instruction that uses or defines a physical register. | 
| Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 898 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { | 
| Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 899 | if (MO.isUse()) { | 
|  | 900 | // If the physreg has no defs anywhere, it's just an ambient register | 
| Dan Gohman | 2f5bdcb | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 901 | // and we can freely move its uses. Alternatively, if it's allocatable, | 
|  | 902 | // it could get allocated to something with a def during allocation. | 
| Jakob Stoklund Olesen | 86ae07f | 2012-01-16 22:34:08 +0000 | [diff] [blame] | 903 | if (!MRI->isConstantPhysReg(Reg, *I.getParent()->getParent())) | 
| Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 904 | return false; | 
| Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 905 | // Otherwise it's safe to move. | 
|  | 906 | continue; | 
|  | 907 | } else if (!MO.isDead()) { | 
|  | 908 | // A def that isn't dead. We can't move it. | 
|  | 909 | return false; | 
| Dan Gohman | 6fb6a59 | 2010-02-28 00:08:44 +0000 | [diff] [blame] | 910 | } else if (CurLoop->getHeader()->isLiveIn(Reg)) { | 
|  | 911 | // If the reg is live into the loop, we can't hoist an instruction | 
|  | 912 | // which would clobber it. | 
|  | 913 | return false; | 
| Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 914 | } | 
|  | 915 | } | 
| Bill Wendling | cd01e89 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 916 |  | 
|  | 917 | if (!MO.isUse()) | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 918 | continue; | 
|  | 919 |  | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 920 | assert(MRI->getVRegDef(Reg) && | 
| Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 921 | "Machine instr not mapped for this vreg?!"); | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 922 |  | 
|  | 923 | // If the loop contains the definition of an operand, then the instruction | 
|  | 924 | // isn't loop invariant. | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 925 | if (CurLoop->contains(MRI->getVRegDef(Reg))) | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 926 | return false; | 
|  | 927 | } | 
|  | 928 |  | 
|  | 929 | // If we got this far, the instruction is loop invariant! | 
|  | 930 | return true; | 
|  | 931 | } | 
|  | 932 |  | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 933 |  | 
| Evan Cheng | ef42bea | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 934 | /// HasAnyPHIUse - Return true if the specified register is used by any | 
|  | 935 | /// phi node. | 
|  | 936 | bool MachineLICM::HasAnyPHIUse(unsigned Reg) const { | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 937 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg), | 
|  | 938 | UE = MRI->use_end(); UI != UE; ++UI) { | 
| Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 939 | MachineInstr *UseMI = &*UI; | 
| Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 940 | if (UseMI->isPHI()) | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 941 | return true; | 
| Evan Cheng | ef42bea | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 942 | // Look pass copies as well. | 
|  | 943 | if (UseMI->isCopy()) { | 
|  | 944 | unsigned Def = UseMI->getOperand(0).getReg(); | 
|  | 945 | if (TargetRegisterInfo::isVirtualRegister(Def) && | 
|  | 946 | HasAnyPHIUse(Def)) | 
|  | 947 | return true; | 
|  | 948 | } | 
| Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 949 | } | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 950 | return false; | 
| Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 951 | } | 
|  | 952 |  | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 953 | /// HasHighOperandLatency - Compute operand latency between a def of 'Reg' | 
|  | 954 | /// and an use in the current loop, return true if the target considered | 
|  | 955 | /// it 'high'. | 
|  | 956 | bool MachineLICM::HasHighOperandLatency(MachineInstr &MI, | 
| Evan Cheng | e96b8d7 | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 957 | unsigned DefIdx, unsigned Reg) const { | 
|  | 958 | if (!InstrItins || InstrItins->isEmpty() || MRI->use_nodbg_empty(Reg)) | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 959 | return false; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 960 |  | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 961 | for (MachineRegisterInfo::use_nodbg_iterator I = MRI->use_nodbg_begin(Reg), | 
|  | 962 | E = MRI->use_nodbg_end(); I != E; ++I) { | 
|  | 963 | MachineInstr *UseMI = &*I; | 
| Evan Cheng | e96b8d7 | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 964 | if (UseMI->isCopyLike()) | 
|  | 965 | continue; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 966 | if (!CurLoop->contains(UseMI->getParent())) | 
|  | 967 | continue; | 
|  | 968 | for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) { | 
|  | 969 | const MachineOperand &MO = UseMI->getOperand(i); | 
|  | 970 | if (!MO.isReg() || !MO.isUse()) | 
|  | 971 | continue; | 
|  | 972 | unsigned MOReg = MO.getReg(); | 
|  | 973 | if (MOReg != Reg) | 
|  | 974 | continue; | 
|  | 975 |  | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 976 | if (TII->hasHighOperandLatency(InstrItins, MRI, &MI, DefIdx, UseMI, i)) | 
|  | 977 | return true; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 978 | } | 
|  | 979 |  | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 980 | // Only look at the first in loop use. | 
|  | 981 | break; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 982 | } | 
|  | 983 |  | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 984 | return false; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 985 | } | 
|  | 986 |  | 
| Evan Cheng | e96b8d7 | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 987 | /// IsCheapInstruction - Return true if the instruction is marked "cheap" or | 
|  | 988 | /// the operand latency between its def and a use is one or less. | 
|  | 989 | bool MachineLICM::IsCheapInstruction(MachineInstr &MI) const { | 
| Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 990 | if (MI.isAsCheapAsAMove() || MI.isCopyLike()) | 
| Evan Cheng | e96b8d7 | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 991 | return true; | 
|  | 992 | if (!InstrItins || InstrItins->isEmpty()) | 
|  | 993 | return false; | 
|  | 994 |  | 
|  | 995 | bool isCheap = false; | 
|  | 996 | unsigned NumDefs = MI.getDesc().getNumDefs(); | 
|  | 997 | for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) { | 
|  | 998 | MachineOperand &DefMO = MI.getOperand(i); | 
|  | 999 | if (!DefMO.isReg() || !DefMO.isDef()) | 
|  | 1000 | continue; | 
|  | 1001 | --NumDefs; | 
|  | 1002 | unsigned Reg = DefMO.getReg(); | 
|  | 1003 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) | 
|  | 1004 | continue; | 
|  | 1005 |  | 
|  | 1006 | if (!TII->hasLowDefLatency(InstrItins, &MI, i)) | 
|  | 1007 | return false; | 
|  | 1008 | isCheap = true; | 
|  | 1009 | } | 
|  | 1010 |  | 
|  | 1011 | return isCheap; | 
|  | 1012 | } | 
|  | 1013 |  | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1014 | /// CanCauseHighRegPressure - Visit BBs from header to current BB, check | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1015 | /// if hoisting an instruction of the given cost matrix can cause high | 
|  | 1016 | /// register pressure. | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1017 | bool MachineLICM::CanCauseHighRegPressure(DenseMap<unsigned, int> &Cost) { | 
|  | 1018 | for (DenseMap<unsigned, int>::iterator CI = Cost.begin(), CE = Cost.end(); | 
|  | 1019 | CI != CE; ++CI) { | 
| Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 1020 | if (CI->second <= 0) | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1021 | continue; | 
|  | 1022 |  | 
|  | 1023 | unsigned RCId = CI->first; | 
| Pete Cooper | 1c3b1ef | 2011-12-22 02:13:25 +0000 | [diff] [blame] | 1024 | unsigned Limit = RegLimit[RCId]; | 
|  | 1025 | int Cost = CI->second; | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1026 | for (unsigned i = BackTrace.size(); i != 0; --i) { | 
|  | 1027 | SmallVector<unsigned, 8> &RP = BackTrace[i-1]; | 
| Pete Cooper | 1c3b1ef | 2011-12-22 02:13:25 +0000 | [diff] [blame] | 1028 | if (RP[RCId] + Cost >= Limit) | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1029 | return true; | 
|  | 1030 | } | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1031 | } | 
|  | 1032 |  | 
|  | 1033 | return false; | 
|  | 1034 | } | 
|  | 1035 |  | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1036 | /// UpdateBackTraceRegPressure - Traverse the back trace from header to the | 
|  | 1037 | /// current block and update their register pressures to reflect the effect | 
|  | 1038 | /// of hoisting MI from the current block to the preheader. | 
|  | 1039 | void MachineLICM::UpdateBackTraceRegPressure(const MachineInstr *MI) { | 
|  | 1040 | if (MI->isImplicitDef()) | 
|  | 1041 | return; | 
|  | 1042 |  | 
|  | 1043 | // First compute the 'cost' of the instruction, i.e. its contribution | 
|  | 1044 | // to register pressure. | 
|  | 1045 | DenseMap<unsigned, int> Cost; | 
|  | 1046 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { | 
|  | 1047 | const MachineOperand &MO = MI->getOperand(i); | 
|  | 1048 | if (!MO.isReg() || MO.isImplicit()) | 
|  | 1049 | continue; | 
|  | 1050 | unsigned Reg = MO.getReg(); | 
| Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 1051 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1052 | continue; | 
|  | 1053 |  | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 1054 | unsigned RCId, RCCost; | 
|  | 1055 | getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost); | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1056 | if (MO.isDef()) { | 
|  | 1057 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); | 
|  | 1058 | if (CI != Cost.end()) | 
|  | 1059 | CI->second += RCCost; | 
|  | 1060 | else | 
|  | 1061 | Cost.insert(std::make_pair(RCId, RCCost)); | 
|  | 1062 | } else if (isOperandKill(MO, MRI)) { | 
|  | 1063 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); | 
|  | 1064 | if (CI != Cost.end()) | 
|  | 1065 | CI->second -= RCCost; | 
|  | 1066 | else | 
|  | 1067 | Cost.insert(std::make_pair(RCId, -RCCost)); | 
|  | 1068 | } | 
|  | 1069 | } | 
|  | 1070 |  | 
|  | 1071 | // Update register pressure of blocks from loop header to current block. | 
|  | 1072 | for (unsigned i = 0, e = BackTrace.size(); i != e; ++i) { | 
|  | 1073 | SmallVector<unsigned, 8> &RP = BackTrace[i]; | 
|  | 1074 | for (DenseMap<unsigned, int>::iterator CI = Cost.begin(), CE = Cost.end(); | 
|  | 1075 | CI != CE; ++CI) { | 
|  | 1076 | unsigned RCId = CI->first; | 
|  | 1077 | RP[RCId] += CI->second; | 
|  | 1078 | } | 
|  | 1079 | } | 
|  | 1080 | } | 
|  | 1081 |  | 
| Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1082 | /// IsProfitableToHoist - Return true if it is potentially profitable to hoist | 
|  | 1083 | /// the given loop invariant. | 
| Evan Cheng | 73f9a9e | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 1084 | bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) { | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1085 | if (MI.isImplicitDef()) | 
|  | 1086 | return true; | 
|  | 1087 |  | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1088 | // If the instruction is cheap, only hoist if it is re-materilizable. LICM | 
|  | 1089 | // will increase register pressure. It's probably not worth it if the | 
|  | 1090 | // instruction is cheap. | 
| Evan Cheng | b39a9fd | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1091 | // Also hoist loads from constant memory, e.g. load from stubs, GOT. Hoisting | 
|  | 1092 | // these tend to help performance in low register pressure situation. The | 
|  | 1093 | // trade off is it may cause spill in high pressure situation. It will end up | 
|  | 1094 | // adding a store in the loop preheader. But the reload is no more expensive. | 
|  | 1095 | // The side benefit is these loads are frequently CSE'ed. | 
| Evan Cheng | e96b8d7 | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 1096 | if (IsCheapInstruction(MI)) { | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1097 | if (!TII->isTriviallyReMaterializable(&MI, AA)) | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1098 | return false; | 
|  | 1099 | } else { | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1100 | // Estimate register pressure to determine whether to LICM the instruction. | 
| Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 1101 | // In low register pressure situation, we can be more aggressive about | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1102 | // hoisting. Also, favors hoisting long latency instructions even in | 
|  | 1103 | // moderately high pressure situation. | 
| Dan Gohman | 4162e3e | 2010-11-11 18:08:43 +0000 | [diff] [blame] | 1104 | // FIXME: If there are long latency loop-invariant instructions inside the | 
|  | 1105 | // loop at this point, why didn't the optimizer's LICM hoist them? | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1106 | DenseMap<unsigned, int> Cost; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1107 | for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) { | 
|  | 1108 | const MachineOperand &MO = MI.getOperand(i); | 
|  | 1109 | if (!MO.isReg() || MO.isImplicit()) | 
|  | 1110 | continue; | 
|  | 1111 | unsigned Reg = MO.getReg(); | 
| Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 1112 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1113 | continue; | 
| Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 1114 |  | 
|  | 1115 | unsigned RCId, RCCost; | 
|  | 1116 | getRegisterClassIDAndCost(&MI, Reg, i, RCId, RCCost); | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1117 | if (MO.isDef()) { | 
| Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1118 | if (HasHighOperandLatency(MI, i, Reg)) { | 
|  | 1119 | ++NumHighLatency; | 
|  | 1120 | return true; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1121 | } | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1122 |  | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1123 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1124 | if (CI != Cost.end()) | 
|  | 1125 | CI->second += RCCost; | 
|  | 1126 | else | 
|  | 1127 | Cost.insert(std::make_pair(RCId, RCCost)); | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1128 | } else if (isOperandKill(MO, MRI)) { | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1129 | // Is a virtual register use is a kill, hoisting it out of the loop | 
|  | 1130 | // may actually reduce register pressure or be register pressure | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1131 | // neutral. | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1132 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); | 
|  | 1133 | if (CI != Cost.end()) | 
|  | 1134 | CI->second -= RCCost; | 
|  | 1135 | else | 
|  | 1136 | Cost.insert(std::make_pair(RCId, -RCCost)); | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1137 | } | 
|  | 1138 | } | 
|  | 1139 |  | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1140 | // Visit BBs from header to current BB, if hoisting this doesn't cause | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1141 | // high register pressure, then it's safe to proceed. | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1142 | if (!CanCauseHighRegPressure(Cost)) { | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1143 | ++NumLowRP; | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1144 | return true; | 
| Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1145 | } | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1146 |  | 
| Evan Cheng | b35afca | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 1147 | // Do not "speculate" in high register pressure situation. If an | 
| Evan Cheng | f192ca0 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 1148 | // instruction is not guaranteed to be executed in the loop, it's best to be | 
|  | 1149 | // conservative. | 
| Evan Cheng | b35afca | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 1150 | if (AvoidSpeculation && | 
|  | 1151 | (!IsGuaranteedToExecute(MI.getParent()) && !MayCSE(&MI))) | 
|  | 1152 | return false; | 
|  | 1153 |  | 
| Chad Rosier | 248c299 | 2012-02-28 00:23:01 +0000 | [diff] [blame] | 1154 | // High register pressure situation, only hoist if the instruction is going | 
|  | 1155 | // to be remat'ed. | 
| Evan Cheng | b35afca | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 1156 | if (!TII->isTriviallyReMaterializable(&MI, AA) && | 
|  | 1157 | !MI.isInvariantLoad(AA)) | 
| Evan Cheng | b39a9fd | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1158 | return false; | 
| Evan Cheng | b39a9fd | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1159 | } | 
| Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1160 |  | 
| Evan Cheng | ef42bea | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 1161 | // If result(s) of this instruction is used by PHIs outside of the loop, then | 
|  | 1162 | // don't hoist it if the instruction because it will introduce an extra copy. | 
| Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1163 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { | 
|  | 1164 | const MachineOperand &MO = MI.getOperand(i); | 
|  | 1165 | if (!MO.isReg() || !MO.isDef()) | 
|  | 1166 | continue; | 
| Evan Cheng | ef42bea | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 1167 | if (HasAnyPHIUse(MO.getReg())) | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1168 | return false; | 
| Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1169 | } | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1170 |  | 
|  | 1171 | return true; | 
|  | 1172 | } | 
|  | 1173 |  | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1174 | MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) { | 
| Evan Cheng | 4ac0d16 | 2010-10-08 18:59:19 +0000 | [diff] [blame] | 1175 | // Don't unfold simple loads. | 
| Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1176 | if (MI->canFoldAsLoad()) | 
| Evan Cheng | 4ac0d16 | 2010-10-08 18:59:19 +0000 | [diff] [blame] | 1177 | return 0; | 
|  | 1178 |  | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1179 | // If not, we may be able to unfold a load and hoist that. | 
|  | 1180 | // First test whether the instruction is loading from an amenable | 
|  | 1181 | // memory location. | 
| Evan Cheng | b8b0ad8 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 1182 | if (!MI->isInvariantLoad(AA)) | 
| Evan Cheng | b39a9fd | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1183 | return 0; | 
|  | 1184 |  | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1185 | // Next determine the register class for a temporary register. | 
| Dan Gohman | 49fa51d | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 1186 | unsigned LoadRegIndex; | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1187 | unsigned NewOpc = | 
|  | 1188 | TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(), | 
|  | 1189 | /*UnfoldLoad=*/true, | 
| Dan Gohman | 49fa51d | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 1190 | /*UnfoldStore=*/false, | 
|  | 1191 | &LoadRegIndex); | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1192 | if (NewOpc == 0) return 0; | 
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1193 | const MCInstrDesc &MID = TII->get(NewOpc); | 
|  | 1194 | if (MID.getNumDefs() != 1) return 0; | 
|  | 1195 | const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI); | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1196 | // Ok, we're unfolding. Create a temporary register and do the unfold. | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1197 | unsigned Reg = MRI->createVirtualRegister(RC); | 
| Evan Cheng | b39a9fd | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1198 |  | 
|  | 1199 | MachineFunction &MF = *MI->getParent()->getParent(); | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1200 | SmallVector<MachineInstr *, 2> NewMIs; | 
|  | 1201 | bool Success = | 
|  | 1202 | TII->unfoldMemoryOperand(MF, MI, Reg, | 
|  | 1203 | /*UnfoldLoad=*/true, /*UnfoldStore=*/false, | 
|  | 1204 | NewMIs); | 
|  | 1205 | (void)Success; | 
|  | 1206 | assert(Success && | 
|  | 1207 | "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold " | 
|  | 1208 | "succeeded!"); | 
|  | 1209 | assert(NewMIs.size() == 2 && | 
|  | 1210 | "Unfolded a load into multiple instructions!"); | 
|  | 1211 | MachineBasicBlock *MBB = MI->getParent(); | 
| Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 1212 | MachineBasicBlock::iterator Pos = MI; | 
|  | 1213 | MBB->insert(Pos, NewMIs[0]); | 
|  | 1214 | MBB->insert(Pos, NewMIs[1]); | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1215 | // If unfolding produced a load that wasn't loop-invariant or profitable to | 
|  | 1216 | // hoist, discard the new instructions and bail. | 
| Evan Cheng | 73f9a9e | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 1217 | if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) { | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1218 | NewMIs[0]->eraseFromParent(); | 
|  | 1219 | NewMIs[1]->eraseFromParent(); | 
|  | 1220 | return 0; | 
|  | 1221 | } | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1222 |  | 
|  | 1223 | // Update register pressure for the unfolded instruction. | 
|  | 1224 | UpdateRegPressure(NewMIs[1]); | 
|  | 1225 |  | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1226 | // Otherwise we successfully unfolded a load that we can hoist. | 
|  | 1227 | MI->eraseFromParent(); | 
|  | 1228 | return NewMIs[0]; | 
|  | 1229 | } | 
|  | 1230 |  | 
| Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1231 | void MachineLICM::InitCSEMap(MachineBasicBlock *BB) { | 
|  | 1232 | for (MachineBasicBlock::iterator I = BB->begin(),E = BB->end(); I != E; ++I) { | 
|  | 1233 | const MachineInstr *MI = &*I; | 
| Evan Cheng | b8b0ad8 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 1234 | unsigned Opcode = MI->getOpcode(); | 
|  | 1235 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator | 
|  | 1236 | CI = CSEMap.find(Opcode); | 
|  | 1237 | if (CI != CSEMap.end()) | 
|  | 1238 | CI->second.push_back(MI); | 
|  | 1239 | else { | 
|  | 1240 | std::vector<const MachineInstr*> CSEMIs; | 
|  | 1241 | CSEMIs.push_back(MI); | 
|  | 1242 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); | 
| Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1243 | } | 
|  | 1244 | } | 
|  | 1245 | } | 
|  | 1246 |  | 
| Evan Cheng | 7ff8319 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1247 | const MachineInstr* | 
|  | 1248 | MachineLICM::LookForDuplicate(const MachineInstr *MI, | 
|  | 1249 | std::vector<const MachineInstr*> &PrevMIs) { | 
| Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1250 | for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) { | 
|  | 1251 | const MachineInstr *PrevMI = PrevMIs[i]; | 
| Evan Cheng | b8b0ad8 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 1252 | if (TII->produceSameValue(MI, PrevMI, (PreRegAlloc ? MRI : 0))) | 
| Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1253 | return PrevMI; | 
|  | 1254 | } | 
|  | 1255 | return 0; | 
|  | 1256 | } | 
|  | 1257 |  | 
|  | 1258 | bool MachineLICM::EliminateCSE(MachineInstr *MI, | 
|  | 1259 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) { | 
| Evan Cheng | d542414 | 2010-07-14 01:22:19 +0000 | [diff] [blame] | 1260 | // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate | 
|  | 1261 | // the undef property onto uses. | 
|  | 1262 | if (CI == CSEMap.end() || MI->isImplicitDef()) | 
| Evan Cheng | 7ff8319 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1263 | return false; | 
|  | 1264 |  | 
|  | 1265 | if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) { | 
| David Greene | 55cf95c | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1266 | DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup); | 
| Dan Gohman | 34021b7 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 1267 |  | 
|  | 1268 | // Replace virtual registers defined by MI by their counterparts defined | 
|  | 1269 | // by Dup. | 
| Evan Cheng | aa563df | 2011-10-17 19:50:12 +0000 | [diff] [blame] | 1270 | SmallVector<unsigned, 2> Defs; | 
| Evan Cheng | 7ff8319 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1271 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { | 
|  | 1272 | const MachineOperand &MO = MI->getOperand(i); | 
| Dan Gohman | 34021b7 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 1273 |  | 
|  | 1274 | // Physical registers may not differ here. | 
|  | 1275 | assert((!MO.isReg() || MO.getReg() == 0 || | 
|  | 1276 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || | 
|  | 1277 | MO.getReg() == Dup->getOperand(i).getReg()) && | 
|  | 1278 | "Instructions with different phys regs are not identical!"); | 
|  | 1279 |  | 
|  | 1280 | if (MO.isReg() && MO.isDef() && | 
| Evan Cheng | aa563df | 2011-10-17 19:50:12 +0000 | [diff] [blame] | 1281 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg())) | 
|  | 1282 | Defs.push_back(i); | 
|  | 1283 | } | 
|  | 1284 |  | 
|  | 1285 | SmallVector<const TargetRegisterClass*, 2> OrigRCs; | 
|  | 1286 | for (unsigned i = 0, e = Defs.size(); i != e; ++i) { | 
|  | 1287 | unsigned Idx = Defs[i]; | 
|  | 1288 | unsigned Reg = MI->getOperand(Idx).getReg(); | 
|  | 1289 | unsigned DupReg = Dup->getOperand(Idx).getReg(); | 
|  | 1290 | OrigRCs.push_back(MRI->getRegClass(DupReg)); | 
|  | 1291 |  | 
|  | 1292 | if (!MRI->constrainRegClass(DupReg, MRI->getRegClass(Reg))) { | 
|  | 1293 | // Restore old RCs if more than one defs. | 
|  | 1294 | for (unsigned j = 0; j != i; ++j) | 
|  | 1295 | MRI->setRegClass(Dup->getOperand(Defs[j]).getReg(), OrigRCs[j]); | 
|  | 1296 | return false; | 
| Dan Gohman | c90f51c | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1297 | } | 
| Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1298 | } | 
| Evan Cheng | aa563df | 2011-10-17 19:50:12 +0000 | [diff] [blame] | 1299 |  | 
|  | 1300 | for (unsigned i = 0, e = Defs.size(); i != e; ++i) { | 
|  | 1301 | unsigned Idx = Defs[i]; | 
|  | 1302 | unsigned Reg = MI->getOperand(Idx).getReg(); | 
|  | 1303 | unsigned DupReg = Dup->getOperand(Idx).getReg(); | 
|  | 1304 | MRI->replaceRegWith(Reg, DupReg); | 
|  | 1305 | MRI->clearKillFlags(DupReg); | 
|  | 1306 | } | 
|  | 1307 |  | 
| Evan Cheng | 7ff8319 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1308 | MI->eraseFromParent(); | 
|  | 1309 | ++NumCSEed; | 
|  | 1310 | return true; | 
| Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1311 | } | 
|  | 1312 | return false; | 
|  | 1313 | } | 
|  | 1314 |  | 
| Evan Cheng | af13895 | 2011-10-12 00:09:14 +0000 | [diff] [blame] | 1315 | /// MayCSE - Return true if the given instruction will be CSE'd if it's | 
|  | 1316 | /// hoisted out of the loop. | 
|  | 1317 | bool MachineLICM::MayCSE(MachineInstr *MI) { | 
|  | 1318 | unsigned Opcode = MI->getOpcode(); | 
|  | 1319 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator | 
|  | 1320 | CI = CSEMap.find(Opcode); | 
|  | 1321 | // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate | 
|  | 1322 | // the undef property onto uses. | 
|  | 1323 | if (CI == CSEMap.end() || MI->isImplicitDef()) | 
|  | 1324 | return false; | 
|  | 1325 |  | 
|  | 1326 | return LookForDuplicate(MI, CI->second) != 0; | 
|  | 1327 | } | 
|  | 1328 |  | 
| Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 1329 | /// Hoist - When an instruction is found to use only loop invariant operands | 
|  | 1330 | /// that are safe to hoist, this instruction is called to do the dirty work. | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1331 | /// | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1332 | bool MachineLICM::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) { | 
| Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1333 | // First check whether we should hoist this instruction. | 
| Evan Cheng | 73f9a9e | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 1334 | if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) { | 
| Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1335 | // If not, try unfolding a hoistable load. | 
|  | 1336 | MI = ExtractHoistableLoad(MI); | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1337 | if (!MI) return false; | 
| Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1338 | } | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1339 |  | 
| Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1340 | // Now move the instructions to the predecessor, inserting it before any | 
|  | 1341 | // terminator instructions. | 
|  | 1342 | DEBUG({ | 
| David Greene | 55cf95c | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1343 | dbgs() << "Hoisting " << *MI; | 
| Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1344 | if (Preheader->getBasicBlock()) | 
| David Greene | 55cf95c | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1345 | dbgs() << " to MachineBasicBlock " | 
| Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1346 | << Preheader->getName(); | 
| Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1347 | if (MI->getParent()->getBasicBlock()) | 
| David Greene | 55cf95c | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1348 | dbgs() << " from MachineBasicBlock " | 
| Jakob Stoklund Olesen | 2bbeaa8 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 1349 | << MI->getParent()->getName(); | 
| David Greene | 55cf95c | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1350 | dbgs() << "\n"; | 
| Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1351 | }); | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1352 |  | 
| Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1353 | // If this is the first instruction being hoisted to the preheader, | 
|  | 1354 | // initialize the CSE map with potential common expressions. | 
| Evan Cheng | 032f326 | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 1355 | if (FirstInLoop) { | 
| Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1356 | InitCSEMap(Preheader); | 
| Evan Cheng | 032f326 | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 1357 | FirstInLoop = false; | 
|  | 1358 | } | 
| Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1359 |  | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1360 | // Look for opportunity to CSE the hoisted instruction. | 
| Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1361 | unsigned Opcode = MI->getOpcode(); | 
|  | 1362 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator | 
|  | 1363 | CI = CSEMap.find(Opcode); | 
| Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1364 | if (!EliminateCSE(MI, CI)) { | 
|  | 1365 | // Otherwise, splice the instruction to the preheader. | 
| Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1366 | Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI); | 
| Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1367 |  | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1368 | // Update register pressure for BBs from header to this block. | 
|  | 1369 | UpdateBackTraceRegPressure(MI); | 
|  | 1370 |  | 
| Dan Gohman | c90f51c | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1371 | // Clear the kill flags of any register this instruction defines, | 
|  | 1372 | // since they may need to be live throughout the entire loop | 
|  | 1373 | // rather than just live for part of it. | 
|  | 1374 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { | 
|  | 1375 | MachineOperand &MO = MI->getOperand(i); | 
|  | 1376 | if (MO.isReg() && MO.isDef() && !MO.isDead()) | 
| Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1377 | MRI->clearKillFlags(MO.getReg()); | 
| Dan Gohman | c90f51c | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1378 | } | 
|  | 1379 |  | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1380 | // Add to the CSE map. | 
|  | 1381 | if (CI != CSEMap.end()) | 
| Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1382 | CI->second.push_back(MI); | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1383 | else { | 
|  | 1384 | std::vector<const MachineInstr*> CSEMIs; | 
| Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1385 | CSEMIs.push_back(MI); | 
| Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1386 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); | 
| Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1387 | } | 
|  | 1388 | } | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1389 |  | 
| Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1390 | ++NumHoisted; | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1391 | Changed = true; | 
| Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1392 |  | 
|  | 1393 | return true; | 
| Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1394 | } | 
| Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1395 |  | 
|  | 1396 | MachineBasicBlock *MachineLICM::getCurPreheader() { | 
|  | 1397 | // Determine the block to which to hoist instructions. If we can't find a | 
|  | 1398 | // suitable loop predecessor, we can't do any hoisting. | 
|  | 1399 |  | 
|  | 1400 | // If we've tried to get a preheader and failed, don't try again. | 
|  | 1401 | if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1)) | 
|  | 1402 | return 0; | 
|  | 1403 |  | 
|  | 1404 | if (!CurPreheader) { | 
|  | 1405 | CurPreheader = CurLoop->getLoopPreheader(); | 
|  | 1406 | if (!CurPreheader) { | 
|  | 1407 | MachineBasicBlock *Pred = CurLoop->getLoopPredecessor(); | 
|  | 1408 | if (!Pred) { | 
|  | 1409 | CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1); | 
|  | 1410 | return 0; | 
|  | 1411 | } | 
|  | 1412 |  | 
|  | 1413 | CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), this); | 
|  | 1414 | if (!CurPreheader) { | 
|  | 1415 | CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1); | 
|  | 1416 | return 0; | 
|  | 1417 | } | 
|  | 1418 | } | 
|  | 1419 | } | 
|  | 1420 | return CurPreheader; | 
|  | 1421 | } |