| Eugene Zelenko | 8361b0a | 2017-06-19 22:43:19 +0000 | [diff] [blame] | 1 | //===- HexagonSubtarget.cpp - Hexagon Subtarget Information ---------------===// |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Hexagon specific subclass of TargetSubtarget. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 14 | #include "Hexagon.h" |
| Eugene Zelenko | 8361b0a | 2017-06-19 22:43:19 +0000 | [diff] [blame] | 15 | #include "HexagonInstrInfo.h" |
| Sirish Pande | 69295b8 | 2012-05-10 20:20:25 +0000 | [diff] [blame] | 16 | #include "HexagonRegisterInfo.h" |
| Eugene Zelenko | 8361b0a | 2017-06-19 22:43:19 +0000 | [diff] [blame] | 17 | #include "HexagonSubtarget.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "MCTargetDesc/HexagonMCTargetDesc.h" |
| 23 | #include "llvm/CodeGen/MachineInstr.h" |
| 24 | #include "llvm/CodeGen/MachineOperand.h" |
| Krzysztof Parzyszek | 9be6673 | 2016-07-15 17:48:09 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/ScheduleDAG.h" |
| 26 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| Eugene Zelenko | 8361b0a | 2017-06-19 22:43:19 +0000 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <cassert> |
| Krzysztof Parzyszek | 207c13f | 2015-11-25 20:30:59 +0000 | [diff] [blame] | 31 | #include <map> |
| 32 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
| Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 35 | #define DEBUG_TYPE "hexagon-subtarget" |
| 36 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 37 | #define GET_SUBTARGETINFO_CTOR |
| 38 | #define GET_SUBTARGETINFO_TARGET_DESC |
| 39 | #include "HexagonGenSubtargetInfo.inc" |
| 40 | |
| Krzysztof Parzyszek | 207c13f | 2015-11-25 20:30:59 +0000 | [diff] [blame] | 41 | static cl::opt<bool> EnableMemOps("enable-hexagon-memops", |
| 42 | cl::Hidden, cl::ZeroOrMore, cl::ValueDisallowed, cl::init(true), |
| 43 | cl::desc("Generate V4 MEMOP in code generation for Hexagon target")); |
| Jyotsna Verma | fdc660b | 2013-03-22 18:41:34 +0000 | [diff] [blame] | 44 | |
| Krzysztof Parzyszek | 207c13f | 2015-11-25 20:30:59 +0000 | [diff] [blame] | 45 | static cl::opt<bool> DisableMemOps("disable-hexagon-memops", |
| 46 | cl::Hidden, cl::ZeroOrMore, cl::ValueDisallowed, cl::init(false), |
| 47 | cl::desc("Do not generate V4 MEMOP in code generation for Hexagon target")); |
| Sirish Pande | 69295b8 | 2012-05-10 20:20:25 +0000 | [diff] [blame] | 48 | |
| Krzysztof Parzyszek | 207c13f | 2015-11-25 20:30:59 +0000 | [diff] [blame] | 49 | static cl::opt<bool> EnableIEEERndNear("enable-hexagon-ieee-rnd-near", |
| 50 | cl::Hidden, cl::ZeroOrMore, cl::init(false), |
| 51 | cl::desc("Generate non-chopped conversion from fp to int.")); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 52 | |
| Krzysztof Parzyszek | b9a1c3a | 2015-11-24 14:55:26 +0000 | [diff] [blame] | 53 | static cl::opt<bool> EnableBSBSched("enable-bsb-sched", |
| Krzysztof Parzyszek | 207c13f | 2015-11-25 20:30:59 +0000 | [diff] [blame] | 54 | cl::Hidden, cl::ZeroOrMore, cl::init(true)); |
| 55 | |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 56 | static cl::opt<bool> EnableTCLatencySched("enable-tc-latency-sched", |
| 57 | cl::Hidden, cl::ZeroOrMore, cl::init(false)); |
| 58 | |
| 59 | static cl::opt<bool> EnableDotCurSched("enable-cur-sched", |
| 60 | cl::Hidden, cl::ZeroOrMore, cl::init(true), |
| 61 | cl::desc("Enable the scheduler to generate .cur")); |
| 62 | |
| 63 | static cl::opt<bool> EnableVecFrwdSched("enable-evec-frwd-sched", |
| 64 | cl::Hidden, cl::ZeroOrMore, cl::init(true)); |
| 65 | |
| Eric Christopher | 5f141b0 | 2015-03-11 22:56:10 +0000 | [diff] [blame] | 66 | static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched", |
| Krzysztof Parzyszek | 207c13f | 2015-11-25 20:30:59 +0000 | [diff] [blame] | 67 | cl::Hidden, cl::ZeroOrMore, cl::init(false), |
| 68 | cl::desc("Disable Hexagon MI Scheduling")); |
| 69 | |
| Krzysztof Parzyszek | 07d7518 | 2016-05-28 02:02:51 +0000 | [diff] [blame] | 70 | static cl::opt<bool> EnableSubregLiveness("hexagon-subreg-liveness", |
| Krzysztof Parzyszek | b5ec487 | 2016-08-24 17:17:39 +0000 | [diff] [blame] | 71 | cl::Hidden, cl::ZeroOrMore, cl::init(true), |
| Krzysztof Parzyszek | 07d7518 | 2016-05-28 02:02:51 +0000 | [diff] [blame] | 72 | cl::desc("Enable subregister liveness tracking for Hexagon")); |
| 73 | |
| Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 74 | static cl::opt<bool> OverrideLongCalls("hexagon-long-calls", |
| 75 | cl::Hidden, cl::ZeroOrMore, cl::init(false), |
| 76 | cl::desc("If present, forces/disables the use of long calls")); |
| 77 | |
| Krzysztof Parzyszek | ee93e00 | 2017-05-05 22:13:57 +0000 | [diff] [blame] | 78 | static cl::opt<bool> EnablePredicatedCalls("hexagon-pred-calls", |
| 79 | cl::Hidden, cl::ZeroOrMore, cl::init(false), |
| 80 | cl::desc("Consider calls to be predicable")); |
| 81 | |
| Krzysztof Parzyszek | 95da97e | 2017-08-28 16:24:22 +0000 | [diff] [blame] | 82 | static cl::opt<bool> SchedPredsCloser("sched-preds-closer", |
| 83 | cl::Hidden, cl::ZeroOrMore, cl::init(true)); |
| 84 | |
| 85 | static cl::opt<bool> SchedRetvalOptimization("sched-retval-optimization", |
| 86 | cl::Hidden, cl::ZeroOrMore, cl::init(true)); |
| 87 | |
| Krzysztof Parzyszek | 2164a27 | 2017-08-28 18:36:21 +0000 | [diff] [blame] | 88 | static cl::opt<bool> EnableCheckBankConflict("hexagon-check-bank-conflict", |
| 89 | cl::Hidden, cl::ZeroOrMore, cl::init(true), |
| 90 | cl::desc("Enable checking for cache bank conflicts")); |
| 91 | |
| Krzysztof Parzyszek | 95da97e | 2017-08-28 16:24:22 +0000 | [diff] [blame] | 92 | |
| Krzysztof Parzyszek | 1665b3d | 2017-09-26 15:06:37 +0000 | [diff] [blame] | 93 | HexagonSubtarget::HexagonSubtarget(const Triple &TT, StringRef CPU, |
| 94 | StringRef FS, const TargetMachine &TM) |
| Krzysztof Parzyszek | d768142 | 2017-11-30 21:25:28 +0000 | [diff] [blame] | 95 | : HexagonGenSubtargetInfo(TT, CPU, FS), OptLevel(TM.getOptLevel()), |
| Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 96 | CPUString(Hexagon_MC::selectHexagonCPU(CPU)), |
| Krzysztof Parzyszek | 1665b3d | 2017-09-26 15:06:37 +0000 | [diff] [blame] | 97 | InstrInfo(initializeSubtargetDependencies(CPU, FS)), |
| 98 | RegInfo(getHwMode()), TLInfo(TM, *this), |
| 99 | InstrItins(getInstrItineraryForCPU(CPUString)) { |
| 100 | // Beware of the default constructor of InstrItineraryData: it will |
| 101 | // reset all members to 0. |
| 102 | assert(InstrItins.Itineraries != nullptr && "InstrItins not initialized"); |
| Krzysztof Parzyszek | 207c13f | 2015-11-25 20:30:59 +0000 | [diff] [blame] | 103 | } |
| Eric Christopher | 5f141b0 | 2015-03-11 22:56:10 +0000 | [diff] [blame] | 104 | |
| Eric Christopher | c4c63ae | 2014-06-27 00:27:40 +0000 | [diff] [blame] | 105 | HexagonSubtarget & |
| 106 | HexagonSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS) { |
| Sumanth Gundapaneni | 9d954c4 | 2017-10-18 17:45:22 +0000 | [diff] [blame] | 107 | static std::map<StringRef, Hexagon::ArchEnum> CpuTable{ |
| 108 | {"hexagonv4", Hexagon::ArchEnum::V4}, |
| 109 | {"hexagonv5", Hexagon::ArchEnum::V5}, |
| 110 | {"hexagonv55", Hexagon::ArchEnum::V55}, |
| 111 | {"hexagonv60", Hexagon::ArchEnum::V60}, |
| 112 | {"hexagonv62", Hexagon::ArchEnum::V62}, |
| Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 113 | {"hexagonv65", Hexagon::ArchEnum::V65}, |
| Krzysztof Parzyszek | 207c13f | 2015-11-25 20:30:59 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| Krzysztof Parzyszek | 1665b3d | 2017-09-26 15:06:37 +0000 | [diff] [blame] | 116 | auto FoundIt = CpuTable.find(CPUString); |
| 117 | if (FoundIt != CpuTable.end()) |
| 118 | HexagonArchVersion = FoundIt->second; |
| Krzysztof Parzyszek | 207c13f | 2015-11-25 20:30:59 +0000 | [diff] [blame] | 119 | else |
| Sebastian Pop | 1a0bef6 | 2012-08-20 19:56:47 +0000 | [diff] [blame] | 120 | llvm_unreachable("Unrecognized Hexagon processor version"); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 121 | |
| Sumanth Gundapaneni | e1983bc | 2017-10-18 18:07:07 +0000 | [diff] [blame] | 122 | UseHVX128BOps = false; |
| 123 | UseHVX64BOps = false; |
| Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 124 | UseLongCalls = false; |
| Krzysztof Parzyszek | 1665b3d | 2017-09-26 15:06:37 +0000 | [diff] [blame] | 125 | |
| 126 | UseMemOps = DisableMemOps ? false : EnableMemOps; |
| 127 | ModeIEEERndNear = EnableIEEERndNear; |
| 128 | UseBSBScheduling = hasV60TOps() && EnableBSBSched; |
| 129 | |
| Sebastian Pop | 1a0bef6 | 2012-08-20 19:56:47 +0000 | [diff] [blame] | 130 | ParseSubtargetFeatures(CPUString, FS); |
| Krzysztof Parzyszek | 207c13f | 2015-11-25 20:30:59 +0000 | [diff] [blame] | 131 | |
| Krzysztof Parzyszek | 080bebd | 2016-07-25 14:42:11 +0000 | [diff] [blame] | 132 | if (OverrideLongCalls.getPosition()) |
| 133 | UseLongCalls = OverrideLongCalls; |
| Krzysztof Parzyszek | 207c13f | 2015-11-25 20:30:59 +0000 | [diff] [blame] | 134 | |
| Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 135 | FeatureBitset Features = getFeatureBits(); |
| 136 | if (HexagonDisableDuplex) |
| 137 | setFeatureBits(Features.set(Hexagon::FeatureDuplex, false)); |
| 138 | setFeatureBits(Hexagon_MC::completeHVXFeatures(Features)); |
| 139 | |
| Eric Christopher | c4c63ae | 2014-06-27 00:27:40 +0000 | [diff] [blame] | 140 | return *this; |
| 141 | } |
| 142 | |
| Krzysztof Parzyszek | 95da97e | 2017-08-28 16:24:22 +0000 | [diff] [blame] | 143 | void HexagonSubtarget::UsrOverflowMutation::apply(ScheduleDAGInstrs *DAG) { |
| 144 | for (SUnit &SU : DAG->SUnits) { |
| 145 | if (!SU.isInstr()) |
| 146 | continue; |
| 147 | SmallVector<SDep, 4> Erase; |
| 148 | for (auto &D : SU.Preds) |
| 149 | if (D.getKind() == SDep::Output && D.getReg() == Hexagon::USR_OVF) |
| 150 | Erase.push_back(D); |
| 151 | for (auto &E : Erase) |
| 152 | SU.removePred(E); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void HexagonSubtarget::HVXMemLatencyMutation::apply(ScheduleDAGInstrs *DAG) { |
| 157 | for (SUnit &SU : DAG->SUnits) { |
| 158 | // Update the latency of chain edges between v60 vector load or store |
| 159 | // instructions to be 1. These instruction cannot be scheduled in the |
| 160 | // same packet. |
| 161 | MachineInstr &MI1 = *SU.getInstr(); |
| 162 | auto *QII = static_cast<const HexagonInstrInfo*>(DAG->TII); |
| 163 | bool IsStoreMI1 = MI1.mayStore(); |
| 164 | bool IsLoadMI1 = MI1.mayLoad(); |
| 165 | if (!QII->isHVXVec(MI1) || !(IsStoreMI1 || IsLoadMI1)) |
| 166 | continue; |
| 167 | for (SDep &SI : SU.Succs) { |
| 168 | if (SI.getKind() != SDep::Order || SI.getLatency() != 0) |
| 169 | continue; |
| 170 | MachineInstr &MI2 = *SI.getSUnit()->getInstr(); |
| 171 | if (!QII->isHVXVec(MI2)) |
| 172 | continue; |
| 173 | if ((IsStoreMI1 && MI2.mayStore()) || (IsLoadMI1 && MI2.mayLoad())) { |
| 174 | SI.setLatency(1); |
| 175 | SU.setHeightDirty(); |
| 176 | // Change the dependence in the opposite direction too. |
| 177 | for (SDep &PI : SI.getSUnit()->Preds) { |
| 178 | if (PI.getSUnit() != &SU || PI.getKind() != SDep::Order) |
| 179 | continue; |
| 180 | PI.setLatency(1); |
| 181 | SI.getSUnit()->setDepthDirty(); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Check if a call and subsequent A2_tfrpi instructions should maintain |
| 189 | // scheduling affinity. We are looking for the TFRI to be consumed in |
| 190 | // the next instruction. This should help reduce the instances of |
| 191 | // double register pairs being allocated and scheduled before a call |
| 192 | // when not used until after the call. This situation is exacerbated |
| 193 | // by the fact that we allocate the pair from the callee saves list, |
| 194 | // leading to excess spills and restores. |
| 195 | bool HexagonSubtarget::CallMutation::shouldTFRICallBind( |
| 196 | const HexagonInstrInfo &HII, const SUnit &Inst1, |
| 197 | const SUnit &Inst2) const { |
| 198 | if (Inst1.getInstr()->getOpcode() != Hexagon::A2_tfrpi) |
| 199 | return false; |
| 200 | |
| 201 | // TypeXTYPE are 64 bit operations. |
| 202 | unsigned Type = HII.getType(*Inst2.getInstr()); |
| 203 | return Type == HexagonII::TypeS_2op || Type == HexagonII::TypeS_3op || |
| 204 | Type == HexagonII::TypeALU64 || Type == HexagonII::TypeM; |
| 205 | } |
| 206 | |
| 207 | void HexagonSubtarget::CallMutation::apply(ScheduleDAGInstrs *DAG) { |
| 208 | SUnit* LastSequentialCall = nullptr; |
| 209 | unsigned VRegHoldingRet = 0; |
| 210 | unsigned RetRegister; |
| 211 | SUnit* LastUseOfRet = nullptr; |
| 212 | auto &TRI = *DAG->MF.getSubtarget().getRegisterInfo(); |
| 213 | auto &HII = *DAG->MF.getSubtarget<HexagonSubtarget>().getInstrInfo(); |
| 214 | |
| 215 | // Currently we only catch the situation when compare gets scheduled |
| 216 | // before preceding call. |
| 217 | for (unsigned su = 0, e = DAG->SUnits.size(); su != e; ++su) { |
| 218 | // Remember the call. |
| 219 | if (DAG->SUnits[su].getInstr()->isCall()) |
| 220 | LastSequentialCall = &DAG->SUnits[su]; |
| 221 | // Look for a compare that defines a predicate. |
| 222 | else if (DAG->SUnits[su].getInstr()->isCompare() && LastSequentialCall) |
| 223 | DAG->SUnits[su].addPred(SDep(LastSequentialCall, SDep::Barrier)); |
| 224 | // Look for call and tfri* instructions. |
| 225 | else if (SchedPredsCloser && LastSequentialCall && su > 1 && su < e-1 && |
| 226 | shouldTFRICallBind(HII, DAG->SUnits[su], DAG->SUnits[su+1])) |
| 227 | DAG->SUnits[su].addPred(SDep(&DAG->SUnits[su-1], SDep::Barrier)); |
| 228 | // Prevent redundant register copies between two calls, which are caused by |
| Francis Visoiu Mistrih | 9d7bb0c | 2017-11-28 17:15:09 +0000 | [diff] [blame] | 229 | // both the return value and the argument for the next call being in %r0. |
| Krzysztof Parzyszek | 95da97e | 2017-08-28 16:24:22 +0000 | [diff] [blame] | 230 | // Example: |
| 231 | // 1: <call1> |
| Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 232 | // 2: %vreg = COPY %r0 |
| 233 | // 3: <use of %vreg> |
| Francis Visoiu Mistrih | 9d7bb0c | 2017-11-28 17:15:09 +0000 | [diff] [blame] | 234 | // 4: %r0 = ... |
| Krzysztof Parzyszek | 95da97e | 2017-08-28 16:24:22 +0000 | [diff] [blame] | 235 | // 5: <call2> |
| 236 | // The scheduler would often swap 3 and 4, so an additional register is |
| 237 | // needed. This code inserts a Barrier dependence between 3 & 4 to prevent |
| Francis Visoiu Mistrih | 9d7bb0c | 2017-11-28 17:15:09 +0000 | [diff] [blame] | 238 | // this. The same applies for %d0 and %v0/%w0, which are also handled. |
| Krzysztof Parzyszek | 95da97e | 2017-08-28 16:24:22 +0000 | [diff] [blame] | 239 | else if (SchedRetvalOptimization) { |
| 240 | const MachineInstr *MI = DAG->SUnits[su].getInstr(); |
| 241 | if (MI->isCopy() && (MI->readsRegister(Hexagon::R0, &TRI) || |
| 242 | MI->readsRegister(Hexagon::V0, &TRI))) { |
| Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 243 | // %vreg = COPY %r0 |
| Krzysztof Parzyszek | 95da97e | 2017-08-28 16:24:22 +0000 | [diff] [blame] | 244 | VRegHoldingRet = MI->getOperand(0).getReg(); |
| 245 | RetRegister = MI->getOperand(1).getReg(); |
| 246 | LastUseOfRet = nullptr; |
| 247 | } else if (VRegHoldingRet && MI->readsVirtualRegister(VRegHoldingRet)) |
| Francis Visoiu Mistrih | 93ef145 | 2017-11-30 12:12:19 +0000 | [diff] [blame] | 248 | // <use of %X> |
| Krzysztof Parzyszek | 95da97e | 2017-08-28 16:24:22 +0000 | [diff] [blame] | 249 | LastUseOfRet = &DAG->SUnits[su]; |
| 250 | else if (LastUseOfRet && MI->definesRegister(RetRegister, &TRI)) |
| Francis Visoiu Mistrih | 9d7bb0c | 2017-11-28 17:15:09 +0000 | [diff] [blame] | 251 | // %r0 = ... |
| Krzysztof Parzyszek | 95da97e | 2017-08-28 16:24:22 +0000 | [diff] [blame] | 252 | DAG->SUnits[su].addPred(SDep(LastUseOfRet, SDep::Barrier)); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| Krzysztof Parzyszek | 2164a27 | 2017-08-28 18:36:21 +0000 | [diff] [blame] | 257 | void HexagonSubtarget::BankConflictMutation::apply(ScheduleDAGInstrs *DAG) { |
| 258 | if (!EnableCheckBankConflict) |
| 259 | return; |
| 260 | |
| 261 | const auto &HII = static_cast<const HexagonInstrInfo&>(*DAG->TII); |
| 262 | |
| 263 | // Create artificial edges between loads that could likely cause a bank |
| 264 | // conflict. Since such loads would normally not have any dependency |
| 265 | // between them, we cannot rely on existing edges. |
| 266 | for (unsigned i = 0, e = DAG->SUnits.size(); i != e; ++i) { |
| 267 | SUnit &S0 = DAG->SUnits[i]; |
| 268 | MachineInstr &L0 = *S0.getInstr(); |
| 269 | if (!L0.mayLoad() || L0.mayStore() || |
| 270 | HII.getAddrMode(L0) != HexagonII::BaseImmOffset) |
| 271 | continue; |
| 272 | int Offset0; |
| 273 | unsigned Size0; |
| 274 | unsigned Base0 = HII.getBaseAndOffset(L0, Offset0, Size0); |
| 275 | // Is the access size is longer than the L1 cache line, skip the check. |
| 276 | if (Base0 == 0 || Size0 >= 32) |
| 277 | continue; |
| 278 | // Scan only up to 32 instructions ahead (to avoid n^2 complexity). |
| 279 | for (unsigned j = i+1, m = std::min(i+32, e); j != m; ++j) { |
| 280 | SUnit &S1 = DAG->SUnits[j]; |
| 281 | MachineInstr &L1 = *S1.getInstr(); |
| 282 | if (!L1.mayLoad() || L1.mayStore() || |
| 283 | HII.getAddrMode(L1) != HexagonII::BaseImmOffset) |
| 284 | continue; |
| 285 | int Offset1; |
| 286 | unsigned Size1; |
| 287 | unsigned Base1 = HII.getBaseAndOffset(L1, Offset1, Size1); |
| 288 | if (Base1 == 0 || Size1 >= 32 || Base0 != Base1) |
| 289 | continue; |
| 290 | // Check bits 3 and 4 of the offset: if they differ, a bank conflict |
| 291 | // is unlikely. |
| 292 | if (((Offset0 ^ Offset1) & 0x18) != 0) |
| 293 | continue; |
| 294 | // Bits 3 and 4 are the same, add an artificial edge and set extra |
| 295 | // latency. |
| 296 | SDep A(&S0, SDep::Artificial); |
| 297 | A.setLatency(1); |
| 298 | S1.addPred(A, true); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| Krzysztof Parzyszek | d768142 | 2017-11-30 21:25:28 +0000 | [diff] [blame] | 303 | /// \brief Enable use of alias analysis during code generation (during MI |
| 304 | /// scheduling, DAGCombine, etc.). |
| 305 | bool HexagonSubtarget::useAA() const { |
| 306 | if (OptLevel != CodeGenOpt::None) |
| 307 | return true; |
| 308 | return false; |
| 309 | } |
| 310 | |
| Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 311 | /// \brief Perform target specific adjustments to the latency of a schedule |
| 312 | /// dependency. |
| 313 | void HexagonSubtarget::adjustSchedDependency(SUnit *Src, SUnit *Dst, |
| 314 | SDep &Dep) const { |
| 315 | MachineInstr *SrcInst = Src->getInstr(); |
| 316 | MachineInstr *DstInst = Dst->getInstr(); |
| 317 | if (!Src->isInstr() || !Dst->isInstr()) |
| 318 | return; |
| 319 | |
| 320 | const HexagonInstrInfo *QII = getInstrInfo(); |
| 321 | |
| 322 | // Instructions with .new operands have zero latency. |
| 323 | SmallSet<SUnit *, 4> ExclSrc; |
| 324 | SmallSet<SUnit *, 4> ExclDst; |
| 325 | if (QII->canExecuteInBundle(*SrcInst, *DstInst) && |
| 326 | isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) { |
| 327 | Dep.setLatency(0); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | if (!hasV60TOps()) |
| 332 | return; |
| 333 | |
| 334 | // If it's a REG_SEQUENCE, use its destination instruction to determine |
| 335 | // the correct latency. |
| 336 | if (DstInst->isRegSequence() && Dst->NumSuccs == 1) { |
| 337 | unsigned RSeqReg = DstInst->getOperand(0).getReg(); |
| 338 | MachineInstr *RSeqDst = Dst->Succs[0].getSUnit()->getInstr(); |
| 339 | unsigned UseIdx = -1; |
| 340 | for (unsigned OpNum = 0; OpNum < RSeqDst->getNumOperands(); OpNum++) { |
| 341 | const MachineOperand &MO = RSeqDst->getOperand(OpNum); |
| 342 | if (MO.isReg() && MO.getReg() && MO.isUse() && MO.getReg() == RSeqReg) { |
| 343 | UseIdx = OpNum; |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | unsigned RSeqLatency = (InstrInfo.getOperandLatency(&InstrItins, *SrcInst, |
| 348 | 0, *RSeqDst, UseIdx)); |
| 349 | Dep.setLatency(RSeqLatency); |
| 350 | } |
| 351 | |
| 352 | // Try to schedule uses near definitions to generate .cur. |
| 353 | ExclSrc.clear(); |
| 354 | ExclDst.clear(); |
| 355 | if (EnableDotCurSched && QII->isToBeScheduledASAP(*SrcInst, *DstInst) && |
| 356 | isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) { |
| 357 | Dep.setLatency(0); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | updateLatency(*SrcInst, *DstInst, Dep); |
| 362 | } |
| 363 | |
| Krzysztof Parzyszek | 9be6673 | 2016-07-15 17:48:09 +0000 | [diff] [blame] | 364 | void HexagonSubtarget::getPostRAMutations( |
| Eugene Zelenko | 8361b0a | 2017-06-19 22:43:19 +0000 | [diff] [blame] | 365 | std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const { |
| Krzysztof Parzyszek | 95da97e | 2017-08-28 16:24:22 +0000 | [diff] [blame] | 366 | Mutations.push_back(llvm::make_unique<UsrOverflowMutation>()); |
| 367 | Mutations.push_back(llvm::make_unique<HVXMemLatencyMutation>()); |
| Krzysztof Parzyszek | 2164a27 | 2017-08-28 18:36:21 +0000 | [diff] [blame] | 368 | Mutations.push_back(llvm::make_unique<BankConflictMutation>()); |
| Krzysztof Parzyszek | 9be6673 | 2016-07-15 17:48:09 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| Krzysztof Parzyszek | 3885d87 | 2016-12-22 19:44:55 +0000 | [diff] [blame] | 371 | void HexagonSubtarget::getSMSMutations( |
| Eugene Zelenko | 8361b0a | 2017-06-19 22:43:19 +0000 | [diff] [blame] | 372 | std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const { |
| Krzysztof Parzyszek | 95da97e | 2017-08-28 16:24:22 +0000 | [diff] [blame] | 373 | Mutations.push_back(llvm::make_unique<UsrOverflowMutation>()); |
| 374 | Mutations.push_back(llvm::make_unique<HVXMemLatencyMutation>()); |
| Krzysztof Parzyszek | 3885d87 | 2016-12-22 19:44:55 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 377 | // Pin the vtable to this file. |
| 378 | void HexagonSubtarget::anchor() {} |
| Eric Christopher | 5f141b0 | 2015-03-11 22:56:10 +0000 | [diff] [blame] | 379 | |
| 380 | bool HexagonSubtarget::enableMachineScheduler() const { |
| 381 | if (DisableHexagonMISched.getNumOccurrences()) |
| 382 | return !DisableHexagonMISched; |
| 383 | return true; |
| 384 | } |
| Krzysztof Parzyszek | 07d7518 | 2016-05-28 02:02:51 +0000 | [diff] [blame] | 385 | |
| Krzysztof Parzyszek | ee93e00 | 2017-05-05 22:13:57 +0000 | [diff] [blame] | 386 | bool HexagonSubtarget::usePredicatedCalls() const { |
| 387 | return EnablePredicatedCalls; |
| 388 | } |
| 389 | |
| Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 390 | void HexagonSubtarget::updateLatency(MachineInstr &SrcInst, |
| 391 | MachineInstr &DstInst, SDep &Dep) const { |
| Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 392 | if (Dep.isArtificial()) { |
| 393 | Dep.setLatency(1); |
| 394 | return; |
| 395 | } |
| 396 | |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 397 | if (!hasV60TOps()) |
| 398 | return; |
| 399 | |
| 400 | auto &QII = static_cast<const HexagonInstrInfo&>(*getInstrInfo()); |
| 401 | |
| Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 402 | // BSB scheduling. |
| 403 | if (QII.isHVXVec(SrcInst) || useBSBScheduling()) |
| 404 | Dep.setLatency((Dep.getLatency() + 1) >> 1); |
| 405 | } |
| 406 | |
| 407 | void HexagonSubtarget::restoreLatency(SUnit *Src, SUnit *Dst) const { |
| 408 | MachineInstr *SrcI = Src->getInstr(); |
| 409 | for (auto &I : Src->Succs) { |
| 410 | if (!I.isAssignedRegDep() || I.getSUnit() != Dst) |
| 411 | continue; |
| 412 | unsigned DepR = I.getReg(); |
| 413 | int DefIdx = -1; |
| 414 | for (unsigned OpNum = 0; OpNum < SrcI->getNumOperands(); OpNum++) { |
| 415 | const MachineOperand &MO = SrcI->getOperand(OpNum); |
| 416 | if (MO.isReg() && MO.isDef() && MO.getReg() == DepR) |
| 417 | DefIdx = OpNum; |
| 418 | } |
| 419 | assert(DefIdx >= 0 && "Def Reg not found in Src MI"); |
| 420 | MachineInstr *DstI = Dst->getInstr(); |
| 421 | for (unsigned OpNum = 0; OpNum < DstI->getNumOperands(); OpNum++) { |
| 422 | const MachineOperand &MO = DstI->getOperand(OpNum); |
| 423 | if (MO.isReg() && MO.isUse() && MO.getReg() == DepR) { |
| 424 | int Latency = (InstrInfo.getOperandLatency(&InstrItins, *SrcI, |
| 425 | DefIdx, *DstI, OpNum)); |
| 426 | |
| 427 | // For some instructions (ex: COPY), we might end up with < 0 latency |
| 428 | // as they don't have any Itinerary class associated with them. |
| 429 | if (Latency <= 0) |
| 430 | Latency = 1; |
| 431 | |
| 432 | I.setLatency(Latency); |
| 433 | updateLatency(*SrcI, *DstI, I); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | // Update the latency of opposite edge too. |
| 438 | for (auto &J : Dst->Preds) { |
| 439 | if (J.getSUnit() != Src) |
| 440 | continue; |
| 441 | J.setLatency(I.getLatency()); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /// Change the latency between the two SUnits. |
| 447 | void HexagonSubtarget::changeLatency(SUnit *Src, SUnit *Dst, unsigned Lat) |
| 448 | const { |
| 449 | for (auto &I : Src->Succs) { |
| 450 | if (I.getSUnit() != Dst) |
| 451 | continue; |
| 452 | SDep T = I; |
| 453 | I.setLatency(Lat); |
| 454 | |
| 455 | // Update the latency of opposite edge too. |
| 456 | T.setSUnit(Src); |
| 457 | auto F = std::find(Dst->Preds.begin(), Dst->Preds.end(), T); |
| 458 | assert(F != Dst->Preds.end()); |
| 459 | F->setLatency(I.getLatency()); |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 463 | /// If the SUnit has a zero latency edge, return the other SUnit. |
| 464 | static SUnit *getZeroLatency(SUnit *N, SmallVector<SDep, 4> &Deps) { |
| 465 | for (auto &I : Deps) |
| 466 | if (I.isAssignedRegDep() && I.getLatency() == 0 && |
| 467 | !I.getSUnit()->getInstr()->isPseudo()) |
| 468 | return I.getSUnit(); |
| 469 | return nullptr; |
| 470 | } |
| 471 | |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 472 | // Return true if these are the best two instructions to schedule |
| 473 | // together with a zero latency. Only one dependence should have a zero |
| 474 | // latency. If there are multiple choices, choose the best, and change |
| Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 475 | // the others, if needed. |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 476 | bool HexagonSubtarget::isBestZeroLatency(SUnit *Src, SUnit *Dst, |
| Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 477 | const HexagonInstrInfo *TII, SmallSet<SUnit*, 4> &ExclSrc, |
| 478 | SmallSet<SUnit*, 4> &ExclDst) const { |
| Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 479 | MachineInstr &SrcInst = *Src->getInstr(); |
| 480 | MachineInstr &DstInst = *Dst->getInstr(); |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 481 | |
| Ron Lieberman | da5df7c | 2016-09-17 16:21:09 +0000 | [diff] [blame] | 482 | // Ignore Boundary SU nodes as these have null instructions. |
| 483 | if (Dst->isBoundaryNode()) |
| 484 | return false; |
| 485 | |
| Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 486 | if (SrcInst.isPHI() || DstInst.isPHI()) |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 487 | return false; |
| 488 | |
| Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 489 | if (!TII->isToBeScheduledASAP(SrcInst, DstInst) && |
| 490 | !TII->canExecuteInBundle(SrcInst, DstInst)) |
| 491 | return false; |
| 492 | |
| 493 | // The architecture doesn't allow three dependent instructions in the same |
| 494 | // packet. So, if the destination has a zero latency successor, then it's |
| 495 | // not a candidate for a zero latency predecessor. |
| 496 | if (getZeroLatency(Dst, Dst->Succs) != nullptr) |
| 497 | return false; |
| 498 | |
| Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 499 | // Check if the Dst instruction is the best candidate first. |
| 500 | SUnit *Best = nullptr; |
| 501 | SUnit *DstBest = nullptr; |
| 502 | SUnit *SrcBest = getZeroLatency(Dst, Dst->Preds); |
| 503 | if (SrcBest == nullptr || Src->NodeNum >= SrcBest->NodeNum) { |
| 504 | // Check that Src doesn't have a better candidate. |
| 505 | DstBest = getZeroLatency(Src, Src->Succs); |
| 506 | if (DstBest == nullptr || Dst->NodeNum <= DstBest->NodeNum) |
| 507 | Best = Dst; |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 508 | } |
| Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 509 | if (Best != Dst) |
| 510 | return false; |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 511 | |
| Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 512 | // The caller frequently adds the same dependence twice. If so, then |
| Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 513 | // return true for this case too. |
| Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 514 | if ((Src == SrcBest && Dst == DstBest ) || |
| 515 | (SrcBest == nullptr && Dst == DstBest) || |
| 516 | (Src == SrcBest && Dst == nullptr)) |
| Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 517 | return true; |
| 518 | |
| 519 | // Reassign the latency for the previous bests, which requires setting |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 520 | // the dependence edge in both directions. |
| Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 521 | if (SrcBest != nullptr) { |
| 522 | if (!hasV60TOps()) |
| 523 | changeLatency(SrcBest, Dst, 1); |
| 524 | else |
| 525 | restoreLatency(SrcBest, Dst); |
| 526 | } |
| 527 | if (DstBest != nullptr) { |
| 528 | if (!hasV60TOps()) |
| 529 | changeLatency(Src, DstBest, 1); |
| 530 | else |
| 531 | restoreLatency(Src, DstBest); |
| 532 | } |
| 533 | |
| 534 | // Attempt to find another opprotunity for zero latency in a different |
| 535 | // dependence. |
| Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 536 | if (SrcBest && DstBest) |
| Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 537 | // If there is an edge from SrcBest to DstBst, then try to change that |
| 538 | // to 0 now. |
| 539 | changeLatency(SrcBest, DstBest, 0); |
| 540 | else if (DstBest) { |
| 541 | // Check if the previous best destination instruction has a new zero |
| 542 | // latency dependence opportunity. |
| 543 | ExclSrc.insert(Src); |
| 544 | for (auto &I : DstBest->Preds) |
| 545 | if (ExclSrc.count(I.getSUnit()) == 0 && |
| 546 | isBestZeroLatency(I.getSUnit(), DstBest, TII, ExclSrc, ExclDst)) |
| 547 | changeLatency(I.getSUnit(), DstBest, 0); |
| 548 | } else if (SrcBest) { |
| 549 | // Check if previous best source instruction has a new zero latency |
| 550 | // dependence opportunity. |
| 551 | ExclDst.insert(Dst); |
| 552 | for (auto &I : SrcBest->Succs) |
| 553 | if (ExclDst.count(I.getSUnit()) == 0 && |
| 554 | isBestZeroLatency(SrcBest, I.getSUnit(), TII, ExclSrc, ExclDst)) |
| 555 | changeLatency(SrcBest, I.getSUnit(), 0); |
| 556 | } |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 557 | |
| Krzysztof Parzyszek | 748d3ef | 2016-07-18 14:23:10 +0000 | [diff] [blame] | 558 | return true; |
| Krzysztof Parzyszek | 408e300 | 2016-07-15 21:34:02 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| Krzysztof Parzyszek | d3d0a4b | 2016-07-22 14:22:43 +0000 | [diff] [blame] | 561 | unsigned HexagonSubtarget::getL1CacheLineSize() const { |
| 562 | return 32; |
| 563 | } |
| 564 | |
| 565 | unsigned HexagonSubtarget::getL1PrefetchDistance() const { |
| 566 | return 32; |
| 567 | } |
| 568 | |
| Krzysztof Parzyszek | 2af5037 | 2017-05-03 20:10:36 +0000 | [diff] [blame] | 569 | bool HexagonSubtarget::enableSubRegLiveness() const { |
| 570 | return EnableSubregLiveness; |
| 571 | } |