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