1*9880d681SAndroid Build Coastguard Worker //===-- PPCCTRLoops.cpp - Identify and generate CTR loops -----------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass identifies loops where we can generate the PPC branch instructions
11*9880d681SAndroid Build Coastguard Worker // that decrement and test the count register (CTR) (bdnz and friends).
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker // The pattern that defines the induction variable can changed depending on
14*9880d681SAndroid Build Coastguard Worker // prior optimizations. For example, the IndVarSimplify phase run by 'opt'
15*9880d681SAndroid Build Coastguard Worker // normalizes induction variables, and the Loop Strength Reduction pass
16*9880d681SAndroid Build Coastguard Worker // run by 'llc' may also make changes to the induction variable.
17*9880d681SAndroid Build Coastguard Worker //
18*9880d681SAndroid Build Coastguard Worker // Criteria for CTR loops:
19*9880d681SAndroid Build Coastguard Worker // - Countable loops (w/ ind. var for a trip count)
20*9880d681SAndroid Build Coastguard Worker // - Try inner-most loops first
21*9880d681SAndroid Build Coastguard Worker // - No nested CTR loops.
22*9880d681SAndroid Build Coastguard Worker // - No function calls in loops.
23*9880d681SAndroid Build Coastguard Worker //
24*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
27*9880d681SAndroid Build Coastguard Worker #include "PPC.h"
28*9880d681SAndroid Build Coastguard Worker #include "PPCTargetMachine.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LoopInfo.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScalarEvolutionExpander.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InlineAsm.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
40*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
41*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/ValueHandle.h"
42*9880d681SAndroid Build Coastguard Worker #include "llvm/PassSupport.h"
43*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
44*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
45*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
46*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/BasicBlockUtils.h"
47*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/Local.h"
48*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/LoopUtils.h"
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
51*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
52*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
53*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
54*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
55*9880d681SAndroid Build Coastguard Worker #endif
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard Worker using namespace llvm;
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "ctrloops"
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
62*9880d681SAndroid Build Coastguard Worker static cl::opt<int> CTRLoopLimit("ppc-max-ctrloop", cl::Hidden, cl::init(-1));
63*9880d681SAndroid Build Coastguard Worker #endif
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCTRLoops, "Number of loops converted to CTR loops");
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker namespace llvm {
68*9880d681SAndroid Build Coastguard Worker void initializePPCCTRLoopsPass(PassRegistry&);
69*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
70*9880d681SAndroid Build Coastguard Worker void initializePPCCTRLoopsVerifyPass(PassRegistry&);
71*9880d681SAndroid Build Coastguard Worker #endif
72*9880d681SAndroid Build Coastguard Worker }
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker namespace {
75*9880d681SAndroid Build Coastguard Worker struct PPCCTRLoops : public FunctionPass {
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
78*9880d681SAndroid Build Coastguard Worker static int Counter;
79*9880d681SAndroid Build Coastguard Worker #endif
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker public:
82*9880d681SAndroid Build Coastguard Worker static char ID;
83*9880d681SAndroid Build Coastguard Worker
PPCCTRLoops__anon1306a1e60111::PPCCTRLoops84*9880d681SAndroid Build Coastguard Worker PPCCTRLoops() : FunctionPass(ID), TM(nullptr) {
85*9880d681SAndroid Build Coastguard Worker initializePPCCTRLoopsPass(*PassRegistry::getPassRegistry());
86*9880d681SAndroid Build Coastguard Worker }
PPCCTRLoops__anon1306a1e60111::PPCCTRLoops87*9880d681SAndroid Build Coastguard Worker PPCCTRLoops(PPCTargetMachine &TM) : FunctionPass(ID), TM(&TM) {
88*9880d681SAndroid Build Coastguard Worker initializePPCCTRLoopsPass(*PassRegistry::getPassRegistry());
89*9880d681SAndroid Build Coastguard Worker }
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker bool runOnFunction(Function &F) override;
92*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage__anon1306a1e60111::PPCCTRLoops93*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
94*9880d681SAndroid Build Coastguard Worker AU.addRequired<LoopInfoWrapperPass>();
95*9880d681SAndroid Build Coastguard Worker AU.addPreserved<LoopInfoWrapperPass>();
96*9880d681SAndroid Build Coastguard Worker AU.addRequired<DominatorTreeWrapperPass>();
97*9880d681SAndroid Build Coastguard Worker AU.addPreserved<DominatorTreeWrapperPass>();
98*9880d681SAndroid Build Coastguard Worker AU.addRequired<ScalarEvolutionWrapperPass>();
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker private:
102*9880d681SAndroid Build Coastguard Worker bool mightUseCTR(const Triple &TT, BasicBlock *BB);
103*9880d681SAndroid Build Coastguard Worker bool convertToCTRLoop(Loop *L);
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker private:
106*9880d681SAndroid Build Coastguard Worker PPCTargetMachine *TM;
107*9880d681SAndroid Build Coastguard Worker LoopInfo *LI;
108*9880d681SAndroid Build Coastguard Worker ScalarEvolution *SE;
109*9880d681SAndroid Build Coastguard Worker const DataLayout *DL;
110*9880d681SAndroid Build Coastguard Worker DominatorTree *DT;
111*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *LibInfo;
112*9880d681SAndroid Build Coastguard Worker bool PreserveLCSSA;
113*9880d681SAndroid Build Coastguard Worker };
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker char PPCCTRLoops::ID = 0;
116*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
117*9880d681SAndroid Build Coastguard Worker int PPCCTRLoops::Counter = 0;
118*9880d681SAndroid Build Coastguard Worker #endif
119*9880d681SAndroid Build Coastguard Worker
120*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
121*9880d681SAndroid Build Coastguard Worker struct PPCCTRLoopsVerify : public MachineFunctionPass {
122*9880d681SAndroid Build Coastguard Worker public:
123*9880d681SAndroid Build Coastguard Worker static char ID;
124*9880d681SAndroid Build Coastguard Worker
PPCCTRLoopsVerify__anon1306a1e60111::PPCCTRLoopsVerify125*9880d681SAndroid Build Coastguard Worker PPCCTRLoopsVerify() : MachineFunctionPass(ID) {
126*9880d681SAndroid Build Coastguard Worker initializePPCCTRLoopsVerifyPass(*PassRegistry::getPassRegistry());
127*9880d681SAndroid Build Coastguard Worker }
128*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage__anon1306a1e60111::PPCCTRLoopsVerify129*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
130*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineDominatorTree>();
131*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
132*9880d681SAndroid Build Coastguard Worker }
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &MF) override;
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker private:
137*9880d681SAndroid Build Coastguard Worker MachineDominatorTree *MDT;
138*9880d681SAndroid Build Coastguard Worker };
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker char PPCCTRLoopsVerify::ID = 0;
141*9880d681SAndroid Build Coastguard Worker #endif // NDEBUG
142*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops",
145*9880d681SAndroid Build Coastguard Worker false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)146*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
147*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
148*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
149*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops",
150*9880d681SAndroid Build Coastguard Worker false, false)
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createPPCCTRLoops(PPCTargetMachine &TM) {
153*9880d681SAndroid Build Coastguard Worker return new PPCCTRLoops(TM);
154*9880d681SAndroid Build Coastguard Worker }
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
157*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(PPCCTRLoopsVerify, "ppc-ctr-loops-verify",
158*9880d681SAndroid Build Coastguard Worker "PowerPC CTR Loops Verify", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)159*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
160*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(PPCCTRLoopsVerify, "ppc-ctr-loops-verify",
161*9880d681SAndroid Build Coastguard Worker "PowerPC CTR Loops Verify", false, false)
162*9880d681SAndroid Build Coastguard Worker
163*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createPPCCTRLoopsVerify() {
164*9880d681SAndroid Build Coastguard Worker return new PPCCTRLoopsVerify();
165*9880d681SAndroid Build Coastguard Worker }
166*9880d681SAndroid Build Coastguard Worker #endif // NDEBUG
167*9880d681SAndroid Build Coastguard Worker
runOnFunction(Function & F)168*9880d681SAndroid Build Coastguard Worker bool PPCCTRLoops::runOnFunction(Function &F) {
169*9880d681SAndroid Build Coastguard Worker if (skipFunction(F))
170*9880d681SAndroid Build Coastguard Worker return false;
171*9880d681SAndroid Build Coastguard Worker
172*9880d681SAndroid Build Coastguard Worker LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
173*9880d681SAndroid Build Coastguard Worker SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
174*9880d681SAndroid Build Coastguard Worker DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
175*9880d681SAndroid Build Coastguard Worker DL = &F.getParent()->getDataLayout();
176*9880d681SAndroid Build Coastguard Worker auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
177*9880d681SAndroid Build Coastguard Worker LibInfo = TLIP ? &TLIP->getTLI() : nullptr;
178*9880d681SAndroid Build Coastguard Worker PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
181*9880d681SAndroid Build Coastguard Worker
182*9880d681SAndroid Build Coastguard Worker for (LoopInfo::iterator I = LI->begin(), E = LI->end();
183*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
184*9880d681SAndroid Build Coastguard Worker Loop *L = *I;
185*9880d681SAndroid Build Coastguard Worker if (!L->getParentLoop())
186*9880d681SAndroid Build Coastguard Worker MadeChange |= convertToCTRLoop(L);
187*9880d681SAndroid Build Coastguard Worker }
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker return MadeChange;
190*9880d681SAndroid Build Coastguard Worker }
191*9880d681SAndroid Build Coastguard Worker
isLargeIntegerTy(bool Is32Bit,Type * Ty)192*9880d681SAndroid Build Coastguard Worker static bool isLargeIntegerTy(bool Is32Bit, Type *Ty) {
193*9880d681SAndroid Build Coastguard Worker if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
194*9880d681SAndroid Build Coastguard Worker return ITy->getBitWidth() > (Is32Bit ? 32U : 64U);
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard Worker return false;
197*9880d681SAndroid Build Coastguard Worker }
198*9880d681SAndroid Build Coastguard Worker
199*9880d681SAndroid Build Coastguard Worker // Determining the address of a TLS variable results in a function call in
200*9880d681SAndroid Build Coastguard Worker // certain TLS models.
memAddrUsesCTR(const PPCTargetMachine * TM,const Value * MemAddr)201*9880d681SAndroid Build Coastguard Worker static bool memAddrUsesCTR(const PPCTargetMachine *TM,
202*9880d681SAndroid Build Coastguard Worker const Value *MemAddr) {
203*9880d681SAndroid Build Coastguard Worker const auto *GV = dyn_cast<GlobalValue>(MemAddr);
204*9880d681SAndroid Build Coastguard Worker if (!GV) {
205*9880d681SAndroid Build Coastguard Worker // Recurse to check for constants that refer to TLS global variables.
206*9880d681SAndroid Build Coastguard Worker if (const auto *CV = dyn_cast<Constant>(MemAddr))
207*9880d681SAndroid Build Coastguard Worker for (const auto &CO : CV->operands())
208*9880d681SAndroid Build Coastguard Worker if (memAddrUsesCTR(TM, CO))
209*9880d681SAndroid Build Coastguard Worker return true;
210*9880d681SAndroid Build Coastguard Worker
211*9880d681SAndroid Build Coastguard Worker return false;
212*9880d681SAndroid Build Coastguard Worker }
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard Worker if (!GV->isThreadLocal())
215*9880d681SAndroid Build Coastguard Worker return false;
216*9880d681SAndroid Build Coastguard Worker if (!TM)
217*9880d681SAndroid Build Coastguard Worker return true;
218*9880d681SAndroid Build Coastguard Worker TLSModel::Model Model = TM->getTLSModel(GV);
219*9880d681SAndroid Build Coastguard Worker return Model == TLSModel::GeneralDynamic || Model == TLSModel::LocalDynamic;
220*9880d681SAndroid Build Coastguard Worker }
221*9880d681SAndroid Build Coastguard Worker
mightUseCTR(const Triple & TT,BasicBlock * BB)222*9880d681SAndroid Build Coastguard Worker bool PPCCTRLoops::mightUseCTR(const Triple &TT, BasicBlock *BB) {
223*9880d681SAndroid Build Coastguard Worker for (BasicBlock::iterator J = BB->begin(), JE = BB->end();
224*9880d681SAndroid Build Coastguard Worker J != JE; ++J) {
225*9880d681SAndroid Build Coastguard Worker if (CallInst *CI = dyn_cast<CallInst>(J)) {
226*9880d681SAndroid Build Coastguard Worker if (InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue())) {
227*9880d681SAndroid Build Coastguard Worker // Inline ASM is okay, unless it clobbers the ctr register.
228*9880d681SAndroid Build Coastguard Worker InlineAsm::ConstraintInfoVector CIV = IA->ParseConstraints();
229*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, ie = CIV.size(); i < ie; ++i) {
230*9880d681SAndroid Build Coastguard Worker InlineAsm::ConstraintInfo &C = CIV[i];
231*9880d681SAndroid Build Coastguard Worker if (C.Type != InlineAsm::isInput)
232*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0, je = C.Codes.size(); j < je; ++j)
233*9880d681SAndroid Build Coastguard Worker if (StringRef(C.Codes[j]).equals_lower("{ctr}"))
234*9880d681SAndroid Build Coastguard Worker return true;
235*9880d681SAndroid Build Coastguard Worker }
236*9880d681SAndroid Build Coastguard Worker
237*9880d681SAndroid Build Coastguard Worker continue;
238*9880d681SAndroid Build Coastguard Worker }
239*9880d681SAndroid Build Coastguard Worker
240*9880d681SAndroid Build Coastguard Worker if (!TM)
241*9880d681SAndroid Build Coastguard Worker return true;
242*9880d681SAndroid Build Coastguard Worker const TargetLowering *TLI =
243*9880d681SAndroid Build Coastguard Worker TM->getSubtargetImpl(*BB->getParent())->getTargetLowering();
244*9880d681SAndroid Build Coastguard Worker
245*9880d681SAndroid Build Coastguard Worker if (Function *F = CI->getCalledFunction()) {
246*9880d681SAndroid Build Coastguard Worker // Most intrinsics don't become function calls, but some might.
247*9880d681SAndroid Build Coastguard Worker // sin, cos, exp and log are always calls.
248*9880d681SAndroid Build Coastguard Worker unsigned Opcode = 0;
249*9880d681SAndroid Build Coastguard Worker if (F->getIntrinsicID() != Intrinsic::not_intrinsic) {
250*9880d681SAndroid Build Coastguard Worker switch (F->getIntrinsicID()) {
251*9880d681SAndroid Build Coastguard Worker default: continue;
252*9880d681SAndroid Build Coastguard Worker // If we have a call to ppc_is_decremented_ctr_nonzero, or ppc_mtctr
253*9880d681SAndroid Build Coastguard Worker // we're definitely using CTR.
254*9880d681SAndroid Build Coastguard Worker case Intrinsic::ppc_is_decremented_ctr_nonzero:
255*9880d681SAndroid Build Coastguard Worker case Intrinsic::ppc_mtctr:
256*9880d681SAndroid Build Coastguard Worker return true;
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard Worker // VisualStudio defines setjmp as _setjmp
259*9880d681SAndroid Build Coastguard Worker #if defined(_MSC_VER) && defined(setjmp) && \
260*9880d681SAndroid Build Coastguard Worker !defined(setjmp_undefined_for_msvc)
261*9880d681SAndroid Build Coastguard Worker # pragma push_macro("setjmp")
262*9880d681SAndroid Build Coastguard Worker # undef setjmp
263*9880d681SAndroid Build Coastguard Worker # define setjmp_undefined_for_msvc
264*9880d681SAndroid Build Coastguard Worker #endif
265*9880d681SAndroid Build Coastguard Worker
266*9880d681SAndroid Build Coastguard Worker case Intrinsic::setjmp:
267*9880d681SAndroid Build Coastguard Worker
268*9880d681SAndroid Build Coastguard Worker #if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)
269*9880d681SAndroid Build Coastguard Worker // let's return it to _setjmp state
270*9880d681SAndroid Build Coastguard Worker # pragma pop_macro("setjmp")
271*9880d681SAndroid Build Coastguard Worker # undef setjmp_undefined_for_msvc
272*9880d681SAndroid Build Coastguard Worker #endif
273*9880d681SAndroid Build Coastguard Worker
274*9880d681SAndroid Build Coastguard Worker case Intrinsic::longjmp:
275*9880d681SAndroid Build Coastguard Worker
276*9880d681SAndroid Build Coastguard Worker // Exclude eh_sjlj_setjmp; we don't need to exclude eh_sjlj_longjmp
277*9880d681SAndroid Build Coastguard Worker // because, although it does clobber the counter register, the
278*9880d681SAndroid Build Coastguard Worker // control can't then return to inside the loop unless there is also
279*9880d681SAndroid Build Coastguard Worker // an eh_sjlj_setjmp.
280*9880d681SAndroid Build Coastguard Worker case Intrinsic::eh_sjlj_setjmp:
281*9880d681SAndroid Build Coastguard Worker
282*9880d681SAndroid Build Coastguard Worker case Intrinsic::memcpy:
283*9880d681SAndroid Build Coastguard Worker case Intrinsic::memmove:
284*9880d681SAndroid Build Coastguard Worker case Intrinsic::memset:
285*9880d681SAndroid Build Coastguard Worker case Intrinsic::powi:
286*9880d681SAndroid Build Coastguard Worker case Intrinsic::log:
287*9880d681SAndroid Build Coastguard Worker case Intrinsic::log2:
288*9880d681SAndroid Build Coastguard Worker case Intrinsic::log10:
289*9880d681SAndroid Build Coastguard Worker case Intrinsic::exp:
290*9880d681SAndroid Build Coastguard Worker case Intrinsic::exp2:
291*9880d681SAndroid Build Coastguard Worker case Intrinsic::pow:
292*9880d681SAndroid Build Coastguard Worker case Intrinsic::sin:
293*9880d681SAndroid Build Coastguard Worker case Intrinsic::cos:
294*9880d681SAndroid Build Coastguard Worker return true;
295*9880d681SAndroid Build Coastguard Worker case Intrinsic::copysign:
296*9880d681SAndroid Build Coastguard Worker if (CI->getArgOperand(0)->getType()->getScalarType()->
297*9880d681SAndroid Build Coastguard Worker isPPC_FP128Ty())
298*9880d681SAndroid Build Coastguard Worker return true;
299*9880d681SAndroid Build Coastguard Worker else
300*9880d681SAndroid Build Coastguard Worker continue; // ISD::FCOPYSIGN is never a library call.
301*9880d681SAndroid Build Coastguard Worker case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
302*9880d681SAndroid Build Coastguard Worker case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
303*9880d681SAndroid Build Coastguard Worker case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
304*9880d681SAndroid Build Coastguard Worker case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
305*9880d681SAndroid Build Coastguard Worker case Intrinsic::rint: Opcode = ISD::FRINT; break;
306*9880d681SAndroid Build Coastguard Worker case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
307*9880d681SAndroid Build Coastguard Worker case Intrinsic::round: Opcode = ISD::FROUND; break;
308*9880d681SAndroid Build Coastguard Worker case Intrinsic::minnum: Opcode = ISD::FMINNUM; break;
309*9880d681SAndroid Build Coastguard Worker case Intrinsic::maxnum: Opcode = ISD::FMAXNUM; break;
310*9880d681SAndroid Build Coastguard Worker }
311*9880d681SAndroid Build Coastguard Worker }
312*9880d681SAndroid Build Coastguard Worker
313*9880d681SAndroid Build Coastguard Worker // PowerPC does not use [US]DIVREM or other library calls for
314*9880d681SAndroid Build Coastguard Worker // operations on regular types which are not otherwise library calls
315*9880d681SAndroid Build Coastguard Worker // (i.e. soft float or atomics). If adapting for targets that do,
316*9880d681SAndroid Build Coastguard Worker // additional care is required here.
317*9880d681SAndroid Build Coastguard Worker
318*9880d681SAndroid Build Coastguard Worker LibFunc::Func Func;
319*9880d681SAndroid Build Coastguard Worker if (!F->hasLocalLinkage() && F->hasName() && LibInfo &&
320*9880d681SAndroid Build Coastguard Worker LibInfo->getLibFunc(F->getName(), Func) &&
321*9880d681SAndroid Build Coastguard Worker LibInfo->hasOptimizedCodeGen(Func)) {
322*9880d681SAndroid Build Coastguard Worker // Non-read-only functions are never treated as intrinsics.
323*9880d681SAndroid Build Coastguard Worker if (!CI->onlyReadsMemory())
324*9880d681SAndroid Build Coastguard Worker return true;
325*9880d681SAndroid Build Coastguard Worker
326*9880d681SAndroid Build Coastguard Worker // Conversion happens only for FP calls.
327*9880d681SAndroid Build Coastguard Worker if (!CI->getArgOperand(0)->getType()->isFloatingPointTy())
328*9880d681SAndroid Build Coastguard Worker return true;
329*9880d681SAndroid Build Coastguard Worker
330*9880d681SAndroid Build Coastguard Worker switch (Func) {
331*9880d681SAndroid Build Coastguard Worker default: return true;
332*9880d681SAndroid Build Coastguard Worker case LibFunc::copysign:
333*9880d681SAndroid Build Coastguard Worker case LibFunc::copysignf:
334*9880d681SAndroid Build Coastguard Worker continue; // ISD::FCOPYSIGN is never a library call.
335*9880d681SAndroid Build Coastguard Worker case LibFunc::copysignl:
336*9880d681SAndroid Build Coastguard Worker return true;
337*9880d681SAndroid Build Coastguard Worker case LibFunc::fabs:
338*9880d681SAndroid Build Coastguard Worker case LibFunc::fabsf:
339*9880d681SAndroid Build Coastguard Worker case LibFunc::fabsl:
340*9880d681SAndroid Build Coastguard Worker continue; // ISD::FABS is never a library call.
341*9880d681SAndroid Build Coastguard Worker case LibFunc::sqrt:
342*9880d681SAndroid Build Coastguard Worker case LibFunc::sqrtf:
343*9880d681SAndroid Build Coastguard Worker case LibFunc::sqrtl:
344*9880d681SAndroid Build Coastguard Worker Opcode = ISD::FSQRT; break;
345*9880d681SAndroid Build Coastguard Worker case LibFunc::floor:
346*9880d681SAndroid Build Coastguard Worker case LibFunc::floorf:
347*9880d681SAndroid Build Coastguard Worker case LibFunc::floorl:
348*9880d681SAndroid Build Coastguard Worker Opcode = ISD::FFLOOR; break;
349*9880d681SAndroid Build Coastguard Worker case LibFunc::nearbyint:
350*9880d681SAndroid Build Coastguard Worker case LibFunc::nearbyintf:
351*9880d681SAndroid Build Coastguard Worker case LibFunc::nearbyintl:
352*9880d681SAndroid Build Coastguard Worker Opcode = ISD::FNEARBYINT; break;
353*9880d681SAndroid Build Coastguard Worker case LibFunc::ceil:
354*9880d681SAndroid Build Coastguard Worker case LibFunc::ceilf:
355*9880d681SAndroid Build Coastguard Worker case LibFunc::ceill:
356*9880d681SAndroid Build Coastguard Worker Opcode = ISD::FCEIL; break;
357*9880d681SAndroid Build Coastguard Worker case LibFunc::rint:
358*9880d681SAndroid Build Coastguard Worker case LibFunc::rintf:
359*9880d681SAndroid Build Coastguard Worker case LibFunc::rintl:
360*9880d681SAndroid Build Coastguard Worker Opcode = ISD::FRINT; break;
361*9880d681SAndroid Build Coastguard Worker case LibFunc::round:
362*9880d681SAndroid Build Coastguard Worker case LibFunc::roundf:
363*9880d681SAndroid Build Coastguard Worker case LibFunc::roundl:
364*9880d681SAndroid Build Coastguard Worker Opcode = ISD::FROUND; break;
365*9880d681SAndroid Build Coastguard Worker case LibFunc::trunc:
366*9880d681SAndroid Build Coastguard Worker case LibFunc::truncf:
367*9880d681SAndroid Build Coastguard Worker case LibFunc::truncl:
368*9880d681SAndroid Build Coastguard Worker Opcode = ISD::FTRUNC; break;
369*9880d681SAndroid Build Coastguard Worker case LibFunc::fmin:
370*9880d681SAndroid Build Coastguard Worker case LibFunc::fminf:
371*9880d681SAndroid Build Coastguard Worker case LibFunc::fminl:
372*9880d681SAndroid Build Coastguard Worker Opcode = ISD::FMINNUM; break;
373*9880d681SAndroid Build Coastguard Worker case LibFunc::fmax:
374*9880d681SAndroid Build Coastguard Worker case LibFunc::fmaxf:
375*9880d681SAndroid Build Coastguard Worker case LibFunc::fmaxl:
376*9880d681SAndroid Build Coastguard Worker Opcode = ISD::FMAXNUM; break;
377*9880d681SAndroid Build Coastguard Worker }
378*9880d681SAndroid Build Coastguard Worker }
379*9880d681SAndroid Build Coastguard Worker
380*9880d681SAndroid Build Coastguard Worker if (Opcode) {
381*9880d681SAndroid Build Coastguard Worker auto &DL = CI->getModule()->getDataLayout();
382*9880d681SAndroid Build Coastguard Worker MVT VTy = TLI->getSimpleValueType(DL, CI->getArgOperand(0)->getType(),
383*9880d681SAndroid Build Coastguard Worker true);
384*9880d681SAndroid Build Coastguard Worker if (VTy == MVT::Other)
385*9880d681SAndroid Build Coastguard Worker return true;
386*9880d681SAndroid Build Coastguard Worker
387*9880d681SAndroid Build Coastguard Worker if (TLI->isOperationLegalOrCustom(Opcode, VTy))
388*9880d681SAndroid Build Coastguard Worker continue;
389*9880d681SAndroid Build Coastguard Worker else if (VTy.isVector() &&
390*9880d681SAndroid Build Coastguard Worker TLI->isOperationLegalOrCustom(Opcode, VTy.getScalarType()))
391*9880d681SAndroid Build Coastguard Worker continue;
392*9880d681SAndroid Build Coastguard Worker
393*9880d681SAndroid Build Coastguard Worker return true;
394*9880d681SAndroid Build Coastguard Worker }
395*9880d681SAndroid Build Coastguard Worker }
396*9880d681SAndroid Build Coastguard Worker
397*9880d681SAndroid Build Coastguard Worker return true;
398*9880d681SAndroid Build Coastguard Worker } else if (isa<BinaryOperator>(J) &&
399*9880d681SAndroid Build Coastguard Worker J->getType()->getScalarType()->isPPC_FP128Ty()) {
400*9880d681SAndroid Build Coastguard Worker // Most operations on ppc_f128 values become calls.
401*9880d681SAndroid Build Coastguard Worker return true;
402*9880d681SAndroid Build Coastguard Worker } else if (isa<UIToFPInst>(J) || isa<SIToFPInst>(J) ||
403*9880d681SAndroid Build Coastguard Worker isa<FPToUIInst>(J) || isa<FPToSIInst>(J)) {
404*9880d681SAndroid Build Coastguard Worker CastInst *CI = cast<CastInst>(J);
405*9880d681SAndroid Build Coastguard Worker if (CI->getSrcTy()->getScalarType()->isPPC_FP128Ty() ||
406*9880d681SAndroid Build Coastguard Worker CI->getDestTy()->getScalarType()->isPPC_FP128Ty() ||
407*9880d681SAndroid Build Coastguard Worker isLargeIntegerTy(TT.isArch32Bit(), CI->getSrcTy()->getScalarType()) ||
408*9880d681SAndroid Build Coastguard Worker isLargeIntegerTy(TT.isArch32Bit(), CI->getDestTy()->getScalarType()))
409*9880d681SAndroid Build Coastguard Worker return true;
410*9880d681SAndroid Build Coastguard Worker } else if (isLargeIntegerTy(TT.isArch32Bit(),
411*9880d681SAndroid Build Coastguard Worker J->getType()->getScalarType()) &&
412*9880d681SAndroid Build Coastguard Worker (J->getOpcode() == Instruction::UDiv ||
413*9880d681SAndroid Build Coastguard Worker J->getOpcode() == Instruction::SDiv ||
414*9880d681SAndroid Build Coastguard Worker J->getOpcode() == Instruction::URem ||
415*9880d681SAndroid Build Coastguard Worker J->getOpcode() == Instruction::SRem)) {
416*9880d681SAndroid Build Coastguard Worker return true;
417*9880d681SAndroid Build Coastguard Worker } else if (TT.isArch32Bit() &&
418*9880d681SAndroid Build Coastguard Worker isLargeIntegerTy(false, J->getType()->getScalarType()) &&
419*9880d681SAndroid Build Coastguard Worker (J->getOpcode() == Instruction::Shl ||
420*9880d681SAndroid Build Coastguard Worker J->getOpcode() == Instruction::AShr ||
421*9880d681SAndroid Build Coastguard Worker J->getOpcode() == Instruction::LShr)) {
422*9880d681SAndroid Build Coastguard Worker // Only on PPC32, for 128-bit integers (specifically not 64-bit
423*9880d681SAndroid Build Coastguard Worker // integers), these might be runtime calls.
424*9880d681SAndroid Build Coastguard Worker return true;
425*9880d681SAndroid Build Coastguard Worker } else if (isa<IndirectBrInst>(J) || isa<InvokeInst>(J)) {
426*9880d681SAndroid Build Coastguard Worker // On PowerPC, indirect jumps use the counter register.
427*9880d681SAndroid Build Coastguard Worker return true;
428*9880d681SAndroid Build Coastguard Worker } else if (SwitchInst *SI = dyn_cast<SwitchInst>(J)) {
429*9880d681SAndroid Build Coastguard Worker if (!TM)
430*9880d681SAndroid Build Coastguard Worker return true;
431*9880d681SAndroid Build Coastguard Worker const TargetLowering *TLI =
432*9880d681SAndroid Build Coastguard Worker TM->getSubtargetImpl(*BB->getParent())->getTargetLowering();
433*9880d681SAndroid Build Coastguard Worker
434*9880d681SAndroid Build Coastguard Worker if (SI->getNumCases() + 1 >= (unsigned)TLI->getMinimumJumpTableEntries())
435*9880d681SAndroid Build Coastguard Worker return true;
436*9880d681SAndroid Build Coastguard Worker }
437*9880d681SAndroid Build Coastguard Worker
438*9880d681SAndroid Build Coastguard Worker if (TM->getSubtargetImpl(*BB->getParent())->getTargetLowering()->useSoftFloat()) {
439*9880d681SAndroid Build Coastguard Worker switch(J->getOpcode()) {
440*9880d681SAndroid Build Coastguard Worker case Instruction::FAdd:
441*9880d681SAndroid Build Coastguard Worker case Instruction::FSub:
442*9880d681SAndroid Build Coastguard Worker case Instruction::FMul:
443*9880d681SAndroid Build Coastguard Worker case Instruction::FDiv:
444*9880d681SAndroid Build Coastguard Worker case Instruction::FRem:
445*9880d681SAndroid Build Coastguard Worker case Instruction::FPTrunc:
446*9880d681SAndroid Build Coastguard Worker case Instruction::FPExt:
447*9880d681SAndroid Build Coastguard Worker case Instruction::FPToUI:
448*9880d681SAndroid Build Coastguard Worker case Instruction::FPToSI:
449*9880d681SAndroid Build Coastguard Worker case Instruction::UIToFP:
450*9880d681SAndroid Build Coastguard Worker case Instruction::SIToFP:
451*9880d681SAndroid Build Coastguard Worker case Instruction::FCmp:
452*9880d681SAndroid Build Coastguard Worker return true;
453*9880d681SAndroid Build Coastguard Worker }
454*9880d681SAndroid Build Coastguard Worker }
455*9880d681SAndroid Build Coastguard Worker
456*9880d681SAndroid Build Coastguard Worker for (Value *Operand : J->operands())
457*9880d681SAndroid Build Coastguard Worker if (memAddrUsesCTR(TM, Operand))
458*9880d681SAndroid Build Coastguard Worker return true;
459*9880d681SAndroid Build Coastguard Worker }
460*9880d681SAndroid Build Coastguard Worker
461*9880d681SAndroid Build Coastguard Worker return false;
462*9880d681SAndroid Build Coastguard Worker }
463*9880d681SAndroid Build Coastguard Worker
convertToCTRLoop(Loop * L)464*9880d681SAndroid Build Coastguard Worker bool PPCCTRLoops::convertToCTRLoop(Loop *L) {
465*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
466*9880d681SAndroid Build Coastguard Worker
467*9880d681SAndroid Build Coastguard Worker const Triple TT =
468*9880d681SAndroid Build Coastguard Worker Triple(L->getHeader()->getParent()->getParent()->getTargetTriple());
469*9880d681SAndroid Build Coastguard Worker if (!TT.isArch32Bit() && !TT.isArch64Bit())
470*9880d681SAndroid Build Coastguard Worker return MadeChange; // Unknown arch. type.
471*9880d681SAndroid Build Coastguard Worker
472*9880d681SAndroid Build Coastguard Worker // Process nested loops first.
473*9880d681SAndroid Build Coastguard Worker for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) {
474*9880d681SAndroid Build Coastguard Worker MadeChange |= convertToCTRLoop(*I);
475*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Nested loop converted\n");
476*9880d681SAndroid Build Coastguard Worker }
477*9880d681SAndroid Build Coastguard Worker
478*9880d681SAndroid Build Coastguard Worker // If a nested loop has been converted, then we can't convert this loop.
479*9880d681SAndroid Build Coastguard Worker if (MadeChange)
480*9880d681SAndroid Build Coastguard Worker return MadeChange;
481*9880d681SAndroid Build Coastguard Worker
482*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
483*9880d681SAndroid Build Coastguard Worker // Stop trying after reaching the limit (if any).
484*9880d681SAndroid Build Coastguard Worker int Limit = CTRLoopLimit;
485*9880d681SAndroid Build Coastguard Worker if (Limit >= 0) {
486*9880d681SAndroid Build Coastguard Worker if (Counter >= CTRLoopLimit)
487*9880d681SAndroid Build Coastguard Worker return false;
488*9880d681SAndroid Build Coastguard Worker Counter++;
489*9880d681SAndroid Build Coastguard Worker }
490*9880d681SAndroid Build Coastguard Worker #endif
491*9880d681SAndroid Build Coastguard Worker
492*9880d681SAndroid Build Coastguard Worker // We don't want to spill/restore the counter register, and so we don't
493*9880d681SAndroid Build Coastguard Worker // want to use the counter register if the loop contains calls.
494*9880d681SAndroid Build Coastguard Worker for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
495*9880d681SAndroid Build Coastguard Worker I != IE; ++I)
496*9880d681SAndroid Build Coastguard Worker if (mightUseCTR(TT, *I))
497*9880d681SAndroid Build Coastguard Worker return MadeChange;
498*9880d681SAndroid Build Coastguard Worker
499*9880d681SAndroid Build Coastguard Worker SmallVector<BasicBlock*, 4> ExitingBlocks;
500*9880d681SAndroid Build Coastguard Worker L->getExitingBlocks(ExitingBlocks);
501*9880d681SAndroid Build Coastguard Worker
502*9880d681SAndroid Build Coastguard Worker BasicBlock *CountedExitBlock = nullptr;
503*9880d681SAndroid Build Coastguard Worker const SCEV *ExitCount = nullptr;
504*9880d681SAndroid Build Coastguard Worker BranchInst *CountedExitBranch = nullptr;
505*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<BasicBlock *>::iterator I = ExitingBlocks.begin(),
506*9880d681SAndroid Build Coastguard Worker IE = ExitingBlocks.end(); I != IE; ++I) {
507*9880d681SAndroid Build Coastguard Worker const SCEV *EC = SE->getExitCount(L, *I);
508*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Exit Count for " << *L << " from block " <<
509*9880d681SAndroid Build Coastguard Worker (*I)->getName() << ": " << *EC << "\n");
510*9880d681SAndroid Build Coastguard Worker if (isa<SCEVCouldNotCompute>(EC))
511*9880d681SAndroid Build Coastguard Worker continue;
512*9880d681SAndroid Build Coastguard Worker if (const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) {
513*9880d681SAndroid Build Coastguard Worker if (ConstEC->getValue()->isZero())
514*9880d681SAndroid Build Coastguard Worker continue;
515*9880d681SAndroid Build Coastguard Worker } else if (!SE->isLoopInvariant(EC, L))
516*9880d681SAndroid Build Coastguard Worker continue;
517*9880d681SAndroid Build Coastguard Worker
518*9880d681SAndroid Build Coastguard Worker if (SE->getTypeSizeInBits(EC->getType()) > (TT.isArch64Bit() ? 64 : 32))
519*9880d681SAndroid Build Coastguard Worker continue;
520*9880d681SAndroid Build Coastguard Worker
521*9880d681SAndroid Build Coastguard Worker // We now have a loop-invariant count of loop iterations (which is not the
522*9880d681SAndroid Build Coastguard Worker // constant zero) for which we know that this loop will not exit via this
523*9880d681SAndroid Build Coastguard Worker // exisiting block.
524*9880d681SAndroid Build Coastguard Worker
525*9880d681SAndroid Build Coastguard Worker // We need to make sure that this block will run on every loop iteration.
526*9880d681SAndroid Build Coastguard Worker // For this to be true, we must dominate all blocks with backedges. Such
527*9880d681SAndroid Build Coastguard Worker // blocks are in-loop predecessors to the header block.
528*9880d681SAndroid Build Coastguard Worker bool NotAlways = false;
529*9880d681SAndroid Build Coastguard Worker for (pred_iterator PI = pred_begin(L->getHeader()),
530*9880d681SAndroid Build Coastguard Worker PIE = pred_end(L->getHeader()); PI != PIE; ++PI) {
531*9880d681SAndroid Build Coastguard Worker if (!L->contains(*PI))
532*9880d681SAndroid Build Coastguard Worker continue;
533*9880d681SAndroid Build Coastguard Worker
534*9880d681SAndroid Build Coastguard Worker if (!DT->dominates(*I, *PI)) {
535*9880d681SAndroid Build Coastguard Worker NotAlways = true;
536*9880d681SAndroid Build Coastguard Worker break;
537*9880d681SAndroid Build Coastguard Worker }
538*9880d681SAndroid Build Coastguard Worker }
539*9880d681SAndroid Build Coastguard Worker
540*9880d681SAndroid Build Coastguard Worker if (NotAlways)
541*9880d681SAndroid Build Coastguard Worker continue;
542*9880d681SAndroid Build Coastguard Worker
543*9880d681SAndroid Build Coastguard Worker // Make sure this blocks ends with a conditional branch.
544*9880d681SAndroid Build Coastguard Worker Instruction *TI = (*I)->getTerminator();
545*9880d681SAndroid Build Coastguard Worker if (!TI)
546*9880d681SAndroid Build Coastguard Worker continue;
547*9880d681SAndroid Build Coastguard Worker
548*9880d681SAndroid Build Coastguard Worker if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
549*9880d681SAndroid Build Coastguard Worker if (!BI->isConditional())
550*9880d681SAndroid Build Coastguard Worker continue;
551*9880d681SAndroid Build Coastguard Worker
552*9880d681SAndroid Build Coastguard Worker CountedExitBranch = BI;
553*9880d681SAndroid Build Coastguard Worker } else
554*9880d681SAndroid Build Coastguard Worker continue;
555*9880d681SAndroid Build Coastguard Worker
556*9880d681SAndroid Build Coastguard Worker // Note that this block may not be the loop latch block, even if the loop
557*9880d681SAndroid Build Coastguard Worker // has a latch block.
558*9880d681SAndroid Build Coastguard Worker CountedExitBlock = *I;
559*9880d681SAndroid Build Coastguard Worker ExitCount = EC;
560*9880d681SAndroid Build Coastguard Worker break;
561*9880d681SAndroid Build Coastguard Worker }
562*9880d681SAndroid Build Coastguard Worker
563*9880d681SAndroid Build Coastguard Worker if (!CountedExitBlock)
564*9880d681SAndroid Build Coastguard Worker return MadeChange;
565*9880d681SAndroid Build Coastguard Worker
566*9880d681SAndroid Build Coastguard Worker BasicBlock *Preheader = L->getLoopPreheader();
567*9880d681SAndroid Build Coastguard Worker
568*9880d681SAndroid Build Coastguard Worker // If we don't have a preheader, then insert one. If we already have a
569*9880d681SAndroid Build Coastguard Worker // preheader, then we can use it (except if the preheader contains a use of
570*9880d681SAndroid Build Coastguard Worker // the CTR register because some such uses might be reordered by the
571*9880d681SAndroid Build Coastguard Worker // selection DAG after the mtctr instruction).
572*9880d681SAndroid Build Coastguard Worker if (!Preheader || mightUseCTR(TT, Preheader))
573*9880d681SAndroid Build Coastguard Worker Preheader = InsertPreheaderForLoop(L, DT, LI, PreserveLCSSA);
574*9880d681SAndroid Build Coastguard Worker if (!Preheader)
575*9880d681SAndroid Build Coastguard Worker return MadeChange;
576*9880d681SAndroid Build Coastguard Worker
577*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Preheader for exit count: " << Preheader->getName() << "\n");
578*9880d681SAndroid Build Coastguard Worker
579*9880d681SAndroid Build Coastguard Worker // Insert the count into the preheader and replace the condition used by the
580*9880d681SAndroid Build Coastguard Worker // selected branch.
581*9880d681SAndroid Build Coastguard Worker MadeChange = true;
582*9880d681SAndroid Build Coastguard Worker
583*9880d681SAndroid Build Coastguard Worker SCEVExpander SCEVE(*SE, Preheader->getModule()->getDataLayout(), "loopcnt");
584*9880d681SAndroid Build Coastguard Worker LLVMContext &C = SE->getContext();
585*9880d681SAndroid Build Coastguard Worker Type *CountType = TT.isArch64Bit() ? Type::getInt64Ty(C) :
586*9880d681SAndroid Build Coastguard Worker Type::getInt32Ty(C);
587*9880d681SAndroid Build Coastguard Worker if (!ExitCount->getType()->isPointerTy() &&
588*9880d681SAndroid Build Coastguard Worker ExitCount->getType() != CountType)
589*9880d681SAndroid Build Coastguard Worker ExitCount = SE->getZeroExtendExpr(ExitCount, CountType);
590*9880d681SAndroid Build Coastguard Worker ExitCount = SE->getAddExpr(ExitCount, SE->getOne(CountType));
591*9880d681SAndroid Build Coastguard Worker Value *ECValue =
592*9880d681SAndroid Build Coastguard Worker SCEVE.expandCodeFor(ExitCount, CountType, Preheader->getTerminator());
593*9880d681SAndroid Build Coastguard Worker
594*9880d681SAndroid Build Coastguard Worker IRBuilder<> CountBuilder(Preheader->getTerminator());
595*9880d681SAndroid Build Coastguard Worker Module *M = Preheader->getParent()->getParent();
596*9880d681SAndroid Build Coastguard Worker Value *MTCTRFunc = Intrinsic::getDeclaration(M, Intrinsic::ppc_mtctr,
597*9880d681SAndroid Build Coastguard Worker CountType);
598*9880d681SAndroid Build Coastguard Worker CountBuilder.CreateCall(MTCTRFunc, ECValue);
599*9880d681SAndroid Build Coastguard Worker
600*9880d681SAndroid Build Coastguard Worker IRBuilder<> CondBuilder(CountedExitBranch);
601*9880d681SAndroid Build Coastguard Worker Value *DecFunc =
602*9880d681SAndroid Build Coastguard Worker Intrinsic::getDeclaration(M, Intrinsic::ppc_is_decremented_ctr_nonzero);
603*9880d681SAndroid Build Coastguard Worker Value *NewCond = CondBuilder.CreateCall(DecFunc, {});
604*9880d681SAndroid Build Coastguard Worker Value *OldCond = CountedExitBranch->getCondition();
605*9880d681SAndroid Build Coastguard Worker CountedExitBranch->setCondition(NewCond);
606*9880d681SAndroid Build Coastguard Worker
607*9880d681SAndroid Build Coastguard Worker // The false branch must exit the loop.
608*9880d681SAndroid Build Coastguard Worker if (!L->contains(CountedExitBranch->getSuccessor(0)))
609*9880d681SAndroid Build Coastguard Worker CountedExitBranch->swapSuccessors();
610*9880d681SAndroid Build Coastguard Worker
611*9880d681SAndroid Build Coastguard Worker // The old condition may be dead now, and may have even created a dead PHI
612*9880d681SAndroid Build Coastguard Worker // (the original induction variable).
613*9880d681SAndroid Build Coastguard Worker RecursivelyDeleteTriviallyDeadInstructions(OldCond);
614*9880d681SAndroid Build Coastguard Worker DeleteDeadPHIs(CountedExitBlock);
615*9880d681SAndroid Build Coastguard Worker
616*9880d681SAndroid Build Coastguard Worker ++NumCTRLoops;
617*9880d681SAndroid Build Coastguard Worker return MadeChange;
618*9880d681SAndroid Build Coastguard Worker }
619*9880d681SAndroid Build Coastguard Worker
620*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
clobbersCTR(const MachineInstr * MI)621*9880d681SAndroid Build Coastguard Worker static bool clobbersCTR(const MachineInstr *MI) {
622*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
623*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI->getOperand(i);
624*9880d681SAndroid Build Coastguard Worker if (MO.isReg()) {
625*9880d681SAndroid Build Coastguard Worker if (MO.isDef() && (MO.getReg() == PPC::CTR || MO.getReg() == PPC::CTR8))
626*9880d681SAndroid Build Coastguard Worker return true;
627*9880d681SAndroid Build Coastguard Worker } else if (MO.isRegMask()) {
628*9880d681SAndroid Build Coastguard Worker if (MO.clobbersPhysReg(PPC::CTR) || MO.clobbersPhysReg(PPC::CTR8))
629*9880d681SAndroid Build Coastguard Worker return true;
630*9880d681SAndroid Build Coastguard Worker }
631*9880d681SAndroid Build Coastguard Worker }
632*9880d681SAndroid Build Coastguard Worker
633*9880d681SAndroid Build Coastguard Worker return false;
634*9880d681SAndroid Build Coastguard Worker }
635*9880d681SAndroid Build Coastguard Worker
verifyCTRBranch(MachineBasicBlock * MBB,MachineBasicBlock::iterator I)636*9880d681SAndroid Build Coastguard Worker static bool verifyCTRBranch(MachineBasicBlock *MBB,
637*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator I) {
638*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator BI = I;
639*9880d681SAndroid Build Coastguard Worker SmallSet<MachineBasicBlock *, 16> Visited;
640*9880d681SAndroid Build Coastguard Worker SmallVector<MachineBasicBlock *, 8> Preds;
641*9880d681SAndroid Build Coastguard Worker bool CheckPreds;
642*9880d681SAndroid Build Coastguard Worker
643*9880d681SAndroid Build Coastguard Worker if (I == MBB->begin()) {
644*9880d681SAndroid Build Coastguard Worker Visited.insert(MBB);
645*9880d681SAndroid Build Coastguard Worker goto queue_preds;
646*9880d681SAndroid Build Coastguard Worker } else
647*9880d681SAndroid Build Coastguard Worker --I;
648*9880d681SAndroid Build Coastguard Worker
649*9880d681SAndroid Build Coastguard Worker check_block:
650*9880d681SAndroid Build Coastguard Worker Visited.insert(MBB);
651*9880d681SAndroid Build Coastguard Worker if (I == MBB->end())
652*9880d681SAndroid Build Coastguard Worker goto queue_preds;
653*9880d681SAndroid Build Coastguard Worker
654*9880d681SAndroid Build Coastguard Worker CheckPreds = true;
655*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::iterator IE = MBB->begin();; --I) {
656*9880d681SAndroid Build Coastguard Worker unsigned Opc = I->getOpcode();
657*9880d681SAndroid Build Coastguard Worker if (Opc == PPC::MTCTRloop || Opc == PPC::MTCTR8loop) {
658*9880d681SAndroid Build Coastguard Worker CheckPreds = false;
659*9880d681SAndroid Build Coastguard Worker break;
660*9880d681SAndroid Build Coastguard Worker }
661*9880d681SAndroid Build Coastguard Worker
662*9880d681SAndroid Build Coastguard Worker if (I != BI && clobbersCTR(I)) {
663*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "BB#" << MBB->getNumber() << " (" <<
664*9880d681SAndroid Build Coastguard Worker MBB->getFullName() << ") instruction " << *I <<
665*9880d681SAndroid Build Coastguard Worker " clobbers CTR, invalidating " << "BB#" <<
666*9880d681SAndroid Build Coastguard Worker BI->getParent()->getNumber() << " (" <<
667*9880d681SAndroid Build Coastguard Worker BI->getParent()->getFullName() << ") instruction " <<
668*9880d681SAndroid Build Coastguard Worker *BI << "\n");
669*9880d681SAndroid Build Coastguard Worker return false;
670*9880d681SAndroid Build Coastguard Worker }
671*9880d681SAndroid Build Coastguard Worker
672*9880d681SAndroid Build Coastguard Worker if (I == IE)
673*9880d681SAndroid Build Coastguard Worker break;
674*9880d681SAndroid Build Coastguard Worker }
675*9880d681SAndroid Build Coastguard Worker
676*9880d681SAndroid Build Coastguard Worker if (!CheckPreds && Preds.empty())
677*9880d681SAndroid Build Coastguard Worker return true;
678*9880d681SAndroid Build Coastguard Worker
679*9880d681SAndroid Build Coastguard Worker if (CheckPreds) {
680*9880d681SAndroid Build Coastguard Worker queue_preds:
681*9880d681SAndroid Build Coastguard Worker if (MachineFunction::iterator(MBB) == MBB->getParent()->begin()) {
682*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Unable to find a MTCTR instruction for BB#" <<
683*9880d681SAndroid Build Coastguard Worker BI->getParent()->getNumber() << " (" <<
684*9880d681SAndroid Build Coastguard Worker BI->getParent()->getFullName() << ") instruction " <<
685*9880d681SAndroid Build Coastguard Worker *BI << "\n");
686*9880d681SAndroid Build Coastguard Worker return false;
687*9880d681SAndroid Build Coastguard Worker }
688*9880d681SAndroid Build Coastguard Worker
689*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
690*9880d681SAndroid Build Coastguard Worker PIE = MBB->pred_end(); PI != PIE; ++PI)
691*9880d681SAndroid Build Coastguard Worker Preds.push_back(*PI);
692*9880d681SAndroid Build Coastguard Worker }
693*9880d681SAndroid Build Coastguard Worker
694*9880d681SAndroid Build Coastguard Worker do {
695*9880d681SAndroid Build Coastguard Worker MBB = Preds.pop_back_val();
696*9880d681SAndroid Build Coastguard Worker if (!Visited.count(MBB)) {
697*9880d681SAndroid Build Coastguard Worker I = MBB->getLastNonDebugInstr();
698*9880d681SAndroid Build Coastguard Worker goto check_block;
699*9880d681SAndroid Build Coastguard Worker }
700*9880d681SAndroid Build Coastguard Worker } while (!Preds.empty());
701*9880d681SAndroid Build Coastguard Worker
702*9880d681SAndroid Build Coastguard Worker return true;
703*9880d681SAndroid Build Coastguard Worker }
704*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & MF)705*9880d681SAndroid Build Coastguard Worker bool PPCCTRLoopsVerify::runOnMachineFunction(MachineFunction &MF) {
706*9880d681SAndroid Build Coastguard Worker MDT = &getAnalysis<MachineDominatorTree>();
707*9880d681SAndroid Build Coastguard Worker
708*9880d681SAndroid Build Coastguard Worker // Verify that all bdnz/bdz instructions are dominated by a loop mtctr before
709*9880d681SAndroid Build Coastguard Worker // any other instructions that might clobber the ctr register.
710*9880d681SAndroid Build Coastguard Worker for (MachineFunction::iterator I = MF.begin(), IE = MF.end();
711*9880d681SAndroid Build Coastguard Worker I != IE; ++I) {
712*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = &*I;
713*9880d681SAndroid Build Coastguard Worker if (!MDT->isReachableFromEntry(MBB))
714*9880d681SAndroid Build Coastguard Worker continue;
715*9880d681SAndroid Build Coastguard Worker
716*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::iterator MII = MBB->getFirstTerminator(),
717*9880d681SAndroid Build Coastguard Worker MIIE = MBB->end(); MII != MIIE; ++MII) {
718*9880d681SAndroid Build Coastguard Worker unsigned Opc = MII->getOpcode();
719*9880d681SAndroid Build Coastguard Worker if (Opc == PPC::BDNZ8 || Opc == PPC::BDNZ ||
720*9880d681SAndroid Build Coastguard Worker Opc == PPC::BDZ8 || Opc == PPC::BDZ)
721*9880d681SAndroid Build Coastguard Worker if (!verifyCTRBranch(MBB, MII))
722*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Invalid PPC CTR loop!");
723*9880d681SAndroid Build Coastguard Worker }
724*9880d681SAndroid Build Coastguard Worker }
725*9880d681SAndroid Build Coastguard Worker
726*9880d681SAndroid Build Coastguard Worker return false;
727*9880d681SAndroid Build Coastguard Worker }
728*9880d681SAndroid Build Coastguard Worker #endif // NDEBUG
729