Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 1 | //===- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ----------===// |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass performs loop invariant code motion on machine instructions. We |
| 10 | // attempt to remove as much code from the body of a loop as possible. |
| 11 | // |
Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 12 | // This pass is not intended to be a replacement or a complete alternative |
| 13 | // for the LLVM-IR-level LICM pass. It is only designed to hoist simple |
| 14 | // constructs that are not exposed before lowering and instruction selection. |
| 15 | // |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/BitVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallSet.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/Analysis/AliasAnalysis.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineDominators.h" |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunction.h" |
| 29 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 30 | #include "llvm/CodeGen/MachineInstr.h" |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineMemOperand.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineOperand.h" |
Bill Wendling | 5da1945 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/PseudoSourceValue.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/TargetLowering.h" |
| 38 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Matthias Braun | 88e2131 | 2015-06-13 03:42:11 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/TargetSchedule.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 41 | #include "llvm/IR/DebugLoc.h" |
| 42 | #include "llvm/MC/MCInstrDesc.h" |
| 43 | #include "llvm/MC/MCRegisterInfo.h" |
| 44 | #include "llvm/Pass.h" |
| 45 | #include "llvm/Support/Casting.h" |
Evan Cheng | b35afca | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | b5c1d9b | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 48 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 49 | #include <algorithm> |
| 50 | #include <cassert> |
| 51 | #include <limits> |
| 52 | #include <vector> |
| 53 | |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 54 | using namespace llvm; |
| 55 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 56 | #define DEBUG_TYPE "machinelicm" |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 57 | |
Evan Cheng | b35afca | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 58 | static cl::opt<bool> |
| 59 | AvoidSpeculation("avoid-speculation", |
| 60 | cl::desc("MachineLICM should avoid speculation"), |
Evan Cheng | 7313337 | 2011-10-26 01:26:57 +0000 | [diff] [blame] | 61 | cl::init(true), cl::Hidden); |
Evan Cheng | b35afca | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 62 | |
Hal Finkel | 0709f51 | 2015-01-08 22:10:48 +0000 | [diff] [blame] | 63 | static cl::opt<bool> |
| 64 | HoistCheapInsts("hoist-cheap-insts", |
| 65 | cl::desc("MachineLICM should hoist even cheap instructions"), |
| 66 | cl::init(false), cl::Hidden); |
| 67 | |
Daniel Jasper | 15e6954 | 2015-03-14 10:58:38 +0000 | [diff] [blame] | 68 | static cl::opt<bool> |
| 69 | SinkInstsToAvoidSpills("sink-insts-to-avoid-spills", |
| 70 | cl::desc("MachineLICM should sink instructions into " |
| 71 | "loops to avoid register spills"), |
| 72 | cl::init(false), cl::Hidden); |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 73 | static cl::opt<bool> |
| 74 | HoistConstStores("hoist-const-stores", |
| 75 | cl::desc("Hoist invariant stores"), |
Zaara Syeda | 935474fe | 2018-04-09 14:50:02 +0000 | [diff] [blame] | 76 | cl::init(true), cl::Hidden); |
Daniel Jasper | 15e6954 | 2015-03-14 10:58:38 +0000 | [diff] [blame] | 77 | |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 78 | STATISTIC(NumHoisted, |
| 79 | "Number of machine instructions hoisted out of loops"); |
| 80 | STATISTIC(NumLowRP, |
| 81 | "Number of instructions hoisted in low reg pressure situation"); |
| 82 | STATISTIC(NumHighLatency, |
| 83 | "Number of high latency instructions hoisted"); |
| 84 | STATISTIC(NumCSEed, |
| 85 | "Number of hoisted machine instructions CSEed"); |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 86 | STATISTIC(NumPostRAHoisted, |
| 87 | "Number of machine instructions hoisted out of loops post regalloc"); |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 88 | STATISTIC(NumStoreConst, |
| 89 | "Number of stores of const phys reg hoisted out of loops"); |
Bill Wendling | 4375173 | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 90 | |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 91 | namespace { |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 92 | |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 93 | class MachineLICMBase : public MachineFunctionPass { |
Bill Wendling | 38236ef | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 94 | const TargetInstrInfo *TII; |
Benjamin Kramer | 56b31bd | 2013-01-11 20:05:37 +0000 | [diff] [blame] | 95 | const TargetLoweringBase *TLI; |
Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 96 | const TargetRegisterInfo *TRI; |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 97 | const MachineFrameInfo *MFI; |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 98 | MachineRegisterInfo *MRI; |
Matthias Braun | 88e2131 | 2015-06-13 03:42:11 +0000 | [diff] [blame] | 99 | TargetSchedModel SchedModel; |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 100 | bool PreRegAlloc; |
Bill Wendling | b678ae7 | 2007-12-11 19:40:06 +0000 | [diff] [blame] | 101 | |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 102 | // Various analyses that we use... |
Dan Gohman | be8137b | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 103 | AliasAnalysis *AA; // Alias analysis info. |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 104 | MachineLoopInfo *MLI; // Current MachineLoopInfo |
Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 105 | MachineDominatorTree *DT; // Machine dominator tree for the cur loop |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 106 | |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 107 | // State that is updated as we process loops |
Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 108 | bool Changed; // True if a loop is changed. |
Evan Cheng | 032f326 | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 109 | bool FirstInLoop; // True if it's the first LICM in the loop. |
Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 110 | MachineLoop *CurLoop; // The current loop we are working on. |
Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 111 | MachineBasicBlock *CurPreheader; // The preheader for CurLoop. |
Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 112 | |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 113 | // Exit blocks for CurLoop. |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 114 | SmallVector<MachineBasicBlock *, 8> ExitBlocks; |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 115 | |
| 116 | bool isExitBlock(const MachineBasicBlock *MBB) const { |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 117 | return is_contained(ExitBlocks, MBB); |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 120 | // Track 'estimated' register pressure. |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 121 | SmallSet<unsigned, 32> RegSeen; |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 122 | SmallVector<unsigned, 8> RegPressure; |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 123 | |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 124 | // Register pressure "limit" per register pressure set. If the pressure |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 125 | // is higher than the limit, then it's considered high. |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 126 | SmallVector<unsigned, 8> RegLimit; |
| 127 | |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 128 | // Register pressure on path leading from loop preheader to current BB. |
| 129 | SmallVector<SmallVector<unsigned, 8>, 16> BackTrace; |
| 130 | |
Dale Johannesen | 329d474 | 2010-07-29 17:45:24 +0000 | [diff] [blame] | 131 | // For each opcode, keep a list of potential CSE instructions. |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 132 | DenseMap<unsigned, std::vector<const MachineInstr *>> CSEMap; |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 133 | |
Evan Cheng | f192ca0 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 134 | enum { |
| 135 | SpeculateFalse = 0, |
| 136 | SpeculateTrue = 1, |
| 137 | SpeculateUnknown = 2 |
| 138 | }; |
| 139 | |
Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 140 | // If a MBB does not dominate loop exiting blocks then it may not safe |
| 141 | // to hoist loads from this block. |
Evan Cheng | f192ca0 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 142 | // Tri-state: 0 - false, 1 - true, 2 - unknown |
| 143 | unsigned SpeculationState; |
Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 144 | |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 145 | public: |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 146 | MachineLICMBase(char &PassID, bool PreRegAlloc) |
| 147 | : MachineFunctionPass(PassID), PreRegAlloc(PreRegAlloc) {} |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 148 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 149 | bool runOnMachineFunction(MachineFunction &MF) override; |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 150 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 151 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 152 | AU.addRequired<MachineLoopInfo>(); |
| 153 | AU.addRequired<MachineDominatorTree>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 154 | AU.addRequired<AAResultsWrapperPass>(); |
Bill Wendling | 3bf5603 | 2008-01-04 08:48:49 +0000 | [diff] [blame] | 155 | AU.addPreserved<MachineLoopInfo>(); |
| 156 | AU.addPreserved<MachineDominatorTree>(); |
| 157 | MachineFunctionPass::getAnalysisUsage(AU); |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 158 | } |
Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 159 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 160 | void releaseMemory() override { |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 161 | RegSeen.clear(); |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 162 | RegPressure.clear(); |
| 163 | RegLimit.clear(); |
Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 164 | BackTrace.clear(); |
Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 165 | CSEMap.clear(); |
| 166 | } |
| 167 | |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 168 | private: |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 169 | /// Keep track of information about hoisting candidates. |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 170 | struct CandidateInfo { |
| 171 | MachineInstr *MI; |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 172 | unsigned Def; |
Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 173 | int FI; |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 174 | |
Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 175 | CandidateInfo(MachineInstr *mi, unsigned def, int fi) |
| 176 | : MI(mi), Def(def), FI(fi) {} |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 179 | void HoistRegionPostRA(); |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 180 | |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 181 | void HoistPostRA(MachineInstr *MI, unsigned Def); |
| 182 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 183 | void ProcessMI(MachineInstr *MI, BitVector &PhysRegDefs, |
| 184 | BitVector &PhysRegClobbers, SmallSet<int, 32> &StoredFIs, |
Craig Topper | 2cd5ff8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 185 | SmallVectorImpl<CandidateInfo> &Candidates); |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 186 | |
Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 187 | void AddToLiveIns(unsigned Reg); |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 188 | |
Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 189 | bool IsLICMCandidate(MachineInstr &I); |
| 190 | |
Bill Wendling | 3f19dfe7 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 191 | bool IsLoopInvariantInst(MachineInstr &I); |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 192 | |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 193 | bool HasLoopPHIUse(const MachineInstr *MI) const; |
Evan Cheng | ef42bea | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 194 | |
Evan Cheng | e96b8d7 | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 195 | bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx, |
| 196 | unsigned Reg) const; |
| 197 | |
| 198 | bool IsCheapInstruction(MachineInstr &MI) const; |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 199 | |
Daniel Jasper | efece52 | 2015-04-03 16:19:48 +0000 | [diff] [blame] | 200 | bool CanCauseHighRegPressure(const DenseMap<unsigned, int> &Cost, |
| 201 | bool Cheap); |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 202 | |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 203 | void UpdateBackTraceRegPressure(const MachineInstr *MI); |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 204 | |
Evan Cheng | 73f9a9e | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 205 | bool IsProfitableToHoist(MachineInstr &MI); |
Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 206 | |
Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 207 | bool IsGuaranteedToExecute(MachineBasicBlock *BB); |
| 208 | |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 209 | void EnterScope(MachineBasicBlock *MBB); |
| 210 | |
| 211 | void ExitScope(MachineBasicBlock *MBB); |
| 212 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 213 | void ExitScopeIfDone( |
| 214 | MachineDomTreeNode *Node, |
| 215 | DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren, |
| 216 | DenseMap<MachineDomTreeNode *, MachineDomTreeNode *> &ParentMap); |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 217 | |
Fangrui Song | cb0bab8 | 2018-07-16 18:51:40 +0000 | [diff] [blame] | 218 | void HoistOutOfLoop(MachineDomTreeNode *HeaderN); |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 219 | |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 220 | void HoistRegion(MachineDomTreeNode *N, bool IsHeader); |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 221 | |
Daniel Jasper | 15e6954 | 2015-03-14 10:58:38 +0000 | [diff] [blame] | 222 | void SinkIntoLoop(); |
| 223 | |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 224 | void InitRegPressure(MachineBasicBlock *BB); |
| 225 | |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 226 | DenseMap<unsigned, int> calcRegisterCost(const MachineInstr *MI, |
| 227 | bool ConsiderSeen, |
| 228 | bool ConsiderUnseenAsDef); |
| 229 | |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 230 | void UpdateRegPressure(const MachineInstr *MI, |
| 231 | bool ConsiderUnseenAsDef = false); |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 232 | |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 233 | MachineInstr *ExtractHoistableLoad(MachineInstr *MI); |
| 234 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 235 | const MachineInstr * |
| 236 | LookForDuplicate(const MachineInstr *MI, |
| 237 | std::vector<const MachineInstr *> &PrevMIs); |
Evan Cheng | 7ff8319 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 238 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 239 | bool EliminateCSE( |
| 240 | MachineInstr *MI, |
| 241 | DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator &CI); |
Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 242 | |
Evan Cheng | af13895 | 2011-10-12 00:09:14 +0000 | [diff] [blame] | 243 | bool MayCSE(MachineInstr *MI); |
| 244 | |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 245 | bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader); |
Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 246 | |
Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 247 | void InitCSEMap(MachineBasicBlock *BB); |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 248 | |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 249 | MachineBasicBlock *getCurPreheader(); |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 250 | }; |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 251 | |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 252 | class MachineLICM : public MachineLICMBase { |
| 253 | public: |
| 254 | static char ID; |
| 255 | MachineLICM() : MachineLICMBase(ID, false) { |
| 256 | initializeMachineLICMPass(*PassRegistry::getPassRegistry()); |
| 257 | } |
| 258 | }; |
| 259 | |
| 260 | class EarlyMachineLICM : public MachineLICMBase { |
| 261 | public: |
| 262 | static char ID; |
| 263 | EarlyMachineLICM() : MachineLICMBase(ID, true) { |
| 264 | initializeEarlyMachineLICMPass(*PassRegistry::getPassRegistry()); |
| 265 | } |
| 266 | }; |
| 267 | |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 268 | } // end anonymous namespace |
| 269 | |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 270 | char MachineLICM::ID; |
| 271 | char EarlyMachineLICM::ID; |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 272 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 273 | char &llvm::MachineLICMID = MachineLICM::ID; |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 274 | char &llvm::EarlyMachineLICMID = EarlyMachineLICM::ID; |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 275 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 276 | INITIALIZE_PASS_BEGIN(MachineLICM, DEBUG_TYPE, |
| 277 | "Machine Loop Invariant Code Motion", false, false) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 278 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 279 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 280 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 281 | INITIALIZE_PASS_END(MachineLICM, DEBUG_TYPE, |
| 282 | "Machine Loop Invariant Code Motion", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 283 | |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 284 | INITIALIZE_PASS_BEGIN(EarlyMachineLICM, "early-machinelicm", |
| 285 | "Early Machine Loop Invariant Code Motion", false, false) |
| 286 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 287 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 288 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| 289 | INITIALIZE_PASS_END(EarlyMachineLICM, "early-machinelicm", |
| 290 | "Early Machine Loop Invariant Code Motion", false, false) |
| 291 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 292 | /// Test if the given loop is the outer-most loop that has a unique predecessor. |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 293 | static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) { |
Dan Gohman | 7929c44 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 294 | // Check whether this loop even has a unique predecessor. |
| 295 | if (!CurLoop->getLoopPredecessor()) |
| 296 | return false; |
| 297 | // Ok, now check to see if any of its outer loops do. |
Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 298 | for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop()) |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 299 | if (L->getLoopPredecessor()) |
Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 300 | return false; |
Dan Gohman | 7929c44 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 301 | // 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] | 302 | return true; |
| 303 | } |
| 304 | |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 305 | bool MachineLICMBase::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 306 | if (skipFunction(MF.getFunction())) |
Paul Robinson | 7c99ec5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 307 | return false; |
| 308 | |
Evan Cheng | 032f326 | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 309 | Changed = FirstInLoop = false; |
Matthias Braun | 88e2131 | 2015-06-13 03:42:11 +0000 | [diff] [blame] | 310 | const TargetSubtargetInfo &ST = MF.getSubtarget(); |
| 311 | TII = ST.getInstrInfo(); |
| 312 | TLI = ST.getTargetLowering(); |
| 313 | TRI = ST.getRegisterInfo(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 314 | MFI = &MF.getFrameInfo(); |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 315 | MRI = &MF.getRegInfo(); |
Sanjay Patel | 0d7df36 | 2018-04-08 19:56:04 +0000 | [diff] [blame] | 316 | SchedModel.init(&ST); |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 317 | |
Andrew Trick | c40815d | 2012-02-08 21:23:03 +0000 | [diff] [blame] | 318 | PreRegAlloc = MRI->isSSA(); |
| 319 | |
Jakob Stoklund Olesen | c8046c0 | 2012-02-11 00:40:36 +0000 | [diff] [blame] | 320 | if (PreRegAlloc) |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 321 | LLVM_DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: "); |
Jakob Stoklund Olesen | c8046c0 | 2012-02-11 00:40:36 +0000 | [diff] [blame] | 322 | else |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 323 | LLVM_DEBUG(dbgs() << "******** Post-regalloc Machine LICM: "); |
| 324 | LLVM_DEBUG(dbgs() << MF.getName() << " ********\n"); |
Jakob Stoklund Olesen | c8046c0 | 2012-02-11 00:40:36 +0000 | [diff] [blame] | 325 | |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 326 | if (PreRegAlloc) { |
| 327 | // Estimate register pressure during pre-regalloc pass. |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 328 | unsigned NumRPS = TRI->getNumRegPressureSets(); |
| 329 | RegPressure.resize(NumRPS); |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 330 | std::fill(RegPressure.begin(), RegPressure.end(), 0); |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 331 | RegLimit.resize(NumRPS); |
| 332 | for (unsigned i = 0, e = NumRPS; i != e; ++i) |
| 333 | RegLimit[i] = TRI->getRegPressureSetLimit(MF, i); |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 336 | // Get our Loop information... |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 337 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 338 | DT = &getAnalysis<MachineDominatorTree>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 339 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 340 | |
Dan Gohman | 7929c44 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 341 | SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end()); |
| 342 | while (!Worklist.empty()) { |
| 343 | CurLoop = Worklist.pop_back_val(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 344 | CurPreheader = nullptr; |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 345 | ExitBlocks.clear(); |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 346 | |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 347 | // If this is done before regalloc, only visit outer-most preheader-sporting |
| 348 | // loops. |
Dan Gohman | 7929c44 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 349 | if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) { |
| 350 | Worklist.append(CurLoop->begin(), CurLoop->end()); |
Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 351 | continue; |
Dan Gohman | 7929c44 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 352 | } |
Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 353 | |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 354 | CurLoop->getExitBlocks(ExitBlocks); |
| 355 | |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 356 | if (!PreRegAlloc) |
Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 357 | HoistRegionPostRA(); |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 358 | else { |
Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 359 | // CSEMap is initialized for loop header when the first instruction is |
| 360 | // being hoisted. |
| 361 | MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader()); |
Evan Cheng | 032f326 | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 362 | FirstInLoop = true; |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 363 | HoistOutOfLoop(N); |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 364 | CSEMap.clear(); |
Daniel Jasper | 15e6954 | 2015-03-14 10:58:38 +0000 | [diff] [blame] | 365 | |
| 366 | if (SinkInstsToAvoidSpills) |
| 367 | SinkIntoLoop(); |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 368 | } |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | return Changed; |
| 372 | } |
| 373 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 374 | /// Return true if instruction stores to the specified frame. |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 375 | static bool InstructionStoresToFI(const MachineInstr *MI, int FI) { |
Geoff Berry | 8e4958e | 2018-05-04 19:25:09 +0000 | [diff] [blame] | 376 | // Check mayStore before memory operands so that e.g. DBG_VALUEs will return |
| 377 | // true since they have no memory operands. |
| 378 | if (!MI->mayStore()) |
| 379 | return false; |
Philip Reames | 42bd26f | 2015-12-23 17:05:57 +0000 | [diff] [blame] | 380 | // If we lost memory operands, conservatively assume that the instruction |
Michael Liao | a5d4537 | 2017-04-26 05:27:20 +0000 | [diff] [blame] | 381 | // writes to all slots. |
Philip Reames | 42bd26f | 2015-12-23 17:05:57 +0000 | [diff] [blame] | 382 | if (MI->memoperands_empty()) |
| 383 | return true; |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 384 | for (const MachineMemOperand *MemOp : MI->memoperands()) { |
| 385 | if (!MemOp->isStore() || !MemOp->getPseudoValue()) |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 386 | continue; |
| 387 | if (const FixedStackPseudoSourceValue *Value = |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 388 | dyn_cast<FixedStackPseudoSourceValue>(MemOp->getPseudoValue())) { |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 389 | if (Value->getFrameIndex() == FI) |
| 390 | return true; |
| 391 | } |
| 392 | } |
| 393 | return false; |
| 394 | } |
| 395 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 396 | /// Examine the instruction for potentai LICM candidate. Also |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 397 | /// gather register def and frame object update information. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 398 | void MachineLICMBase::ProcessMI(MachineInstr *MI, |
| 399 | BitVector &PhysRegDefs, |
| 400 | BitVector &PhysRegClobbers, |
| 401 | SmallSet<int, 32> &StoredFIs, |
| 402 | SmallVectorImpl<CandidateInfo> &Candidates) { |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 403 | bool RuledOut = false; |
Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 404 | bool HasNonInvariantUse = false; |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 405 | unsigned Def = 0; |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 406 | for (const MachineOperand &MO : MI->operands()) { |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 407 | if (MO.isFI()) { |
| 408 | // Remember if the instruction stores to the frame index. |
| 409 | int FI = MO.getIndex(); |
| 410 | if (!StoredFIs.count(FI) && |
| 411 | MFI->isSpillSlotObjectIndex(FI) && |
| 412 | InstructionStoresToFI(MI, FI)) |
| 413 | StoredFIs.insert(FI); |
Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 414 | HasNonInvariantUse = true; |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 415 | continue; |
| 416 | } |
| 417 | |
Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 418 | // We can't hoist an instruction defining a physreg that is clobbered in |
| 419 | // the loop. |
| 420 | if (MO.isRegMask()) { |
Jakob Stoklund Olesen | 5e1ac45 | 2012-02-02 23:52:57 +0000 | [diff] [blame] | 421 | PhysRegClobbers.setBitsNotInMask(MO.getRegMask()); |
Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 422 | continue; |
| 423 | } |
| 424 | |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 425 | if (!MO.isReg()) |
| 426 | continue; |
| 427 | unsigned Reg = MO.getReg(); |
| 428 | if (!Reg) |
| 429 | continue; |
| 430 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 431 | "Not expecting virtual register!"); |
| 432 | |
Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 433 | if (!MO.isDef()) { |
Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 434 | if (Reg && (PhysRegDefs.test(Reg) || PhysRegClobbers.test(Reg))) |
Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 435 | // If it's using a non-loop-invariant register, then it's obviously not |
| 436 | // safe to hoist. |
| 437 | HasNonInvariantUse = true; |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 438 | continue; |
Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 439 | } |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 440 | |
| 441 | if (MO.isImplicit()) { |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 442 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) |
| 443 | PhysRegClobbers.set(*AI); |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 444 | if (!MO.isDead()) |
| 445 | // Non-dead implicit def? This cannot be hoisted. |
| 446 | RuledOut = true; |
| 447 | // No need to check if a dead implicit def is also defined by |
| 448 | // another instruction. |
| 449 | continue; |
| 450 | } |
| 451 | |
| 452 | // FIXME: For now, avoid instructions with multiple defs, unless |
| 453 | // it's a dead implicit def. |
| 454 | if (Def) |
| 455 | RuledOut = true; |
| 456 | else |
| 457 | Def = Reg; |
| 458 | |
| 459 | // If we have already seen another instruction that defines the same |
Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 460 | // register, then this is not safe. Two defs is indicated by setting a |
| 461 | // PhysRegClobbers bit. |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 462 | for (MCRegAliasIterator AS(Reg, TRI, true); AS.isValid(); ++AS) { |
Jakob Stoklund Olesen | 20948fa | 2012-01-23 21:01:15 +0000 | [diff] [blame] | 463 | if (PhysRegDefs.test(*AS)) |
| 464 | PhysRegClobbers.set(*AS); |
Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 465 | } |
Craig Topper | 6934202 | 2018-12-05 03:41:26 +0000 | [diff] [blame] | 466 | // Need a second loop because MCRegAliasIterator can visit the same |
| 467 | // register twice. |
| 468 | for (MCRegAliasIterator AS(Reg, TRI, true); AS.isValid(); ++AS) |
| 469 | PhysRegDefs.set(*AS); |
| 470 | |
Richard Sandiford | 96aa93d | 2013-08-20 09:11:13 +0000 | [diff] [blame] | 471 | if (PhysRegClobbers.test(Reg)) |
| 472 | // MI defined register is seen defined by another instruction in |
| 473 | // the loop, it cannot be a LICM candidate. |
| 474 | RuledOut = true; |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 477 | // Only consider reloads for now and remats which do not have register |
| 478 | // operands. FIXME: Consider unfold load folding instructions. |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 479 | if (Def && !RuledOut) { |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 480 | int FI = std::numeric_limits<int>::min(); |
Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 481 | if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) || |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 482 | (TII->isLoadFromStackSlot(*MI, FI) && MFI->isSpillSlotObjectIndex(FI))) |
Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 483 | Candidates.push_back(CandidateInfo(MI, Def, FI)); |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 487 | /// Walk the specified region of the CFG and hoist loop invariants out to the |
| 488 | /// preheader. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 489 | void MachineLICMBase::HoistRegionPostRA() { |
Evan Cheng | 7fede87 | 2012-03-27 01:50:58 +0000 | [diff] [blame] | 490 | MachineBasicBlock *Preheader = getCurPreheader(); |
| 491 | if (!Preheader) |
| 492 | return; |
| 493 | |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 494 | unsigned NumRegs = TRI->getNumRegs(); |
Jakob Stoklund Olesen | 6b17ef5 | 2012-01-20 22:27:12 +0000 | [diff] [blame] | 495 | BitVector PhysRegDefs(NumRegs); // Regs defined once in the loop. |
| 496 | BitVector PhysRegClobbers(NumRegs); // Regs defined more than once. |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 497 | |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 498 | SmallVector<CandidateInfo, 32> Candidates; |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 499 | SmallSet<int, 32> StoredFIs; |
| 500 | |
| 501 | // Walk the entire region, count number of defs for each register, and |
Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 502 | // collect potential LICM candidates. |
Benjamin Kramer | 28559a2 | 2018-09-10 12:32:06 +0000 | [diff] [blame] | 503 | for (MachineBasicBlock *BB : CurLoop->getBlocks()) { |
Bill Wendling | 918cea2 | 2011-10-12 02:58:01 +0000 | [diff] [blame] | 504 | // If the header of the loop containing this basic block is a landing pad, |
| 505 | // then don't try to hoist instructions out of this loop. |
| 506 | const MachineLoop *ML = MLI->getLoopFor(BB); |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 507 | if (ML && ML->getHeader()->isEHPad()) continue; |
Bill Wendling | 918cea2 | 2011-10-12 02:58:01 +0000 | [diff] [blame] | 508 | |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 509 | // Conservatively treat live-in's as an external def. |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 510 | // FIXME: That means a reload that're reused in successor block(s) will not |
| 511 | // be LICM'ed. |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 512 | for (const auto &LI : BB->liveins()) { |
| 513 | for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 514 | PhysRegDefs.set(*AI); |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Evan Cheng | f192ca0 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 517 | SpeculationState = SpeculateUnknown; |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 518 | for (MachineInstr &MI : *BB) |
| 519 | ProcessMI(&MI, PhysRegDefs, PhysRegClobbers, StoredFIs, Candidates); |
Evan Cheng | 5fdb57c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 520 | } |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 521 | |
Evan Cheng | 7fede87 | 2012-03-27 01:50:58 +0000 | [diff] [blame] | 522 | // Gather the registers read / clobbered by the terminator. |
| 523 | BitVector TermRegs(NumRegs); |
| 524 | MachineBasicBlock::iterator TI = Preheader->getFirstTerminator(); |
| 525 | if (TI != Preheader->end()) { |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 526 | for (const MachineOperand &MO : TI->operands()) { |
Evan Cheng | 7fede87 | 2012-03-27 01:50:58 +0000 | [diff] [blame] | 527 | if (!MO.isReg()) |
| 528 | continue; |
| 529 | unsigned Reg = MO.getReg(); |
| 530 | if (!Reg) |
| 531 | continue; |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 532 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) |
| 533 | TermRegs.set(*AI); |
Evan Cheng | 7fede87 | 2012-03-27 01:50:58 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 537 | // Now evaluate whether the potential candidates qualify. |
| 538 | // 1. Check if the candidate defined register is defined by another |
| 539 | // instruction in the loop. |
| 540 | // 2. If the candidate is a load from stack slot (always true for now), |
| 541 | // check if the slot is stored anywhere in the loop. |
Evan Cheng | 7fede87 | 2012-03-27 01:50:58 +0000 | [diff] [blame] | 542 | // 3. Make sure candidate def should not clobber |
| 543 | // registers read by the terminator. Similarly its def should not be |
| 544 | // clobbered by the terminator. |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 545 | for (CandidateInfo &Candidate : Candidates) { |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 546 | if (Candidate.FI != std::numeric_limits<int>::min() && |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 547 | StoredFIs.count(Candidate.FI)) |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 548 | continue; |
| 549 | |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 550 | unsigned Def = Candidate.Def; |
Evan Cheng | 7fede87 | 2012-03-27 01:50:58 +0000 | [diff] [blame] | 551 | if (!PhysRegClobbers.test(Def) && !TermRegs.test(Def)) { |
Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 552 | bool Safe = true; |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 553 | MachineInstr *MI = Candidate.MI; |
| 554 | for (const MachineOperand &MO : MI->operands()) { |
Evan Cheng | 87585d7 | 2010-04-13 22:13:34 +0000 | [diff] [blame] | 555 | if (!MO.isReg() || MO.isDef() || !MO.getReg()) |
Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 556 | continue; |
Evan Cheng | 7fede87 | 2012-03-27 01:50:58 +0000 | [diff] [blame] | 557 | unsigned Reg = MO.getReg(); |
| 558 | if (PhysRegDefs.test(Reg) || |
| 559 | PhysRegClobbers.test(Reg)) { |
Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 560 | // If it's using a non-loop-invariant register, then it's obviously |
| 561 | // not safe to hoist. |
| 562 | Safe = false; |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | if (Safe) |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 567 | HoistPostRA(MI, Candidate.Def); |
Evan Cheng | 89e7479 | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 568 | } |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 572 | /// Add register 'Reg' to the livein sets of BBs in the current loop, and make |
| 573 | /// sure it is not killed by any instructions in the loop. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 574 | void MachineLICMBase::AddToLiveIns(unsigned Reg) { |
Benjamin Kramer | 28559a2 | 2018-09-10 12:32:06 +0000 | [diff] [blame] | 575 | for (MachineBasicBlock *BB : CurLoop->getBlocks()) { |
Jakob Stoklund Olesen | 011207a | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 576 | if (!BB->isLiveIn(Reg)) |
| 577 | BB->addLiveIn(Reg); |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 578 | for (MachineInstr &MI : *BB) { |
| 579 | for (MachineOperand &MO : MI.operands()) { |
Jakob Stoklund Olesen | 011207a | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 580 | if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue; |
| 581 | if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg())) |
| 582 | MO.setIsKill(false); |
| 583 | } |
| 584 | } |
| 585 | } |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 588 | /// When an instruction is found to only use loop invariant operands that is |
| 589 | /// safe to hoist, this instruction is called to do the dirty work. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 590 | void MachineLICMBase::HoistPostRA(MachineInstr *MI, unsigned Def) { |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 591 | MachineBasicBlock *Preheader = getCurPreheader(); |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 592 | |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 593 | // Now move the instructions to the predecessor, inserting it before any |
| 594 | // terminator instructions. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 595 | LLVM_DEBUG(dbgs() << "Hoisting to " << printMBBReference(*Preheader) |
| 596 | << " from " << printMBBReference(*MI->getParent()) << ": " |
| 597 | << *MI); |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 598 | |
| 599 | // Splice the instruction to the preheader. |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 600 | MachineBasicBlock *MBB = MI->getParent(); |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 601 | Preheader->splice(Preheader->getFirstTerminator(), MBB, MI); |
Evan Cheng | 058b9f0 | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 602 | |
Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 603 | // 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] | 604 | // loop invariant must be kept live throughout the whole loop. This is |
| 605 | // important to ensure later passes do not scavenge the def register. |
| 606 | AddToLiveIns(Def); |
Evan Cheng | 6ea5949 | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 607 | |
| 608 | ++NumPostRAHoisted; |
| 609 | Changed = true; |
| 610 | } |
| 611 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 612 | /// Check if this mbb is guaranteed to execute. If not then a load from this mbb |
| 613 | /// may not be safe to hoist. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 614 | bool MachineLICMBase::IsGuaranteedToExecute(MachineBasicBlock *BB) { |
Evan Cheng | f192ca0 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 615 | if (SpeculationState != SpeculateUnknown) |
| 616 | return SpeculationState == SpeculateFalse; |
Andrew Trick | 5209c73 | 2012-02-08 21:23:00 +0000 | [diff] [blame] | 617 | |
Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 618 | if (BB != CurLoop->getHeader()) { |
| 619 | // Check loop exiting blocks. |
| 620 | SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks; |
| 621 | CurLoop->getExitingBlocks(CurrentLoopExitingBlocks); |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 622 | for (MachineBasicBlock *CurrentLoopExitingBlock : CurrentLoopExitingBlocks) |
| 623 | if (!DT->dominates(BB, CurrentLoopExitingBlock)) { |
Nick Lewycky | 404feb9 | 2011-10-13 01:09:50 +0000 | [diff] [blame] | 624 | SpeculationState = SpeculateTrue; |
| 625 | return false; |
Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
Evan Cheng | f192ca0 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 629 | SpeculationState = SpeculateFalse; |
| 630 | return true; |
Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 633 | void MachineLICMBase::EnterScope(MachineBasicBlock *MBB) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 634 | LLVM_DEBUG(dbgs() << "Entering " << printMBBReference(*MBB) << '\n'); |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 635 | |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 636 | // Remember livein register pressure. |
| 637 | BackTrace.push_back(RegPressure); |
| 638 | } |
Bill Wendling | 918cea2 | 2011-10-12 02:58:01 +0000 | [diff] [blame] | 639 | |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 640 | void MachineLICMBase::ExitScope(MachineBasicBlock *MBB) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 641 | LLVM_DEBUG(dbgs() << "Exiting " << printMBBReference(*MBB) << '\n'); |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 642 | BackTrace.pop_back(); |
| 643 | } |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 644 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 645 | /// Destroy scope for the MBB that corresponds to the given dominator tree node |
| 646 | /// if its a leaf or all of its children are done. Walk up the dominator tree to |
| 647 | /// destroy ancestors which are now done. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 648 | void MachineLICMBase::ExitScopeIfDone(MachineDomTreeNode *Node, |
| 649 | DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren, |
| 650 | DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) { |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 651 | if (OpenChildren[Node]) |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 652 | return; |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 653 | |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 654 | // Pop scope. |
| 655 | ExitScope(Node->getBlock()); |
| 656 | |
| 657 | // Now traverse upwards to pop ancestors whose offsprings are all done. |
| 658 | while (MachineDomTreeNode *Parent = ParentMap[Node]) { |
| 659 | unsigned Left = --OpenChildren[Parent]; |
| 660 | if (Left != 0) |
| 661 | break; |
| 662 | ExitScope(Parent->getBlock()); |
| 663 | Node = Parent; |
| 664 | } |
| 665 | } |
| 666 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 667 | /// Walk the specified loop in the CFG (defined by all blocks dominated by the |
| 668 | /// specified header block, and that are in the current loop) in depth first |
| 669 | /// order w.r.t the DominatorTree. This allows us to visit definitions before |
| 670 | /// uses, allowing us to hoist a loop body in one pass without iteration. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 671 | void MachineLICMBase::HoistOutOfLoop(MachineDomTreeNode *HeaderN) { |
Daniel Jasper | 4bb224d | 2015-02-05 22:39:46 +0000 | [diff] [blame] | 672 | MachineBasicBlock *Preheader = getCurPreheader(); |
| 673 | if (!Preheader) |
| 674 | return; |
| 675 | |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 676 | SmallVector<MachineDomTreeNode*, 32> Scopes; |
| 677 | SmallVector<MachineDomTreeNode*, 8> WorkList; |
| 678 | DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap; |
| 679 | DenseMap<MachineDomTreeNode*, unsigned> OpenChildren; |
| 680 | |
| 681 | // Perform a DFS walk to determine the order of visit. |
| 682 | WorkList.push_back(HeaderN); |
Daniel Jasper | 4bb224d | 2015-02-05 22:39:46 +0000 | [diff] [blame] | 683 | while (!WorkList.empty()) { |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 684 | MachineDomTreeNode *Node = WorkList.pop_back_val(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 685 | assert(Node && "Null dominator tree node?"); |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 686 | MachineBasicBlock *BB = Node->getBlock(); |
| 687 | |
| 688 | // If the header of the loop containing this basic block is a landing pad, |
| 689 | // then don't try to hoist instructions out of this loop. |
| 690 | const MachineLoop *ML = MLI->getLoopFor(BB); |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 691 | if (ML && ML->getHeader()->isEHPad()) |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 692 | continue; |
| 693 | |
| 694 | // If this subregion is not in the top level loop at all, exit. |
| 695 | if (!CurLoop->contains(BB)) |
| 696 | continue; |
| 697 | |
| 698 | Scopes.push_back(Node); |
| 699 | const std::vector<MachineDomTreeNode*> &Children = Node->getChildren(); |
| 700 | unsigned NumChildren = Children.size(); |
| 701 | |
| 702 | // Don't hoist things out of a large switch statement. This often causes |
| 703 | // code to be hoisted that wasn't going to be executed, and increases |
| 704 | // register pressure in a situation where it's likely to matter. |
| 705 | if (BB->succ_size() >= 25) |
| 706 | NumChildren = 0; |
| 707 | |
| 708 | OpenChildren[Node] = NumChildren; |
| 709 | // Add children in reverse order as then the next popped worklist node is |
| 710 | // the first child of this node. This means we ultimately traverse the |
| 711 | // DOM tree in exactly the same order as if we'd recursed. |
| 712 | for (int i = (int)NumChildren-1; i >= 0; --i) { |
| 713 | MachineDomTreeNode *Child = Children[i]; |
| 714 | ParentMap[Child] = Node; |
| 715 | WorkList.push_back(Child); |
| 716 | } |
Daniel Dunbar | 418204e | 2010-10-19 17:14:24 +0000 | [diff] [blame] | 717 | } |
Evan Cheng | 8249dfe | 2010-10-19 00:55:07 +0000 | [diff] [blame] | 718 | |
Daniel Jasper | 4bb224d | 2015-02-05 22:39:46 +0000 | [diff] [blame] | 719 | if (Scopes.size() == 0) |
| 720 | return; |
| 721 | |
| 722 | // Compute registers which are livein into the loop headers. |
| 723 | RegSeen.clear(); |
| 724 | BackTrace.clear(); |
| 725 | InitRegPressure(Preheader); |
| 726 | |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 727 | // Now perform LICM. |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 728 | for (MachineDomTreeNode *Node : Scopes) { |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 729 | MachineBasicBlock *MBB = Node->getBlock(); |
Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 730 | |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 731 | EnterScope(MBB); |
| 732 | |
| 733 | // Process the block |
| 734 | SpeculationState = SpeculateUnknown; |
| 735 | for (MachineBasicBlock::iterator |
| 736 | MII = MBB->begin(), E = MBB->end(); MII != E; ) { |
| 737 | MachineBasicBlock::iterator NextMII = MII; ++NextMII; |
| 738 | MachineInstr *MI = &*MII; |
| 739 | if (!Hoist(MI, Preheader)) |
| 740 | UpdateRegPressure(MI); |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 741 | // If we have hoisted an instruction that may store, it can only be a |
| 742 | // constant store. |
Pete Cooper | 1eed5b5 | 2011-12-22 02:05:40 +0000 | [diff] [blame] | 743 | MII = NextMII; |
| 744 | } |
| 745 | |
| 746 | // If it's a leaf node, it's done. Traverse upwards to pop ancestors. |
| 747 | ExitScopeIfDone(Node, OpenChildren, ParentMap); |
Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 748 | } |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 751 | /// Sink instructions into loops if profitable. This especially tries to prevent |
| 752 | /// register spills caused by register pressure if there is little to no |
| 753 | /// overhead moving instructions into loops. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 754 | void MachineLICMBase::SinkIntoLoop() { |
Daniel Jasper | 15e6954 | 2015-03-14 10:58:38 +0000 | [diff] [blame] | 755 | MachineBasicBlock *Preheader = getCurPreheader(); |
| 756 | if (!Preheader) |
| 757 | return; |
| 758 | |
| 759 | SmallVector<MachineInstr *, 8> Candidates; |
| 760 | for (MachineBasicBlock::instr_iterator I = Preheader->instr_begin(); |
| 761 | I != Preheader->instr_end(); ++I) { |
| 762 | // We need to ensure that we can safely move this instruction into the loop. |
Michael Liao | a5d4537 | 2017-04-26 05:27:20 +0000 | [diff] [blame] | 763 | // As such, it must not have side-effects, e.g. such as a call has. |
Duncan P. N. Exon Smith | 5ec1568 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 764 | if (IsLoopInvariantInst(*I) && !HasLoopPHIUse(&*I)) |
| 765 | Candidates.push_back(&*I); |
Daniel Jasper | 15e6954 | 2015-03-14 10:58:38 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | for (MachineInstr *I : Candidates) { |
| 769 | const MachineOperand &MO = I->getOperand(0); |
| 770 | if (!MO.isDef() || !MO.isReg() || !MO.getReg()) |
| 771 | continue; |
| 772 | if (!MRI->hasOneDef(MO.getReg())) |
| 773 | continue; |
| 774 | bool CanSink = true; |
| 775 | MachineBasicBlock *B = nullptr; |
| 776 | for (MachineInstr &MI : MRI->use_instructions(MO.getReg())) { |
| 777 | // FIXME: Come up with a proper cost model that estimates whether sinking |
| 778 | // the instruction (and thus possibly executing it on every loop |
| 779 | // iteration) is more expensive than a register. |
| 780 | // For now assumes that copies are cheap and thus almost always worth it. |
| 781 | if (!MI.isCopy()) { |
| 782 | CanSink = false; |
| 783 | break; |
| 784 | } |
| 785 | if (!B) { |
| 786 | B = MI.getParent(); |
| 787 | continue; |
| 788 | } |
| 789 | B = DT->findNearestCommonDominator(B, MI.getParent()); |
| 790 | if (!B) { |
| 791 | CanSink = false; |
| 792 | break; |
| 793 | } |
| 794 | } |
| 795 | if (!CanSink || !B || B == Preheader) |
| 796 | continue; |
| 797 | B->splice(B->getFirstNonPHI(), Preheader, I); |
| 798 | } |
| 799 | } |
| 800 | |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 801 | static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) { |
| 802 | return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg()); |
| 803 | } |
| 804 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 805 | /// Find all virtual register references that are liveout of the preheader to |
| 806 | /// initialize the starting "register pressure". Note this does not count live |
| 807 | /// through (livein but not used) registers. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 808 | void MachineLICMBase::InitRegPressure(MachineBasicBlock *BB) { |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 809 | std::fill(RegPressure.begin(), RegPressure.end(), 0); |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 810 | |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 811 | // If the preheader has only a single predecessor and it ends with a |
| 812 | // fallthrough or an unconditional branch, then scan its predecessor for live |
| 813 | // defs as well. This happens whenever the preheader is created by splitting |
| 814 | // the critical edge from the loop predecessor to the loop header. |
| 815 | if (BB->pred_size() == 1) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 816 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 817 | SmallVector<MachineOperand, 4> Cond; |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 818 | if (!TII->analyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty()) |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 819 | InitRegPressure(*BB->pred_begin()); |
| 820 | } |
| 821 | |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 822 | for (const MachineInstr &MI : *BB) |
| 823 | UpdateRegPressure(&MI, /*ConsiderUnseenAsDef=*/true); |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 826 | /// Update estimate of register pressure after the specified instruction. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 827 | void MachineLICMBase::UpdateRegPressure(const MachineInstr *MI, |
| 828 | bool ConsiderUnseenAsDef) { |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 829 | auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/true, ConsiderUnseenAsDef); |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 830 | for (const auto &RPIdAndCost : Cost) { |
| 831 | unsigned Class = RPIdAndCost.first; |
| 832 | if (static_cast<int>(RegPressure[Class]) < -RPIdAndCost.second) |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 833 | RegPressure[Class] = 0; |
| 834 | else |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 835 | RegPressure[Class] += RPIdAndCost.second; |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 838 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 839 | /// Calculate the additional register pressure that the registers used in MI |
| 840 | /// cause. |
| 841 | /// |
| 842 | /// If 'ConsiderSeen' is true, updates 'RegSeen' and uses the information to |
| 843 | /// figure out which usages are live-ins. |
| 844 | /// FIXME: Figure out a way to consider 'RegSeen' from all code paths. |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 845 | DenseMap<unsigned, int> |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 846 | MachineLICMBase::calcRegisterCost(const MachineInstr *MI, bool ConsiderSeen, |
| 847 | bool ConsiderUnseenAsDef) { |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 848 | DenseMap<unsigned, int> Cost; |
| 849 | if (MI->isImplicitDef()) |
| 850 | return Cost; |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 851 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { |
| 852 | const MachineOperand &MO = MI->getOperand(i); |
Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 853 | if (!MO.isReg() || MO.isImplicit()) |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 854 | continue; |
| 855 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 856 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 857 | continue; |
| 858 | |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 859 | // FIXME: It seems bad to use RegSeen only for some of these calculations. |
| 860 | bool isNew = ConsiderSeen ? RegSeen.insert(Reg).second : false; |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 861 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
| 862 | |
| 863 | RegClassWeight W = TRI->getRegClassWeight(RC); |
| 864 | int RCCost = 0; |
Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 865 | if (MO.isDef()) |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 866 | RCCost = W.RegWeight; |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 867 | else { |
| 868 | bool isKill = isOperandKill(MO, MRI); |
| 869 | if (isNew && !isKill && ConsiderUnseenAsDef) |
| 870 | // Haven't seen this, it must be a livein. |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 871 | RCCost = W.RegWeight; |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 872 | else if (!isNew && isKill) |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 873 | RCCost = -W.RegWeight; |
| 874 | } |
| 875 | if (RCCost == 0) |
| 876 | continue; |
| 877 | const int *PS = TRI->getRegClassPressureSets(RC); |
| 878 | for (; *PS != -1; ++PS) { |
| 879 | if (Cost.find(*PS) == Cost.end()) |
| 880 | Cost[*PS] = RCCost; |
| 881 | else |
| 882 | Cost[*PS] += RCCost; |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 883 | } |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 884 | } |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 885 | return Cost; |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 888 | /// Return true if this machine instruction loads from global offset table or |
| 889 | /// constant pool. |
Philip Reames | 42bd26f | 2015-12-23 17:05:57 +0000 | [diff] [blame] | 890 | static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI) { |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 891 | assert(MI.mayLoad() && "Expected MI that loads!"); |
Michael Liao | a5d4537 | 2017-04-26 05:27:20 +0000 | [diff] [blame] | 892 | |
Philip Reames | 42bd26f | 2015-12-23 17:05:57 +0000 | [diff] [blame] | 893 | // If we lost memory operands, conservatively assume that the instruction |
Michael Liao | a5d4537 | 2017-04-26 05:27:20 +0000 | [diff] [blame] | 894 | // reads from everything.. |
Philip Reames | 42bd26f | 2015-12-23 17:05:57 +0000 | [diff] [blame] | 895 | if (MI.memoperands_empty()) |
| 896 | return true; |
| 897 | |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 898 | for (MachineMemOperand *MemOp : MI.memoperands()) |
| 899 | if (const PseudoSourceValue *PSV = MemOp->getPseudoValue()) |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 900 | if (PSV->isGOT() || PSV->isConstantPool()) |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 901 | return true; |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 902 | |
Devang Patel | 69a4565 | 2011-10-17 17:35:01 +0000 | [diff] [blame] | 903 | return false; |
| 904 | } |
| 905 | |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 906 | // This function iterates through all the operands of the input store MI and |
| 907 | // checks that each register operand statisfies isCallerPreservedPhysReg. |
| 908 | // This means, the value being stored and the address where it is being stored |
| 909 | // is constant throughout the body of the function (not including prologue and |
| 910 | // epilogue). When called with an MI that isn't a store, it returns false. |
Zaara Syeda | 935474fe | 2018-04-09 14:50:02 +0000 | [diff] [blame] | 911 | // A future improvement can be to check if the store registers are constant |
| 912 | // throughout the loop rather than throughout the funtion. |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 913 | static bool isInvariantStore(const MachineInstr &MI, |
| 914 | const TargetRegisterInfo *TRI, |
| 915 | const MachineRegisterInfo *MRI) { |
| 916 | |
Zaara Syeda | 935474fe | 2018-04-09 14:50:02 +0000 | [diff] [blame] | 917 | bool FoundCallerPresReg = false; |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 918 | if (!MI.mayStore() || MI.hasUnmodeledSideEffects() || |
| 919 | (MI.getNumOperands() == 0)) |
| 920 | return false; |
| 921 | |
| 922 | // Check that all register operands are caller-preserved physical registers. |
| 923 | for (const MachineOperand &MO : MI.operands()) { |
| 924 | if (MO.isReg()) { |
| 925 | unsigned Reg = MO.getReg(); |
| 926 | // If operand is a virtual register, check if it comes from a copy of a |
| 927 | // physical register. |
| 928 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 929 | Reg = TRI->lookThruCopyLike(MO.getReg(), MRI); |
| 930 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 931 | return false; |
| 932 | if (!TRI->isCallerPreservedPhysReg(Reg, *MI.getMF())) |
| 933 | return false; |
Zaara Syeda | 935474fe | 2018-04-09 14:50:02 +0000 | [diff] [blame] | 934 | else |
| 935 | FoundCallerPresReg = true; |
| 936 | } else if (!MO.isImm()) { |
| 937 | return false; |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 938 | } |
| 939 | } |
Zaara Syeda | 935474fe | 2018-04-09 14:50:02 +0000 | [diff] [blame] | 940 | return FoundCallerPresReg; |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | // Return true if the input MI is a copy instruction that feeds an invariant |
| 944 | // store instruction. This means that the src of the copy has to satisfy |
| 945 | // isCallerPreservedPhysReg and atleast one of it's users should satisfy |
| 946 | // isInvariantStore. |
| 947 | static bool isCopyFeedingInvariantStore(const MachineInstr &MI, |
| 948 | const MachineRegisterInfo *MRI, |
| 949 | const TargetRegisterInfo *TRI) { |
| 950 | |
| 951 | // FIXME: If targets would like to look through instructions that aren't |
| 952 | // pure copies, this can be updated to a query. |
| 953 | if (!MI.isCopy()) |
| 954 | return false; |
| 955 | |
| 956 | const MachineFunction *MF = MI.getMF(); |
| 957 | // Check that we are copying a constant physical register. |
| 958 | unsigned CopySrcReg = MI.getOperand(1).getReg(); |
| 959 | if (TargetRegisterInfo::isVirtualRegister(CopySrcReg)) |
| 960 | return false; |
| 961 | |
| 962 | if (!TRI->isCallerPreservedPhysReg(CopySrcReg, *MF)) |
| 963 | return false; |
| 964 | |
| 965 | unsigned CopyDstReg = MI.getOperand(0).getReg(); |
| 966 | // Check if any of the uses of the copy are invariant stores. |
| 967 | assert (TargetRegisterInfo::isVirtualRegister(CopyDstReg) && |
| 968 | "copy dst is not a virtual reg"); |
| 969 | |
| 970 | for (MachineInstr &UseMI : MRI->use_instructions(CopyDstReg)) { |
| 971 | if (UseMI.mayStore() && isInvariantStore(UseMI, TRI, MRI)) |
| 972 | return true; |
| 973 | } |
| 974 | return false; |
| 975 | } |
| 976 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 977 | /// Returns true if the instruction may be a suitable candidate for LICM. |
| 978 | /// e.g. If the instruction is a call, then it's obviously not safe to hoist it. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 979 | bool MachineLICMBase::IsLICMCandidate(MachineInstr &I) { |
Chris Lattner | 0b7ae20 | 2010-07-12 00:00:35 +0000 | [diff] [blame] | 980 | // Check if it's safe to move the instruction. |
| 981 | bool DontMoveAcrossStore = true; |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 982 | if ((!I.isSafeToMove(AA, DontMoveAcrossStore)) && |
| 983 | !(HoistConstStores && isInvariantStore(I, TRI, MRI))) { |
Chris Lattner | c8226f3 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 984 | return false; |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 985 | } |
Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 986 | |
| 987 | // If it is load then check if it is guaranteed to execute by making sure that |
| 988 | // 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] | 989 | // the loop which does not execute this load, so we can't hoist it. Loads |
| 990 | // from constant memory are not safe to speculate all the time, for example |
| 991 | // indexed load from a jump table. |
Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 992 | // Stores and side effects are already checked by isSafeToMove. |
Philip Reames | 42bd26f | 2015-12-23 17:05:57 +0000 | [diff] [blame] | 993 | if (I.mayLoad() && !mayLoadFromGOTOrConstantPool(I) && |
Devang Patel | 69a4565 | 2011-10-17 17:35:01 +0000 | [diff] [blame] | 994 | !IsGuaranteedToExecute(I.getParent())) |
Devang Patel | 453d401 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 995 | return false; |
| 996 | |
Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 997 | return true; |
| 998 | } |
| 999 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1000 | /// Returns true if the instruction is loop invariant. |
| 1001 | /// I.e., all virtual register operands are defined outside of the loop, |
| 1002 | /// physical registers aren't accessed explicitly, and there are no side |
Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 1003 | /// effects that aren't captured by the operands or other flags. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1004 | bool MachineLICMBase::IsLoopInvariantInst(MachineInstr &I) { |
Evan Cheng | 0a2aff2 | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 1005 | if (!IsLICMCandidate(I)) |
| 1006 | return false; |
Bill Wendling | 2823eae | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 1007 | |
Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 1008 | // The instruction is loop invariant if all of its operands are. |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 1009 | for (const MachineOperand &MO : I.operands()) { |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1010 | if (!MO.isReg()) |
Bill Wendling | cd01e89 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 1011 | continue; |
| 1012 | |
Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1013 | unsigned Reg = MO.getReg(); |
| 1014 | if (Reg == 0) continue; |
| 1015 | |
| 1016 | // Don't hoist an instruction that uses or defines a physical register. |
Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 1017 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 1018 | if (MO.isUse()) { |
| 1019 | // 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] | 1020 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 1021 | // it could get allocated to something with a def during allocation. |
Lei Huang | b4733ca | 2017-06-15 18:29:59 +0000 | [diff] [blame] | 1022 | // However, if the physreg is known to always be caller saved/restored |
| 1023 | // then this use is safe to hoist. |
| 1024 | if (!MRI->isConstantPhysReg(Reg) && |
Justin Bogner | fdf9bf4 | 2017-10-10 23:50:49 +0000 | [diff] [blame] | 1025 | !(TRI->isCallerPreservedPhysReg(Reg, *I.getMF()))) |
| 1026 | return false; |
Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 1027 | // Otherwise it's safe to move. |
| 1028 | continue; |
| 1029 | } else if (!MO.isDead()) { |
| 1030 | // A def that isn't dead. We can't move it. |
| 1031 | return false; |
Dan Gohman | 6fb6a59 | 2010-02-28 00:08:44 +0000 | [diff] [blame] | 1032 | } else if (CurLoop->getHeader()->isLiveIn(Reg)) { |
| 1033 | // If the reg is live into the loop, we can't hoist an instruction |
| 1034 | // which would clobber it. |
| 1035 | return false; |
Dan Gohman | e30d63f | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } |
Bill Wendling | cd01e89 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 1038 | |
| 1039 | if (!MO.isUse()) |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1040 | continue; |
| 1041 | |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1042 | assert(MRI->getVRegDef(Reg) && |
Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 1043 | "Machine instr not mapped for this vreg?!"); |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1044 | |
| 1045 | // If the loop contains the definition of an operand, then the instruction |
| 1046 | // isn't loop invariant. |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1047 | if (CurLoop->contains(MRI->getVRegDef(Reg))) |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1048 | return false; |
| 1049 | } |
| 1050 | |
| 1051 | // If we got this far, the instruction is loop invariant! |
| 1052 | return true; |
| 1053 | } |
| 1054 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1055 | /// Return true if the specified instruction is used by a phi node and hoisting |
| 1056 | /// it could cause a copy to be inserted. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1057 | bool MachineLICMBase::HasLoopPHIUse(const MachineInstr *MI) const { |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 1058 | SmallVector<const MachineInstr*, 8> Work(1, MI); |
| 1059 | do { |
| 1060 | MI = Work.pop_back_val(); |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 1061 | for (const MachineOperand &MO : MI->operands()) { |
| 1062 | if (!MO.isReg() || !MO.isDef()) |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 1063 | continue; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 1064 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 1065 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 1066 | continue; |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 1067 | for (MachineInstr &UseMI : MRI->use_instructions(Reg)) { |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 1068 | // A PHI may cause a copy to be inserted. |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 1069 | if (UseMI.isPHI()) { |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 1070 | // A PHI inside the loop causes a copy because the live range of Reg is |
| 1071 | // extended across the PHI. |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 1072 | if (CurLoop->contains(&UseMI)) |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 1073 | return true; |
| 1074 | // A PHI in an exit block can cause a copy to be inserted if the PHI |
| 1075 | // has multiple predecessors in the loop with different values. |
| 1076 | // For now, approximate by rejecting all exit blocks. |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 1077 | if (isExitBlock(UseMI.getParent())) |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 1078 | return true; |
| 1079 | continue; |
| 1080 | } |
| 1081 | // Look past copies as well. |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 1082 | if (UseMI.isCopy() && CurLoop->contains(&UseMI)) |
| 1083 | Work.push_back(&UseMI); |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 1084 | } |
Evan Cheng | ef42bea | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 1085 | } |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 1086 | } while (!Work.empty()); |
Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1087 | return false; |
Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1090 | /// Compute operand latency between a def of 'Reg' and an use in the current |
| 1091 | /// loop, return true if the target considered it high. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1092 | bool MachineLICMBase::HasHighOperandLatency(MachineInstr &MI, |
| 1093 | unsigned DefIdx, |
| 1094 | unsigned Reg) const { |
Matthias Braun | 88e2131 | 2015-06-13 03:42:11 +0000 | [diff] [blame] | 1095 | if (MRI->use_nodbg_empty(Reg)) |
Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1096 | return false; |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1097 | |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 1098 | for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) { |
| 1099 | if (UseMI.isCopyLike()) |
Evan Cheng | e96b8d7 | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 1100 | continue; |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 1101 | if (!CurLoop->contains(UseMI.getParent())) |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1102 | continue; |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 1103 | for (unsigned i = 0, e = UseMI.getNumOperands(); i != e; ++i) { |
| 1104 | const MachineOperand &MO = UseMI.getOperand(i); |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1105 | if (!MO.isReg() || !MO.isUse()) |
| 1106 | continue; |
| 1107 | unsigned MOReg = MO.getReg(); |
| 1108 | if (MOReg != Reg) |
| 1109 | continue; |
| 1110 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1111 | if (TII->hasHighOperandLatency(SchedModel, MRI, MI, DefIdx, UseMI, i)) |
Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1112 | return true; |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1115 | // Only look at the first in loop use. |
| 1116 | break; |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Evan Cheng | 63c7608 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1119 | return false; |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1122 | /// Return true if the instruction is marked "cheap" or the operand latency |
| 1123 | /// between its def and a use is one or less. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1124 | bool MachineLICMBase::IsCheapInstruction(MachineInstr &MI) const { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1125 | if (TII->isAsCheapAsAMove(MI) || MI.isCopyLike()) |
Evan Cheng | e96b8d7 | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 1126 | return true; |
Evan Cheng | e96b8d7 | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 1127 | |
| 1128 | bool isCheap = false; |
| 1129 | unsigned NumDefs = MI.getDesc().getNumDefs(); |
| 1130 | for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) { |
| 1131 | MachineOperand &DefMO = MI.getOperand(i); |
| 1132 | if (!DefMO.isReg() || !DefMO.isDef()) |
| 1133 | continue; |
| 1134 | --NumDefs; |
| 1135 | unsigned Reg = DefMO.getReg(); |
| 1136 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 1137 | continue; |
| 1138 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1139 | if (!TII->hasLowDefLatency(SchedModel, MI, i)) |
Evan Cheng | e96b8d7 | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 1140 | return false; |
| 1141 | isCheap = true; |
| 1142 | } |
| 1143 | |
| 1144 | return isCheap; |
| 1145 | } |
| 1146 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1147 | /// Visit BBs from header to current BB, check if hoisting an instruction of the |
| 1148 | /// given cost matrix can cause high register pressure. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1149 | bool |
| 1150 | MachineLICMBase::CanCauseHighRegPressure(const DenseMap<unsigned, int>& Cost, |
| 1151 | bool CheapInstr) { |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 1152 | for (const auto &RPIdAndCost : Cost) { |
| 1153 | if (RPIdAndCost.second <= 0) |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1154 | continue; |
| 1155 | |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 1156 | unsigned Class = RPIdAndCost.first; |
Daniel Jasper | efece52 | 2015-04-03 16:19:48 +0000 | [diff] [blame] | 1157 | int Limit = RegLimit[Class]; |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1158 | |
| 1159 | // Don't hoist cheap instructions if they would increase register pressure, |
| 1160 | // even if we're under the limit. |
Hal Finkel | 0709f51 | 2015-01-08 22:10:48 +0000 | [diff] [blame] | 1161 | if (CheapInstr && !HoistCheapInsts) |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1162 | return true; |
| 1163 | |
Daniel Jasper | efece52 | 2015-04-03 16:19:48 +0000 | [diff] [blame] | 1164 | for (const auto &RP : BackTrace) |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 1165 | if (static_cast<int>(RP[Class]) + RPIdAndCost.second >= Limit) |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1166 | return true; |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | return false; |
| 1170 | } |
| 1171 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1172 | /// Traverse the back trace from header to the current block and update their |
| 1173 | /// register pressures to reflect the effect of hoisting MI from the current |
| 1174 | /// block to the preheader. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1175 | void MachineLICMBase::UpdateBackTraceRegPressure(const MachineInstr *MI) { |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1176 | // First compute the 'cost' of the instruction, i.e. its contribution |
| 1177 | // to register pressure. |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 1178 | auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false, |
| 1179 | /*ConsiderUnseenAsDef=*/false); |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1180 | |
| 1181 | // Update register pressure of blocks from loop header to current block. |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 1182 | for (auto &RP : BackTrace) |
Daniel Jasper | 274928f | 2015-04-14 11:56:25 +0000 | [diff] [blame] | 1183 | for (const auto &RPIdAndCost : Cost) |
| 1184 | RP[RPIdAndCost.first] += RPIdAndCost.second; |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1187 | /// Return true if it is potentially profitable to hoist the given loop |
| 1188 | /// invariant. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1189 | bool MachineLICMBase::IsProfitableToHoist(MachineInstr &MI) { |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1190 | if (MI.isImplicitDef()) |
| 1191 | return true; |
| 1192 | |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1193 | // Besides removing computation from the loop, hoisting an instruction has |
| 1194 | // these effects: |
| 1195 | // |
| 1196 | // - The value defined by the instruction becomes live across the entire |
| 1197 | // loop. This increases register pressure in the loop. |
| 1198 | // |
| 1199 | // - If the value is used by a PHI in the loop, a copy will be required for |
| 1200 | // lowering the PHI after extending the live range. |
| 1201 | // |
| 1202 | // - When hoisting the last use of a value in the loop, that value no longer |
| 1203 | // needs to be live in the loop. This lowers register pressure in the loop. |
Evan Cheng | 90da66b | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 1204 | |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 1205 | if (HoistConstStores && isCopyFeedingInvariantStore(MI, MRI, TRI)) |
| 1206 | return true; |
| 1207 | |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1208 | bool CheapInstr = IsCheapInstruction(MI); |
| 1209 | bool CreatesCopy = HasLoopPHIUse(&MI); |
Evan Cheng | 4443630 | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1210 | |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1211 | // Don't hoist a cheap instruction if it would create a copy in the loop. |
| 1212 | if (CheapInstr && CreatesCopy) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1213 | LLVM_DEBUG(dbgs() << "Won't hoist cheap instr with loop PHI use: " << MI); |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1214 | return false; |
Evan Cheng | b39a9fd | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1215 | } |
Evan Cheng | 1d9f7ac | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1216 | |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1217 | // Rematerializable instructions should always be hoisted since the register |
| 1218 | // allocator can just pull them down again when needed. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1219 | if (TII->isTriviallyReMaterializable(MI, AA)) |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1220 | return true; |
| 1221 | |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1222 | // FIXME: If there are long latency loop-invariant instructions inside the |
| 1223 | // loop at this point, why didn't the optimizer's LICM hoist them? |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1224 | for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) { |
| 1225 | const MachineOperand &MO = MI.getOperand(i); |
| 1226 | if (!MO.isReg() || MO.isImplicit()) |
| 1227 | continue; |
| 1228 | unsigned Reg = MO.getReg(); |
| 1229 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 1230 | continue; |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 1231 | if (MO.isDef() && HasHighOperandLatency(MI, i, Reg)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1232 | LLVM_DEBUG(dbgs() << "Hoist High Latency: " << MI); |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 1233 | ++NumHighLatency; |
| 1234 | return true; |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
Daniel Jasper | e87e82b | 2015-04-07 16:42:35 +0000 | [diff] [blame] | 1238 | // Estimate register pressure to determine whether to LICM the instruction. |
| 1239 | // In low register pressure situation, we can be more aggressive about |
| 1240 | // hoisting. Also, favors hoisting long latency instructions even in |
| 1241 | // moderately high pressure situation. |
| 1242 | // Cheap instructions will only be hoisted if they don't increase register |
| 1243 | // pressure at all. |
| 1244 | auto Cost = calcRegisterCost(&MI, /*ConsiderSeen=*/false, |
| 1245 | /*ConsiderUnseenAsDef=*/false); |
| 1246 | |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1247 | // Visit BBs from header to current BB, if hoisting this doesn't cause |
| 1248 | // high register pressure, then it's safe to proceed. |
| 1249 | if (!CanCauseHighRegPressure(Cost, CheapInstr)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1250 | LLVM_DEBUG(dbgs() << "Hoist non-reg-pressure: " << MI); |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1251 | ++NumLowRP; |
| 1252 | return true; |
| 1253 | } |
| 1254 | |
| 1255 | // Don't risk increasing register pressure if it would create copies. |
| 1256 | if (CreatesCopy) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1257 | LLVM_DEBUG(dbgs() << "Won't hoist instr with loop PHI use: " << MI); |
Jakob Stoklund Olesen | a3e86a6 | 2012-04-11 00:00:26 +0000 | [diff] [blame] | 1258 | return false; |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | // Do not "speculate" in high register pressure situation. If an |
| 1262 | // instruction is not guaranteed to be executed in the loop, it's best to be |
| 1263 | // conservative. |
| 1264 | if (AvoidSpeculation && |
| 1265 | (!IsGuaranteedToExecute(MI.getParent()) && !MayCSE(&MI))) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1266 | LLVM_DEBUG(dbgs() << "Won't speculate: " << MI); |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1267 | return false; |
| 1268 | } |
| 1269 | |
| 1270 | // High register pressure situation, only hoist if the instruction is going |
| 1271 | // to be remat'ed. |
Justin Lebar | d98cf00 | 2016-09-10 01:03:20 +0000 | [diff] [blame] | 1272 | if (!TII->isTriviallyReMaterializable(MI, AA) && |
| 1273 | !MI.isDereferenceableInvariantLoad(AA)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1274 | LLVM_DEBUG(dbgs() << "Can't remat / high reg-pressure: " << MI); |
Jakob Stoklund Olesen | 645bdd4 | 2012-04-11 00:00:28 +0000 | [diff] [blame] | 1275 | return false; |
| 1276 | } |
Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1277 | |
| 1278 | return true; |
| 1279 | } |
| 1280 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1281 | /// Unfold a load from the given machineinstr if the load itself could be |
| 1282 | /// hoisted. Return the unfolded and hoistable load, or null if the load |
| 1283 | /// couldn't be unfolded or if it wouldn't be hoistable. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1284 | MachineInstr *MachineLICMBase::ExtractHoistableLoad(MachineInstr *MI) { |
Evan Cheng | 4ac0d16 | 2010-10-08 18:59:19 +0000 | [diff] [blame] | 1285 | // Don't unfold simple loads. |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1286 | if (MI->canFoldAsLoad()) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1287 | return nullptr; |
Evan Cheng | 4ac0d16 | 2010-10-08 18:59:19 +0000 | [diff] [blame] | 1288 | |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1289 | // If not, we may be able to unfold a load and hoist that. |
| 1290 | // First test whether the instruction is loading from an amenable |
| 1291 | // memory location. |
Justin Lebar | d98cf00 | 2016-09-10 01:03:20 +0000 | [diff] [blame] | 1292 | if (!MI->isDereferenceableInvariantLoad(AA)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1293 | return nullptr; |
Evan Cheng | b39a9fd | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1294 | |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1295 | // Next determine the register class for a temporary register. |
Dan Gohman | 49fa51d | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 1296 | unsigned LoadRegIndex; |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1297 | unsigned NewOpc = |
| 1298 | TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(), |
| 1299 | /*UnfoldLoad=*/true, |
Dan Gohman | 49fa51d | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 1300 | /*UnfoldStore=*/false, |
| 1301 | &LoadRegIndex); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1302 | if (NewOpc == 0) return nullptr; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1303 | const MCInstrDesc &MID = TII->get(NewOpc); |
Justin Bogner | fdf9bf4 | 2017-10-10 23:50:49 +0000 | [diff] [blame] | 1304 | MachineFunction &MF = *MI->getMF(); |
Jakob Stoklund Olesen | 3c52f02 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 1305 | const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI, MF); |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1306 | // Ok, we're unfolding. Create a temporary register and do the unfold. |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1307 | unsigned Reg = MRI->createVirtualRegister(RC); |
Evan Cheng | b39a9fd | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1308 | |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1309 | SmallVector<MachineInstr *, 2> NewMIs; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1310 | bool Success = TII->unfoldMemoryOperand(MF, *MI, Reg, |
| 1311 | /*UnfoldLoad=*/true, |
| 1312 | /*UnfoldStore=*/false, NewMIs); |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1313 | (void)Success; |
| 1314 | assert(Success && |
| 1315 | "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold " |
| 1316 | "succeeded!"); |
| 1317 | assert(NewMIs.size() == 2 && |
| 1318 | "Unfolded a load into multiple instructions!"); |
| 1319 | MachineBasicBlock *MBB = MI->getParent(); |
Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 1320 | MachineBasicBlock::iterator Pos = MI; |
| 1321 | MBB->insert(Pos, NewMIs[0]); |
| 1322 | MBB->insert(Pos, NewMIs[1]); |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1323 | // If unfolding produced a load that wasn't loop-invariant or profitable to |
| 1324 | // hoist, discard the new instructions and bail. |
Evan Cheng | 73f9a9e | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 1325 | if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) { |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1326 | NewMIs[0]->eraseFromParent(); |
| 1327 | NewMIs[1]->eraseFromParent(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1328 | return nullptr; |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1329 | } |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1330 | |
| 1331 | // Update register pressure for the unfolded instruction. |
| 1332 | UpdateRegPressure(NewMIs[1]); |
| 1333 | |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1334 | // Otherwise we successfully unfolded a load that we can hoist. |
| 1335 | MI->eraseFromParent(); |
| 1336 | return NewMIs[0]; |
| 1337 | } |
| 1338 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1339 | /// Initialize the CSE map with instructions that are in the current loop |
| 1340 | /// preheader that may become duplicates of instructions that are hoisted |
| 1341 | /// out of the loop. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1342 | void MachineLICMBase::InitCSEMap(MachineBasicBlock *BB) { |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 1343 | for (MachineInstr &MI : *BB) |
| 1344 | CSEMap[MI.getOpcode()].push_back(&MI); |
Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1347 | /// Find an instruction amount PrevMIs that is a duplicate of MI. |
| 1348 | /// Return this instruction if it's found. |
Evan Cheng | 7ff8319 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1349 | const MachineInstr* |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1350 | MachineLICMBase::LookForDuplicate(const MachineInstr *MI, |
| 1351 | std::vector<const MachineInstr*> &PrevMIs) { |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 1352 | for (const MachineInstr *PrevMI : PrevMIs) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1353 | if (TII->produceSameValue(*MI, *PrevMI, (PreRegAlloc ? MRI : nullptr))) |
Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1354 | return PrevMI; |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 1355 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1356 | return nullptr; |
Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1359 | /// Given a LICM'ed instruction, look for an instruction on the preheader that |
| 1360 | /// computes the same value. If it's found, do a RAU on with the definition of |
| 1361 | /// the existing instruction rather than hoisting the instruction to the |
| 1362 | /// preheader. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1363 | bool MachineLICMBase::EliminateCSE(MachineInstr *MI, |
| 1364 | DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator &CI) { |
Evan Cheng | d542414 | 2010-07-14 01:22:19 +0000 | [diff] [blame] | 1365 | // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate |
| 1366 | // the undef property onto uses. |
| 1367 | if (CI == CSEMap.end() || MI->isImplicitDef()) |
Evan Cheng | 7ff8319 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1368 | return false; |
| 1369 | |
| 1370 | if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1371 | LLVM_DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup); |
Dan Gohman | 34021b7 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 1372 | |
| 1373 | // Replace virtual registers defined by MI by their counterparts defined |
| 1374 | // by Dup. |
Evan Cheng | aa563df | 2011-10-17 19:50:12 +0000 | [diff] [blame] | 1375 | SmallVector<unsigned, 2> Defs; |
Evan Cheng | 7ff8319 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1376 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1377 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 34021b7 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 1378 | |
| 1379 | // Physical registers may not differ here. |
| 1380 | assert((!MO.isReg() || MO.getReg() == 0 || |
| 1381 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || |
| 1382 | MO.getReg() == Dup->getOperand(i).getReg()) && |
| 1383 | "Instructions with different phys regs are not identical!"); |
| 1384 | |
| 1385 | if (MO.isReg() && MO.isDef() && |
Evan Cheng | aa563df | 2011-10-17 19:50:12 +0000 | [diff] [blame] | 1386 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
| 1387 | Defs.push_back(i); |
| 1388 | } |
| 1389 | |
| 1390 | SmallVector<const TargetRegisterClass*, 2> OrigRCs; |
| 1391 | for (unsigned i = 0, e = Defs.size(); i != e; ++i) { |
| 1392 | unsigned Idx = Defs[i]; |
| 1393 | unsigned Reg = MI->getOperand(Idx).getReg(); |
| 1394 | unsigned DupReg = Dup->getOperand(Idx).getReg(); |
| 1395 | OrigRCs.push_back(MRI->getRegClass(DupReg)); |
| 1396 | |
| 1397 | if (!MRI->constrainRegClass(DupReg, MRI->getRegClass(Reg))) { |
| 1398 | // Restore old RCs if more than one defs. |
| 1399 | for (unsigned j = 0; j != i; ++j) |
| 1400 | MRI->setRegClass(Dup->getOperand(Defs[j]).getReg(), OrigRCs[j]); |
| 1401 | return false; |
Dan Gohman | c90f51c | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1402 | } |
Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1403 | } |
Evan Cheng | aa563df | 2011-10-17 19:50:12 +0000 | [diff] [blame] | 1404 | |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 1405 | for (unsigned Idx : Defs) { |
Evan Cheng | aa563df | 2011-10-17 19:50:12 +0000 | [diff] [blame] | 1406 | unsigned Reg = MI->getOperand(Idx).getReg(); |
| 1407 | unsigned DupReg = Dup->getOperand(Idx).getReg(); |
| 1408 | MRI->replaceRegWith(Reg, DupReg); |
| 1409 | MRI->clearKillFlags(DupReg); |
| 1410 | } |
| 1411 | |
Evan Cheng | 7ff8319 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1412 | MI->eraseFromParent(); |
| 1413 | ++NumCSEed; |
| 1414 | return true; |
Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1415 | } |
| 1416 | return false; |
| 1417 | } |
| 1418 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1419 | /// Return true if the given instruction will be CSE'd if it's hoisted out of |
| 1420 | /// the loop. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1421 | bool MachineLICMBase::MayCSE(MachineInstr *MI) { |
Evan Cheng | af13895 | 2011-10-12 00:09:14 +0000 | [diff] [blame] | 1422 | unsigned Opcode = MI->getOpcode(); |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 1423 | DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator |
Evan Cheng | af13895 | 2011-10-12 00:09:14 +0000 | [diff] [blame] | 1424 | CI = CSEMap.find(Opcode); |
| 1425 | // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate |
| 1426 | // the undef property onto uses. |
| 1427 | if (CI == CSEMap.end() || MI->isImplicitDef()) |
| 1428 | return false; |
| 1429 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1430 | return LookForDuplicate(MI, CI->second) != nullptr; |
Evan Cheng | af13895 | 2011-10-12 00:09:14 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1433 | /// When an instruction is found to use only loop invariant operands |
Bill Wendling | 70613b8 | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 1434 | /// that are safe to hoist, this instruction is called to do the dirty work. |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1435 | /// It returns true if the instruction is hoisted. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1436 | bool MachineLICMBase::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) { |
Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1437 | // First check whether we should hoist this instruction. |
Evan Cheng | 73f9a9e | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 1438 | if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) { |
Dan Gohman | 104f57c | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1439 | // If not, try unfolding a hoistable load. |
| 1440 | MI = ExtractHoistableLoad(MI); |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1441 | if (!MI) return false; |
Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1442 | } |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1443 | |
Zaara Syeda | 6535993 | 2018-03-23 15:28:15 +0000 | [diff] [blame] | 1444 | // If we have hoisted an instruction that may store, it can only be a constant |
| 1445 | // store. |
| 1446 | if (MI->mayStore()) |
| 1447 | NumStoreConst++; |
| 1448 | |
Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1449 | // Now move the instructions to the predecessor, inserting it before any |
| 1450 | // terminator instructions. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1451 | LLVM_DEBUG({ |
| 1452 | dbgs() << "Hoisting " << *MI; |
| 1453 | if (MI->getParent()->getBasicBlock()) |
| 1454 | dbgs() << " from " << printMBBReference(*MI->getParent()); |
| 1455 | if (Preheader->getBasicBlock()) |
| 1456 | dbgs() << " to " << printMBBReference(*Preheader); |
| 1457 | dbgs() << "\n"; |
| 1458 | }); |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1459 | |
Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1460 | // If this is the first instruction being hoisted to the preheader, |
| 1461 | // initialize the CSE map with potential common expressions. |
Evan Cheng | 032f326 | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 1462 | if (FirstInLoop) { |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1463 | InitCSEMap(Preheader); |
Evan Cheng | 032f326 | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 1464 | FirstInLoop = false; |
| 1465 | } |
Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1466 | |
Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1467 | // Look for opportunity to CSE the hoisted instruction. |
Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1468 | unsigned Opcode = MI->getOpcode(); |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 1469 | DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator |
Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1470 | CI = CSEMap.find(Opcode); |
Evan Cheng | 921152f | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1471 | if (!EliminateCSE(MI, CI)) { |
| 1472 | // Otherwise, splice the instruction to the preheader. |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1473 | Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI); |
Evan Cheng | f42b5af | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1474 | |
Wolfgang Pieb | 42f92a7 | 2016-12-02 00:37:57 +0000 | [diff] [blame] | 1475 | // Since we are moving the instruction out of its basic block, we do not |
Michael Liao | a5d4537 | 2017-04-26 05:27:20 +0000 | [diff] [blame] | 1476 | // retain its debug location. Doing so would degrade the debugging |
Wolfgang Pieb | 42f92a7 | 2016-12-02 00:37:57 +0000 | [diff] [blame] | 1477 | // experience and adversely affect the accuracy of profiling information. |
| 1478 | MI->setDebugLoc(DebugLoc()); |
| 1479 | |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1480 | // Update register pressure for BBs from header to this block. |
| 1481 | UpdateBackTraceRegPressure(MI); |
| 1482 | |
Dan Gohman | c90f51c | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1483 | // Clear the kill flags of any register this instruction defines, |
| 1484 | // since they may need to be live throughout the entire loop |
| 1485 | // rather than just live for part of it. |
Sanjay Patel | 882a8ee | 2016-01-06 23:45:05 +0000 | [diff] [blame] | 1486 | for (MachineOperand &MO : MI->operands()) |
Dan Gohman | c90f51c | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1487 | if (MO.isReg() && MO.isDef() && !MO.isDead()) |
Evan Cheng | d62719c | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1488 | MRI->clearKillFlags(MO.getReg()); |
Dan Gohman | c90f51c | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1489 | |
Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1490 | // Add to the CSE map. |
| 1491 | if (CI != CSEMap.end()) |
Dan Gohman | 1b44f10 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1492 | CI->second.push_back(MI); |
Benjamin Kramer | e12a6ba | 2014-10-03 18:33:16 +0000 | [diff] [blame] | 1493 | else |
| 1494 | CSEMap[Opcode].push_back(MI); |
Evan Cheng | 399660c | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1495 | } |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1496 | |
Dan Gohman | 79618d1 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1497 | ++NumHoisted; |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1498 | Changed = true; |
Evan Cheng | 87066f0 | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1499 | |
| 1500 | return true; |
Bill Wendling | fb706bc | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1501 | } |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1502 | |
Sanjay Patel | 87c6c07 | 2015-12-10 16:34:21 +0000 | [diff] [blame] | 1503 | /// Get the preheader for the current loop, splitting a critical edge if needed. |
Matthias Braun | 4a7c8e7 | 2018-01-19 06:46:10 +0000 | [diff] [blame] | 1504 | MachineBasicBlock *MachineLICMBase::getCurPreheader() { |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1505 | // Determine the block to which to hoist instructions. If we can't find a |
| 1506 | // suitable loop predecessor, we can't do any hoisting. |
| 1507 | |
| 1508 | // If we've tried to get a preheader and failed, don't try again. |
| 1509 | if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1510 | return nullptr; |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1511 | |
| 1512 | if (!CurPreheader) { |
| 1513 | CurPreheader = CurLoop->getLoopPreheader(); |
| 1514 | if (!CurPreheader) { |
| 1515 | MachineBasicBlock *Pred = CurLoop->getLoopPredecessor(); |
| 1516 | if (!Pred) { |
| 1517 | CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1518 | return nullptr; |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Quentin Colombet | 23341a8 | 2016-04-21 21:01:13 +0000 | [diff] [blame] | 1521 | CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), *this); |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1522 | if (!CurPreheader) { |
| 1523 | CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1524 | return nullptr; |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | return CurPreheader; |
| 1529 | } |