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