Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 1 | //===-- PPCCTRLoops.cpp - Identify and generate CTR loops -----------------===// |
| 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 pass identifies loops where we can generate the PPC branch instructions |
| 11 | // that decrement and test the count register (CTR) (bdnz and friends). |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 12 | // |
| 13 | // The pattern that defines the induction variable can changed depending on |
| 14 | // prior optimizations. For example, the IndVarSimplify phase run by 'opt' |
| 15 | // normalizes induction variables, and the Loop Strength Reduction pass |
| 16 | // run by 'llc' may also make changes to the induction variable. |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 17 | // |
| 18 | // Criteria for CTR loops: |
| 19 | // - Countable loops (w/ ind. var for a trip count) |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 20 | // - Try inner-most loops first |
| 21 | // - No nested CTR loops. |
| 22 | // - No function calls in loops. |
| 23 | // |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 26 | #include "PPC.h" |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 27 | #include "PPCSubtarget.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 28 | #include "PPCTargetMachine.h" |
Lei Huang | 0724fea | 2017-10-12 16:43:33 +0000 | [diff] [blame] | 29 | #include "PPCTargetTransformInfo.h" |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/Statistic.h" |
Lei Huang | 0724fea | 2017-10-12 16:43:33 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/AssumptionCache.h" |
Nemanja Ivanovic | 2139e99 | 2018-05-02 22:56:04 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/CFG.h" |
Lei Huang | 0724fea | 2017-10-12 16:43:33 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/CodeMetrics.h" |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/LoopInfo.h" |
Nemanja Ivanovic | 2139e99 | 2018-05-02 22:56:04 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/LoopIterator.h" |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Lei Huang | 0724fea | 2017-10-12 16:43:33 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/TargetTransformInfo.h" |
David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 40 | #include "llvm/Transforms/Utils/Local.h" |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/TargetPassConfig.h" |
Lei Huang | 0724fea | 2017-10-12 16:43:33 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/TargetSchedule.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Constants.h" |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 44 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 45 | #include "llvm/IR/Dominators.h" |
Hal Finkel | 2f474f0 | 2013-05-18 09:20:39 +0000 | [diff] [blame] | 46 | #include "llvm/IR/InlineAsm.h" |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 47 | #include "llvm/IR/Instructions.h" |
| 48 | #include "llvm/IR/IntrinsicInst.h" |
| 49 | #include "llvm/IR/Module.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 50 | #include "llvm/IR/ValueHandle.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 51 | #include "llvm/PassSupport.h" |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 52 | #include "llvm/Support/CommandLine.h" |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Debug.h" |
| 54 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 55 | #include "llvm/Transforms/Scalar.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 56 | #include "llvm/Transforms/Utils.h" |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 57 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Hal Finkel | a969df8 | 2013-05-20 20:46:30 +0000 | [diff] [blame] | 58 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 59 | |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 60 | #ifndef NDEBUG |
| 61 | #include "llvm/CodeGen/MachineDominators.h" |
| 62 | #include "llvm/CodeGen/MachineFunction.h" |
| 63 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 64 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 65 | #endif |
| 66 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 67 | using namespace llvm; |
| 68 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 69 | #define DEBUG_TYPE "ctrloops" |
| 70 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 71 | #ifndef NDEBUG |
| 72 | static cl::opt<int> CTRLoopLimit("ppc-max-ctrloop", cl::Hidden, cl::init(-1)); |
| 73 | #endif |
| 74 | |
Lei Huang | 0724fea | 2017-10-12 16:43:33 +0000 | [diff] [blame] | 75 | // The latency of mtctr is only justified if there are more than 4 |
| 76 | // comparisons that will be removed as a result. |
| 77 | static cl::opt<unsigned> |
| 78 | SmallCTRLoopThreshold("min-ctr-loop-threshold", cl::init(4), cl::Hidden, |
| 79 | cl::desc("Loops with a constant trip count smaller than " |
| 80 | "this value will not use the count register.")); |
| 81 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 82 | STATISTIC(NumCTRLoops, "Number of loops converted to CTR loops"); |
| 83 | |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 84 | namespace llvm { |
| 85 | void initializePPCCTRLoopsPass(PassRegistry&); |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 86 | #ifndef NDEBUG |
| 87 | void initializePPCCTRLoopsVerifyPass(PassRegistry&); |
| 88 | #endif |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 91 | namespace { |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 92 | struct PPCCTRLoops : public FunctionPass { |
| 93 | |
| 94 | #ifndef NDEBUG |
| 95 | static int Counter; |
| 96 | #endif |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 97 | |
| 98 | public: |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 99 | static char ID; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 100 | |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 101 | PPCCTRLoops() : FunctionPass(ID) { |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 102 | initializePPCCTRLoopsPass(*PassRegistry::getPassRegistry()); |
| 103 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 104 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 105 | bool runOnFunction(Function &F) override; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 106 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 107 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 108 | AU.addRequired<LoopInfoWrapperPass>(); |
| 109 | AU.addPreserved<LoopInfoWrapperPass>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 110 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 111 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 112 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Lei Huang | 0724fea | 2017-10-12 16:43:33 +0000 | [diff] [blame] | 113 | AU.addRequired<AssumptionCacheTracker>(); |
| 114 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | private: |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 118 | bool mightUseCTR(BasicBlock *BB); |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 119 | bool convertToCTRLoop(Loop *L); |
Hal Finkel | e6d7c28 | 2013-05-20 16:47:10 +0000 | [diff] [blame] | 120 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 121 | private: |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 122 | const PPCTargetMachine *TM; |
| 123 | const PPCSubtarget *STI; |
| 124 | const PPCTargetLowering *TLI; |
| 125 | const DataLayout *DL; |
| 126 | const TargetLibraryInfo *LibInfo; |
Lei Huang | 0724fea | 2017-10-12 16:43:33 +0000 | [diff] [blame] | 127 | const TargetTransformInfo *TTI; |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 128 | LoopInfo *LI; |
| 129 | ScalarEvolution *SE; |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 130 | DominatorTree *DT; |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 131 | bool PreserveLCSSA; |
Lei Huang | 0724fea | 2017-10-12 16:43:33 +0000 | [diff] [blame] | 132 | TargetSchedModel SchedModel; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | char PPCCTRLoops::ID = 0; |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 136 | #ifndef NDEBUG |
| 137 | int PPCCTRLoops::Counter = 0; |
| 138 | #endif |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 139 | |
| 140 | #ifndef NDEBUG |
| 141 | struct PPCCTRLoopsVerify : public MachineFunctionPass { |
| 142 | public: |
| 143 | static char ID; |
| 144 | |
| 145 | PPCCTRLoopsVerify() : MachineFunctionPass(ID) { |
| 146 | initializePPCCTRLoopsVerifyPass(*PassRegistry::getPassRegistry()); |
| 147 | } |
| 148 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 149 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 150 | AU.addRequired<MachineDominatorTree>(); |
| 151 | MachineFunctionPass::getAnalysisUsage(AU); |
| 152 | } |
| 153 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 154 | bool runOnMachineFunction(MachineFunction &MF) override; |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 155 | |
| 156 | private: |
| 157 | MachineDominatorTree *MDT; |
| 158 | }; |
| 159 | |
| 160 | char PPCCTRLoopsVerify::ID = 0; |
| 161 | #endif // NDEBUG |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 162 | } // end anonymous namespace |
| 163 | |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 164 | INITIALIZE_PASS_BEGIN(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops", |
| 165 | false, false) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 166 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 167 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 168 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 169 | INITIALIZE_PASS_END(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops", |
| 170 | false, false) |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 171 | |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 172 | FunctionPass *llvm::createPPCCTRLoops() { return new PPCCTRLoops(); } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 173 | |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 174 | #ifndef NDEBUG |
| 175 | INITIALIZE_PASS_BEGIN(PPCCTRLoopsVerify, "ppc-ctr-loops-verify", |
| 176 | "PowerPC CTR Loops Verify", false, false) |
| 177 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 178 | INITIALIZE_PASS_END(PPCCTRLoopsVerify, "ppc-ctr-loops-verify", |
| 179 | "PowerPC CTR Loops Verify", false, false) |
| 180 | |
| 181 | FunctionPass *llvm::createPPCCTRLoopsVerify() { |
| 182 | return new PPCCTRLoopsVerify(); |
| 183 | } |
| 184 | #endif // NDEBUG |
| 185 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 186 | bool PPCCTRLoops::runOnFunction(Function &F) { |
Andrew Kaylor | 289bd5f | 2016-04-27 19:39:32 +0000 | [diff] [blame] | 187 | if (skipFunction(F)) |
| 188 | return false; |
| 189 | |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 190 | auto *TPC = getAnalysisIfAvailable<TargetPassConfig>(); |
| 191 | if (!TPC) |
| 192 | return false; |
| 193 | |
| 194 | TM = &TPC->getTM<PPCTargetMachine>(); |
| 195 | STI = TM->getSubtargetImpl(F); |
| 196 | TLI = STI->getTargetLowering(); |
| 197 | |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 198 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 199 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 200 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Lei Huang | 0724fea | 2017-10-12 16:43:33 +0000 | [diff] [blame] | 201 | TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 202 | DL = &F.getParent()->getDataLayout(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 203 | auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>(); |
| 204 | LibInfo = TLIP ? &TLIP->getTLI() : nullptr; |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 205 | PreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 206 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 207 | bool MadeChange = false; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 208 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 209 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 210 | I != E; ++I) { |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 211 | Loop *L = *I; |
| 212 | if (!L->getParentLoop()) |
| 213 | MadeChange |= convertToCTRLoop(L); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 216 | return MadeChange; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Hal Finkel | 2230404 | 2014-02-25 20:51:50 +0000 | [diff] [blame] | 219 | static bool isLargeIntegerTy(bool Is32Bit, Type *Ty) { |
| 220 | if (IntegerType *ITy = dyn_cast<IntegerType>(Ty)) |
Aaron Ballman | 3d69c5c | 2014-02-26 20:22:20 +0000 | [diff] [blame] | 221 | return ITy->getBitWidth() > (Is32Bit ? 32U : 64U); |
Hal Finkel | 2230404 | 2014-02-25 20:51:50 +0000 | [diff] [blame] | 222 | |
| 223 | return false; |
| 224 | } |
| 225 | |
David Majnemer | d0bcef2 | 2014-12-27 19:45:38 +0000 | [diff] [blame] | 226 | // Determining the address of a TLS variable results in a function call in |
| 227 | // certain TLS models. |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 228 | static bool memAddrUsesCTR(const PPCTargetMachine &TM, const Value *MemAddr) { |
David Majnemer | d0bcef2 | 2014-12-27 19:45:38 +0000 | [diff] [blame] | 229 | const auto *GV = dyn_cast<GlobalValue>(MemAddr); |
Hal Finkel | 7d0e34e | 2015-10-28 23:43:00 +0000 | [diff] [blame] | 230 | if (!GV) { |
| 231 | // Recurse to check for constants that refer to TLS global variables. |
| 232 | if (const auto *CV = dyn_cast<Constant>(MemAddr)) |
| 233 | for (const auto &CO : CV->operands()) |
| 234 | if (memAddrUsesCTR(TM, CO)) |
| 235 | return true; |
| 236 | |
David Majnemer | d0bcef2 | 2014-12-27 19:45:38 +0000 | [diff] [blame] | 237 | return false; |
Hal Finkel | 7d0e34e | 2015-10-28 23:43:00 +0000 | [diff] [blame] | 238 | } |
| 239 | |
David Majnemer | d0bcef2 | 2014-12-27 19:45:38 +0000 | [diff] [blame] | 240 | if (!GV->isThreadLocal()) |
| 241 | return false; |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 242 | TLSModel::Model Model = TM.getTLSModel(GV); |
David Majnemer | d0bcef2 | 2014-12-27 19:45:38 +0000 | [diff] [blame] | 243 | return Model == TLSModel::GeneralDynamic || Model == TLSModel::LocalDynamic; |
| 244 | } |
| 245 | |
Eric Christopher | 56f481b | 2017-06-29 23:28:47 +0000 | [diff] [blame] | 246 | // Loop through the inline asm constraints and look for something that clobbers |
| 247 | // ctr. |
| 248 | static bool asmClobbersCTR(InlineAsm *IA) { |
| 249 | InlineAsm::ConstraintInfoVector CIV = IA->ParseConstraints(); |
| 250 | for (unsigned i = 0, ie = CIV.size(); i < ie; ++i) { |
| 251 | InlineAsm::ConstraintInfo &C = CIV[i]; |
| 252 | if (C.Type != InlineAsm::isInput) |
| 253 | for (unsigned j = 0, je = C.Codes.size(); j < je; ++j) |
| 254 | if (StringRef(C.Codes[j]).equals_lower("{ctr}")) |
| 255 | return true; |
| 256 | } |
| 257 | return false; |
| 258 | } |
| 259 | |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 260 | bool PPCCTRLoops::mightUseCTR(BasicBlock *BB) { |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 261 | for (BasicBlock::iterator J = BB->begin(), JE = BB->end(); |
| 262 | J != JE; ++J) { |
| 263 | if (CallInst *CI = dyn_cast<CallInst>(J)) { |
Eric Christopher | 56f481b | 2017-06-29 23:28:47 +0000 | [diff] [blame] | 264 | // Inline ASM is okay, unless it clobbers the ctr register. |
Hal Finkel | 2f474f0 | 2013-05-18 09:20:39 +0000 | [diff] [blame] | 265 | if (InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue())) { |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 266 | if (asmClobbersCTR(IA)) |
| 267 | return true; |
Hal Finkel | 2f474f0 | 2013-05-18 09:20:39 +0000 | [diff] [blame] | 268 | continue; |
| 269 | } |
| 270 | |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 271 | if (Function *F = CI->getCalledFunction()) { |
| 272 | // Most intrinsics don't become function calls, but some might. |
| 273 | // sin, cos, exp and log are always calls. |
Hal Finkel | 0b37175 | 2016-03-27 05:40:56 +0000 | [diff] [blame] | 274 | unsigned Opcode = 0; |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 275 | if (F->getIntrinsicID() != Intrinsic::not_intrinsic) { |
| 276 | switch (F->getIntrinsicID()) { |
| 277 | default: continue; |
Eric Christopher | 71f6e2f | 2015-09-08 22:14:58 +0000 | [diff] [blame] | 278 | // If we have a call to ppc_is_decremented_ctr_nonzero, or ppc_mtctr |
| 279 | // we're definitely using CTR. |
| 280 | case Intrinsic::ppc_is_decremented_ctr_nonzero: |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 281 | case Intrinsic::ppc_mtctr: |
| 282 | return true; |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 283 | |
| 284 | // VisualStudio defines setjmp as _setjmp |
| 285 | #if defined(_MSC_VER) && defined(setjmp) && \ |
| 286 | !defined(setjmp_undefined_for_msvc) |
| 287 | # pragma push_macro("setjmp") |
| 288 | # undef setjmp |
| 289 | # define setjmp_undefined_for_msvc |
| 290 | #endif |
| 291 | |
| 292 | case Intrinsic::setjmp: |
| 293 | |
| 294 | #if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc) |
| 295 | // let's return it to _setjmp state |
| 296 | # pragma pop_macro("setjmp") |
| 297 | # undef setjmp_undefined_for_msvc |
| 298 | #endif |
| 299 | |
| 300 | case Intrinsic::longjmp: |
Hal Finkel | 40f76d5 | 2013-07-17 05:35:44 +0000 | [diff] [blame] | 301 | |
| 302 | // Exclude eh_sjlj_setjmp; we don't need to exclude eh_sjlj_longjmp |
| 303 | // because, although it does clobber the counter register, the |
| 304 | // control can't then return to inside the loop unless there is also |
| 305 | // an eh_sjlj_setjmp. |
| 306 | case Intrinsic::eh_sjlj_setjmp: |
| 307 | |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 308 | case Intrinsic::memcpy: |
| 309 | case Intrinsic::memmove: |
| 310 | case Intrinsic::memset: |
| 311 | case Intrinsic::powi: |
| 312 | case Intrinsic::log: |
| 313 | case Intrinsic::log2: |
| 314 | case Intrinsic::log10: |
| 315 | case Intrinsic::exp: |
| 316 | case Intrinsic::exp2: |
| 317 | case Intrinsic::pow: |
| 318 | case Intrinsic::sin: |
| 319 | case Intrinsic::cos: |
| 320 | return true; |
Hal Finkel | 0c5c01aa | 2013-08-19 23:35:46 +0000 | [diff] [blame] | 321 | case Intrinsic::copysign: |
| 322 | if (CI->getArgOperand(0)->getType()->getScalarType()-> |
| 323 | isPPC_FP128Ty()) |
| 324 | return true; |
| 325 | else |
| 326 | continue; // ISD::FCOPYSIGN is never a library call. |
Hal Finkel | cef9e52 | 2017-04-11 02:03:17 +0000 | [diff] [blame] | 327 | case Intrinsic::sqrt: Opcode = ISD::FSQRT; break; |
| 328 | case Intrinsic::floor: Opcode = ISD::FFLOOR; break; |
| 329 | case Intrinsic::ceil: Opcode = ISD::FCEIL; break; |
| 330 | case Intrinsic::trunc: Opcode = ISD::FTRUNC; break; |
| 331 | case Intrinsic::rint: Opcode = ISD::FRINT; break; |
| 332 | case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break; |
| 333 | case Intrinsic::round: Opcode = ISD::FROUND; break; |
| 334 | case Intrinsic::minnum: Opcode = ISD::FMINNUM; break; |
| 335 | case Intrinsic::maxnum: Opcode = ISD::FMAXNUM; break; |
| 336 | case Intrinsic::umul_with_overflow: Opcode = ISD::UMULO; break; |
| 337 | case Intrinsic::smul_with_overflow: Opcode = ISD::SMULO; break; |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | // PowerPC does not use [US]DIVREM or other library calls for |
| 342 | // operations on regular types which are not otherwise library calls |
| 343 | // (i.e. soft float or atomics). If adapting for targets that do, |
| 344 | // additional care is required here. |
| 345 | |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 346 | LibFunc Func; |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 347 | if (!F->hasLocalLinkage() && F->hasName() && LibInfo && |
| 348 | LibInfo->getLibFunc(F->getName(), Func) && |
| 349 | LibInfo->hasOptimizedCodeGen(Func)) { |
| 350 | // Non-read-only functions are never treated as intrinsics. |
| 351 | if (!CI->onlyReadsMemory()) |
| 352 | return true; |
| 353 | |
| 354 | // Conversion happens only for FP calls. |
| 355 | if (!CI->getArgOperand(0)->getType()->isFloatingPointTy()) |
| 356 | return true; |
| 357 | |
| 358 | switch (Func) { |
| 359 | default: return true; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 360 | case LibFunc_copysign: |
| 361 | case LibFunc_copysignf: |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 362 | continue; // ISD::FCOPYSIGN is never a library call. |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 363 | case LibFunc_copysignl: |
Hal Finkel | 1cf48ab | 2013-08-19 23:35:24 +0000 | [diff] [blame] | 364 | return true; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 365 | case LibFunc_fabs: |
| 366 | case LibFunc_fabsf: |
| 367 | case LibFunc_fabsl: |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 368 | continue; // ISD::FABS is never a library call. |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 369 | case LibFunc_sqrt: |
| 370 | case LibFunc_sqrtf: |
| 371 | case LibFunc_sqrtl: |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 372 | Opcode = ISD::FSQRT; break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 373 | case LibFunc_floor: |
| 374 | case LibFunc_floorf: |
| 375 | case LibFunc_floorl: |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 376 | Opcode = ISD::FFLOOR; break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 377 | case LibFunc_nearbyint: |
| 378 | case LibFunc_nearbyintf: |
| 379 | case LibFunc_nearbyintl: |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 380 | Opcode = ISD::FNEARBYINT; break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 381 | case LibFunc_ceil: |
| 382 | case LibFunc_ceilf: |
| 383 | case LibFunc_ceill: |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 384 | Opcode = ISD::FCEIL; break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 385 | case LibFunc_rint: |
| 386 | case LibFunc_rintf: |
| 387 | case LibFunc_rintl: |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 388 | Opcode = ISD::FRINT; break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 389 | case LibFunc_round: |
| 390 | case LibFunc_roundf: |
| 391 | case LibFunc_roundl: |
Hal Finkel | 171817e | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 392 | Opcode = ISD::FROUND; break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 393 | case LibFunc_trunc: |
| 394 | case LibFunc_truncf: |
| 395 | case LibFunc_truncl: |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 396 | Opcode = ISD::FTRUNC; break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 397 | case LibFunc_fmin: |
| 398 | case LibFunc_fminf: |
| 399 | case LibFunc_fminl: |
Hal Finkel | 0b37175 | 2016-03-27 05:40:56 +0000 | [diff] [blame] | 400 | Opcode = ISD::FMINNUM; break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 401 | case LibFunc_fmax: |
| 402 | case LibFunc_fmaxf: |
| 403 | case LibFunc_fmaxl: |
Hal Finkel | 0b37175 | 2016-03-27 05:40:56 +0000 | [diff] [blame] | 404 | Opcode = ISD::FMAXNUM; break; |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 405 | } |
Hal Finkel | 0b37175 | 2016-03-27 05:40:56 +0000 | [diff] [blame] | 406 | } |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 407 | |
Hal Finkel | 0b37175 | 2016-03-27 05:40:56 +0000 | [diff] [blame] | 408 | if (Opcode) { |
Sean Fertile | 33a1776 | 2018-01-09 03:03:41 +0000 | [diff] [blame] | 409 | EVT EVTy = |
| 410 | TLI->getValueType(*DL, CI->getArgOperand(0)->getType(), true); |
| 411 | |
| 412 | if (EVTy == MVT::Other) |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 413 | return true; |
NAKAMURA Takumi | a9cb538 | 2015-09-22 11:14:39 +0000 | [diff] [blame] | 414 | |
Sean Fertile | 33a1776 | 2018-01-09 03:03:41 +0000 | [diff] [blame] | 415 | if (TLI->isOperationLegalOrCustom(Opcode, EVTy)) |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 416 | continue; |
Sean Fertile | 33a1776 | 2018-01-09 03:03:41 +0000 | [diff] [blame] | 417 | else if (EVTy.isVector() && |
| 418 | TLI->isOperationLegalOrCustom(Opcode, EVTy.getScalarType())) |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 419 | continue; |
| 420 | |
| 421 | return true; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return true; |
| 426 | } else if (isa<BinaryOperator>(J) && |
| 427 | J->getType()->getScalarType()->isPPC_FP128Ty()) { |
| 428 | // Most operations on ppc_f128 values become calls. |
| 429 | return true; |
| 430 | } else if (isa<UIToFPInst>(J) || isa<SIToFPInst>(J) || |
| 431 | isa<FPToUIInst>(J) || isa<FPToSIInst>(J)) { |
| 432 | CastInst *CI = cast<CastInst>(J); |
| 433 | if (CI->getSrcTy()->getScalarType()->isPPC_FP128Ty() || |
| 434 | CI->getDestTy()->getScalarType()->isPPC_FP128Ty() || |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 435 | isLargeIntegerTy(!TM->isPPC64(), CI->getSrcTy()->getScalarType()) || |
| 436 | isLargeIntegerTy(!TM->isPPC64(), CI->getDestTy()->getScalarType())) |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 437 | return true; |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 438 | } else if (isLargeIntegerTy(!TM->isPPC64(), |
Hal Finkel | 2230404 | 2014-02-25 20:51:50 +0000 | [diff] [blame] | 439 | J->getType()->getScalarType()) && |
Hal Finkel | fa5f6f7 | 2013-06-07 22:16:19 +0000 | [diff] [blame] | 440 | (J->getOpcode() == Instruction::UDiv || |
| 441 | J->getOpcode() == Instruction::SDiv || |
| 442 | J->getOpcode() == Instruction::URem || |
| 443 | J->getOpcode() == Instruction::SRem)) { |
| 444 | return true; |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 445 | } else if (!TM->isPPC64() && |
Hal Finkel | c4c6c87 | 2014-05-11 16:23:29 +0000 | [diff] [blame] | 446 | isLargeIntegerTy(false, J->getType()->getScalarType()) && |
| 447 | (J->getOpcode() == Instruction::Shl || |
| 448 | J->getOpcode() == Instruction::AShr || |
| 449 | J->getOpcode() == Instruction::LShr)) { |
| 450 | // Only on PPC32, for 128-bit integers (specifically not 64-bit |
| 451 | // integers), these might be runtime calls. |
| 452 | return true; |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 453 | } else if (isa<IndirectBrInst>(J) || isa<InvokeInst>(J)) { |
| 454 | // On PowerPC, indirect jumps use the counter register. |
| 455 | return true; |
| 456 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(J)) { |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 457 | if (SI->getNumCases() + 1 >= (unsigned)TLI->getMinimumJumpTableEntries()) |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 458 | return true; |
| 459 | } |
Petar Jovanovic | 0b44f24 | 2016-03-17 17:11:33 +0000 | [diff] [blame] | 460 | |
Nemanja Ivanovic | e54a9ee | 2018-02-22 03:02:41 +0000 | [diff] [blame] | 461 | // FREM is always a call. |
| 462 | if (J->getOpcode() == Instruction::FRem) |
| 463 | return true; |
| 464 | |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 465 | if (STI->useSoftFloat()) { |
Petar Jovanovic | 0b44f24 | 2016-03-17 17:11:33 +0000 | [diff] [blame] | 466 | switch(J->getOpcode()) { |
| 467 | case Instruction::FAdd: |
| 468 | case Instruction::FSub: |
| 469 | case Instruction::FMul: |
| 470 | case Instruction::FDiv: |
Petar Jovanovic | 0b44f24 | 2016-03-17 17:11:33 +0000 | [diff] [blame] | 471 | case Instruction::FPTrunc: |
| 472 | case Instruction::FPExt: |
| 473 | case Instruction::FPToUI: |
| 474 | case Instruction::FPToSI: |
| 475 | case Instruction::UIToFP: |
| 476 | case Instruction::SIToFP: |
| 477 | case Instruction::FCmp: |
| 478 | return true; |
| 479 | } |
| 480 | } |
| 481 | |
David Majnemer | d0bcef2 | 2014-12-27 19:45:38 +0000 | [diff] [blame] | 482 | for (Value *Operand : J->operands()) |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 483 | if (memAddrUsesCTR(*TM, Operand)) |
David Majnemer | d0bcef2 | 2014-12-27 19:45:38 +0000 | [diff] [blame] | 484 | return true; |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | return false; |
| 488 | } |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 489 | bool PPCCTRLoops::convertToCTRLoop(Loop *L) { |
| 490 | bool MadeChange = false; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 491 | |
Lei Huang | 0724fea | 2017-10-12 16:43:33 +0000 | [diff] [blame] | 492 | // Do not convert small short loops to CTR loop. |
| 493 | unsigned ConstTripCount = SE->getSmallConstantTripCount(L); |
| 494 | if (ConstTripCount && ConstTripCount < SmallCTRLoopThreshold) { |
| 495 | SmallPtrSet<const Value *, 32> EphValues; |
| 496 | auto AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache( |
| 497 | *L->getHeader()->getParent()); |
| 498 | CodeMetrics::collectEphemeralValues(L, &AC, EphValues); |
| 499 | CodeMetrics Metrics; |
| 500 | for (BasicBlock *BB : L->blocks()) |
| 501 | Metrics.analyzeBasicBlock(BB, *TTI, EphValues); |
| 502 | // 6 is an approximate latency for the mtctr instruction. |
| 503 | if (Metrics.NumInsts <= (6 * SchedModel.getIssueWidth())) |
| 504 | return false; |
| 505 | } |
| 506 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 507 | // Process nested loops first. |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 508 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) { |
| 509 | MadeChange |= convertToCTRLoop(*I); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 510 | LLVM_DEBUG(dbgs() << "Nested loop converted\n"); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 511 | } |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 512 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 513 | // If a nested loop has been converted, then we can't convert this loop. |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 514 | if (MadeChange) |
| 515 | return MadeChange; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 516 | |
Nemanja Ivanovic | 2139e99 | 2018-05-02 22:56:04 +0000 | [diff] [blame] | 517 | // Bail out if the loop has irreducible control flow. |
| 518 | LoopBlocksRPO RPOT(L); |
| 519 | RPOT.perform(LI); |
| 520 | if (containsIrreducibleCFG<const BasicBlock *>(RPOT, *LI)) |
| 521 | return false; |
| 522 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 523 | #ifndef NDEBUG |
| 524 | // Stop trying after reaching the limit (if any). |
| 525 | int Limit = CTRLoopLimit; |
| 526 | if (Limit >= 0) { |
| 527 | if (Counter >= CTRLoopLimit) |
Hal Finkel | 21f2a43 | 2013-03-18 17:40:44 +0000 | [diff] [blame] | 528 | return false; |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 529 | Counter++; |
Hal Finkel | 21f2a43 | 2013-03-18 17:40:44 +0000 | [diff] [blame] | 530 | } |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 531 | #endif |
Hal Finkel | 21f2a43 | 2013-03-18 17:40:44 +0000 | [diff] [blame] | 532 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 533 | // We don't want to spill/restore the counter register, and so we don't |
| 534 | // want to use the counter register if the loop contains calls. |
| 535 | for (Loop::block_iterator I = L->block_begin(), IE = L->block_end(); |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 536 | I != IE; ++I) |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 537 | if (mightUseCTR(*I)) |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 538 | return MadeChange; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 539 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 540 | SmallVector<BasicBlock*, 4> ExitingBlocks; |
| 541 | L->getExitingBlocks(ExitingBlocks); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 542 | |
Hiroshi Inoue | c5ab1ab | 2018-02-05 12:25:29 +0000 | [diff] [blame] | 543 | // If there is an exit edge known to be frequently taken, |
| 544 | // we should not transform this loop. |
| 545 | for (auto &BB : ExitingBlocks) { |
| 546 | Instruction *TI = BB->getTerminator(); |
| 547 | if (!TI) continue; |
| 548 | |
| 549 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
Hiroshi Inoue | ad48d2fe | 2018-02-06 11:34:16 +0000 | [diff] [blame] | 550 | uint64_t TrueWeight = 0, FalseWeight = 0; |
Hiroshi Inoue | c5ab1ab | 2018-02-05 12:25:29 +0000 | [diff] [blame] | 551 | if (!BI->isConditional() || |
| 552 | !BI->extractProfMetadata(TrueWeight, FalseWeight)) |
| 553 | continue; |
| 554 | |
| 555 | // If the exit path is more frequent than the loop path, |
| 556 | // we return here without further analysis for this loop. |
| 557 | bool TrueIsExit = !L->contains(BI->getSuccessor(0)); |
| 558 | if (( TrueIsExit && FalseWeight < TrueWeight) || |
| 559 | (!TrueIsExit && FalseWeight > TrueWeight)) |
| 560 | return MadeChange; |
| 561 | } |
| 562 | } |
| 563 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 564 | BasicBlock *CountedExitBlock = nullptr; |
| 565 | const SCEV *ExitCount = nullptr; |
| 566 | BranchInst *CountedExitBranch = nullptr; |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 567 | for (SmallVectorImpl<BasicBlock *>::iterator I = ExitingBlocks.begin(), |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 568 | IE = ExitingBlocks.end(); I != IE; ++I) { |
| 569 | const SCEV *EC = SE->getExitCount(L, *I); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 570 | LLVM_DEBUG(dbgs() << "Exit Count for " << *L << " from block " |
| 571 | << (*I)->getName() << ": " << *EC << "\n"); |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 572 | if (isa<SCEVCouldNotCompute>(EC)) |
| 573 | continue; |
| 574 | if (const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) { |
| 575 | if (ConstEC->getValue()->isZero()) |
| 576 | continue; |
| 577 | } else if (!SE->isLoopInvariant(EC, L)) |
| 578 | continue; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 579 | |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 580 | if (SE->getTypeSizeInBits(EC->getType()) > (TM->isPPC64() ? 64 : 32)) |
Hal Finkel | 25e4a0d | 2013-07-01 19:34:59 +0000 | [diff] [blame] | 581 | continue; |
| 582 | |
Nemanja Ivanovic | 2139e99 | 2018-05-02 22:56:04 +0000 | [diff] [blame] | 583 | // If this exiting block is contained in a nested loop, it is not eligible |
| 584 | // for insertion of the branch-and-decrement since the inner loop would |
| 585 | // end up messing up the value in the CTR. |
| 586 | if (LI->getLoopFor(*I) != L) |
| 587 | continue; |
| 588 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 589 | // We now have a loop-invariant count of loop iterations (which is not the |
| 590 | // constant zero) for which we know that this loop will not exit via this |
Hiroshi Inoue | 0f7f59f | 2018-06-13 08:54:13 +0000 | [diff] [blame^] | 591 | // existing block. |
Hal Finkel | 6261c2d | 2012-06-16 20:34:07 +0000 | [diff] [blame] | 592 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 593 | // We need to make sure that this block will run on every loop iteration. |
| 594 | // For this to be true, we must dominate all blocks with backedges. Such |
| 595 | // blocks are in-loop predecessors to the header block. |
| 596 | bool NotAlways = false; |
| 597 | for (pred_iterator PI = pred_begin(L->getHeader()), |
| 598 | PIE = pred_end(L->getHeader()); PI != PIE; ++PI) { |
| 599 | if (!L->contains(*PI)) |
| 600 | continue; |
| 601 | |
| 602 | if (!DT->dominates(*I, *PI)) { |
| 603 | NotAlways = true; |
| 604 | break; |
| 605 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 606 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 607 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 608 | if (NotAlways) |
| 609 | continue; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 610 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 611 | // Make sure this blocks ends with a conditional branch. |
| 612 | Instruction *TI = (*I)->getTerminator(); |
| 613 | if (!TI) |
| 614 | continue; |
| 615 | |
| 616 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 617 | if (!BI->isConditional()) |
| 618 | continue; |
| 619 | |
| 620 | CountedExitBranch = BI; |
| 621 | } else |
| 622 | continue; |
| 623 | |
| 624 | // Note that this block may not be the loop latch block, even if the loop |
| 625 | // has a latch block. |
| 626 | CountedExitBlock = *I; |
| 627 | ExitCount = EC; |
| 628 | break; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 631 | if (!CountedExitBlock) |
| 632 | return MadeChange; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 633 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 634 | BasicBlock *Preheader = L->getLoopPreheader(); |
Hal Finkel | 5f587c5 | 2013-05-16 19:58:38 +0000 | [diff] [blame] | 635 | |
| 636 | // If we don't have a preheader, then insert one. If we already have a |
| 637 | // preheader, then we can use it (except if the preheader contains a use of |
| 638 | // the CTR register because some such uses might be reordered by the |
| 639 | // selection DAG after the mtctr instruction). |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 640 | if (!Preheader || mightUseCTR(Preheader)) |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 641 | Preheader = InsertPreheaderForLoop(L, DT, LI, PreserveLCSSA); |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 642 | if (!Preheader) |
| 643 | return MadeChange; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 644 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 645 | LLVM_DEBUG(dbgs() << "Preheader for exit count: " << Preheader->getName() |
| 646 | << "\n"); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 647 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 648 | // Insert the count into the preheader and replace the condition used by the |
| 649 | // selected branch. |
| 650 | MadeChange = true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 651 | |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 652 | SCEVExpander SCEVE(*SE, *DL, "loopcnt"); |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 653 | LLVMContext &C = SE->getContext(); |
Eric Christopher | b16eacf | 2017-06-29 23:28:45 +0000 | [diff] [blame] | 654 | Type *CountType = TM->isPPC64() ? Type::getInt64Ty(C) : Type::getInt32Ty(C); |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 655 | if (!ExitCount->getType()->isPointerTy() && |
| 656 | ExitCount->getType() != CountType) |
| 657 | ExitCount = SE->getZeroExtendExpr(ExitCount, CountType); |
Sanjoy Das | 2aacc0e | 2015-09-23 01:59:04 +0000 | [diff] [blame] | 658 | ExitCount = SE->getAddExpr(ExitCount, SE->getOne(CountType)); |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 659 | Value *ECValue = |
| 660 | SCEVE.expandCodeFor(ExitCount, CountType, Preheader->getTerminator()); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 661 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 662 | IRBuilder<> CountBuilder(Preheader->getTerminator()); |
| 663 | Module *M = Preheader->getParent()->getParent(); |
| 664 | Value *MTCTRFunc = Intrinsic::getDeclaration(M, Intrinsic::ppc_mtctr, |
| 665 | CountType); |
| 666 | CountBuilder.CreateCall(MTCTRFunc, ECValue); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 667 | |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 668 | IRBuilder<> CondBuilder(CountedExitBranch); |
| 669 | Value *DecFunc = |
| 670 | Intrinsic::getDeclaration(M, Intrinsic::ppc_is_decremented_ctr_nonzero); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 671 | Value *NewCond = CondBuilder.CreateCall(DecFunc, {}); |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 672 | Value *OldCond = CountedExitBranch->getCondition(); |
| 673 | CountedExitBranch->setCondition(NewCond); |
| 674 | |
| 675 | // The false branch must exit the loop. |
| 676 | if (!L->contains(CountedExitBranch->getSuccessor(0))) |
| 677 | CountedExitBranch->swapSuccessors(); |
| 678 | |
| 679 | // The old condition may be dead now, and may have even created a dead PHI |
| 680 | // (the original induction variable). |
| 681 | RecursivelyDeleteTriviallyDeadInstructions(OldCond); |
Sean Fertile | d44cb18 | 2017-07-05 17:57:57 +0000 | [diff] [blame] | 682 | // Run through the basic blocks of the loop and see if any of them have dead |
| 683 | // PHIs that can be removed. |
| 684 | for (auto I : L->blocks()) |
| 685 | DeleteDeadPHIs(I); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 686 | |
| 687 | ++NumCTRLoops; |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 688 | return MadeChange; |
| 689 | } |
| 690 | |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 691 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 692 | static bool clobbersCTR(const MachineInstr &MI) { |
| 693 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 694 | const MachineOperand &MO = MI.getOperand(i); |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 695 | if (MO.isReg()) { |
| 696 | if (MO.isDef() && (MO.getReg() == PPC::CTR || MO.getReg() == PPC::CTR8)) |
| 697 | return true; |
| 698 | } else if (MO.isRegMask()) { |
| 699 | if (MO.clobbersPhysReg(PPC::CTR) || MO.clobbersPhysReg(PPC::CTR8)) |
| 700 | return true; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | static bool verifyCTRBranch(MachineBasicBlock *MBB, |
| 708 | MachineBasicBlock::iterator I) { |
| 709 | MachineBasicBlock::iterator BI = I; |
| 710 | SmallSet<MachineBasicBlock *, 16> Visited; |
| 711 | SmallVector<MachineBasicBlock *, 8> Preds; |
| 712 | bool CheckPreds; |
| 713 | |
| 714 | if (I == MBB->begin()) { |
| 715 | Visited.insert(MBB); |
| 716 | goto queue_preds; |
| 717 | } else |
| 718 | --I; |
| 719 | |
| 720 | check_block: |
| 721 | Visited.insert(MBB); |
| 722 | if (I == MBB->end()) |
| 723 | goto queue_preds; |
| 724 | |
| 725 | CheckPreds = true; |
| 726 | for (MachineBasicBlock::iterator IE = MBB->begin();; --I) { |
| 727 | unsigned Opc = I->getOpcode(); |
Hal Finkel | 0859ef2 | 2013-05-20 16:08:37 +0000 | [diff] [blame] | 728 | if (Opc == PPC::MTCTRloop || Opc == PPC::MTCTR8loop) { |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 729 | CheckPreds = false; |
| 730 | break; |
| 731 | } |
| 732 | |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 733 | if (I != BI && clobbersCTR(*I)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 734 | LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " (" << MBB->getFullName() |
| 735 | << ") instruction " << *I |
| 736 | << " clobbers CTR, invalidating " |
| 737 | << printMBBReference(*BI->getParent()) << " (" |
| 738 | << BI->getParent()->getFullName() << ") instruction " |
| 739 | << *BI << "\n"); |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 740 | return false; |
| 741 | } |
| 742 | |
| 743 | if (I == IE) |
| 744 | break; |
| 745 | } |
| 746 | |
| 747 | if (!CheckPreds && Preds.empty()) |
| 748 | return true; |
| 749 | |
| 750 | if (CheckPreds) { |
| 751 | queue_preds: |
| 752 | if (MachineFunction::iterator(MBB) == MBB->getParent()->begin()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 753 | LLVM_DEBUG(dbgs() << "Unable to find a MTCTR instruction for " |
| 754 | << printMBBReference(*BI->getParent()) << " (" |
| 755 | << BI->getParent()->getFullName() << ") instruction " |
| 756 | << *BI << "\n"); |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 757 | return false; |
| 758 | } |
| 759 | |
| 760 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 761 | PIE = MBB->pred_end(); PI != PIE; ++PI) |
| 762 | Preds.push_back(*PI); |
| 763 | } |
| 764 | |
| 765 | do { |
| 766 | MBB = Preds.pop_back_val(); |
| 767 | if (!Visited.count(MBB)) { |
| 768 | I = MBB->getLastNonDebugInstr(); |
| 769 | goto check_block; |
| 770 | } |
| 771 | } while (!Preds.empty()); |
| 772 | |
| 773 | return true; |
| 774 | } |
| 775 | |
| 776 | bool PPCCTRLoopsVerify::runOnMachineFunction(MachineFunction &MF) { |
| 777 | MDT = &getAnalysis<MachineDominatorTree>(); |
| 778 | |
| 779 | // Verify that all bdnz/bdz instructions are dominated by a loop mtctr before |
| 780 | // any other instructions that might clobber the ctr register. |
| 781 | for (MachineFunction::iterator I = MF.begin(), IE = MF.end(); |
| 782 | I != IE; ++I) { |
Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 783 | MachineBasicBlock *MBB = &*I; |
Hal Finkel | 8ca3884 | 2013-05-20 16:08:17 +0000 | [diff] [blame] | 784 | if (!MDT->isReachableFromEntry(MBB)) |
| 785 | continue; |
| 786 | |
| 787 | for (MachineBasicBlock::iterator MII = MBB->getFirstTerminator(), |
| 788 | MIIE = MBB->end(); MII != MIIE; ++MII) { |
| 789 | unsigned Opc = MII->getOpcode(); |
| 790 | if (Opc == PPC::BDNZ8 || Opc == PPC::BDNZ || |
| 791 | Opc == PPC::BDZ8 || Opc == PPC::BDZ) |
| 792 | if (!verifyCTRBranch(MBB, MII)) |
| 793 | llvm_unreachable("Invalid PPC CTR loop!"); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | return false; |
| 798 | } |
| 799 | #endif // NDEBUG |