69 #define DEBUG_TYPE "ctrloops" 79 cl::desc(
"Loops with a constant trip count smaller than " 80 "this value will not use the count register."));
82 STATISTIC(NumCTRLoops,
"Number of loops converted to CTR loops");
119 bool convertToCTRLoop(
Loop *L);
137 int PPCCTRLoops::Counter = 0;
176 "PowerPC CTR Loops Verify",
false,
false)
182 return new PPCCTRLoopsVerify();
190 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
195 STI =
TM->getSubtargetImpl(F);
196 TLI = STI->getTargetLowering();
198 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
199 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
200 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
201 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
203 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
204 LibInfo = TLIP ? &TLIP->getTLI() :
nullptr;
205 PreserveLCSSA = mustPreserveAnalysisID(
LCSSAID);
207 bool MadeChange =
false;
213 MadeChange |= convertToCTRLoop(L);
221 return ITy->getBitWidth() > (Is32Bit ? 32U : 64U);
232 if (
const auto *CV = dyn_cast<Constant>(MemAddr))
233 for (
const auto &CO : CV->operands())
240 if (!GV->isThreadLocal())
250 for (
unsigned i = 0, ie = CIV.size(); i < ie; ++i) {
253 for (
unsigned j = 0, je = C.
Codes.size(); j < je; ++j)
260 bool PPCCTRLoops::mightUseCTR(
BasicBlock *BB) {
263 if (
CallInst *CI = dyn_cast<CallInst>(J)) {
265 if (
InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue())) {
271 if (
Function *F = CI->getCalledFunction()) {
285 #if defined(_MSC_VER) && defined(setjmp) && \ 286 !defined(setjmp_undefined_for_msvc) 287 # pragma push_macro("setjmp") 289 # define setjmp_undefined_for_msvc 294 #if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc) 296 # pragma pop_macro("setjmp") 297 # undef setjmp_undefined_for_msvc 322 if (CI->getArgOperand(0)->getType()->getScalarType()->
348 LibInfo->getLibFunc(F->
getName(), Func) &&
349 LibInfo->hasOptimizedCodeGen(Func)) {
351 if (!CI->onlyReadsMemory())
355 if (!CI->getArgOperand(0)->getType()->isFloatingPointTy())
359 default:
return true;
360 case LibFunc_copysign:
361 case LibFunc_copysignf:
363 case LibFunc_copysignl:
377 case LibFunc_nearbyint:
378 case LibFunc_nearbyintf:
379 case LibFunc_nearbyintl:
410 TLI->getValueType(*DL, CI->getArgOperand(0)->getType(),
true);
415 if (TLI->isOperationLegalOrCustom(Opcode, EVTy))
426 }
else if (isa<BinaryOperator>(J) &&
427 J->getType()->getScalarType()->isPPC_FP128Ty()) {
430 }
else if (isa<UIToFPInst>(J) || isa<SIToFPInst>(J) ||
431 isa<FPToUIInst>(J) || isa<FPToSIInst>(J)) {
439 J->getType()->getScalarType()) &&
440 (J->getOpcode() == Instruction::UDiv ||
441 J->getOpcode() == Instruction::SDiv ||
442 J->getOpcode() == Instruction::URem ||
443 J->getOpcode() == Instruction::SRem)) {
445 }
else if (!
TM->isPPC64() &&
447 (J->getOpcode() == Instruction::Shl ||
448 J->getOpcode() == Instruction::AShr ||
449 J->getOpcode() == Instruction::LShr)) {
453 }
else if (isa<IndirectBrInst>(J) || isa<InvokeInst>(J)) {
457 if (
SI->getNumCases() + 1 >= (
unsigned)TLI->getMinimumJumpTableEntries())
462 if (J->getOpcode() == Instruction::FRem)
465 if (STI->useSoftFloat()) {
466 switch(J->getOpcode()) {
467 case Instruction::FAdd:
468 case Instruction::FSub:
469 case Instruction::FMul:
470 case Instruction::FDiv:
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:
482 for (
Value *Operand : J->operands())
489 bool PPCCTRLoops::convertToCTRLoop(
Loop *L) {
490 bool MadeChange =
false;
493 unsigned ConstTripCount = SE->getSmallConstantTripCount(L);
496 auto AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
503 if (Metrics.
NumInsts <= (6 * SchedModel.getIssueWidth()))
509 MadeChange |= convertToCTRLoop(*
I);
520 if (containsIrreducibleCFG<const BasicBlock *>(RPOT, *LI))
545 for (
auto &BB : ExitingBlocks) {
549 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
550 uint64_t TrueWeight = 0, FalseWeight = 0;
551 if (!BI->isConditional() ||
552 !BI->extractProfMetadata(TrueWeight, FalseWeight))
557 bool TrueIsExit = !L->
contains(BI->getSuccessor(0));
558 if (( TrueIsExit && FalseWeight < TrueWeight) ||
559 (!TrueIsExit && FalseWeight > TrueWeight))
565 const SCEV *ExitCount =
nullptr;
568 IE = ExitingBlocks.end();
I !=
IE; ++
I) {
569 const SCEV *EC = SE->getExitCount(L, *
I);
571 << (*I)->
getName() <<
": " << *EC <<
"\n");
572 if (isa<SCEVCouldNotCompute>(EC))
574 if (
const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) {
575 if (ConstEC->getValue()->isZero())
577 }
else if (!SE->isLoopInvariant(EC, L))
580 if (SE->getTypeSizeInBits(EC->
getType()) > (
TM->isPPC64() ? 64 : 32))
586 if (LI->getLoopFor(*
I) != L)
596 bool NotAlways =
false;
602 if (!DT->dominates(*
I, *PI)) {
616 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
617 if (!BI->isConditional())
620 CountedExitBranch = BI;
626 CountedExitBlock = *
I;
631 if (!CountedExitBlock)
640 if (!Preheader || mightUseCTR(Preheader))
656 ExitCount->
getType() != CountType)
657 ExitCount = SE->getZeroExtendExpr(ExitCount, CountType);
658 ExitCount = SE->getAddExpr(ExitCount, SE->getOne(CountType));
666 CountBuilder.CreateCall(MTCTRFunc, ECValue);
714 if (I == MBB->
begin()) {
727 unsigned Opc = I->getOpcode();
728 if (Opc == PPC::MTCTRloop || Opc == PPC::MTCTR8loop) {
735 <<
") instruction " << *I
736 <<
" clobbers CTR, invalidating " 738 << BI->getParent()->getFullName() <<
") instruction " 747 if (!CheckPreds && Preds.
empty())
755 << BI->getParent()->getFullName() <<
") instruction " 761 PIE = MBB->
pred_end(); PI != PIE; ++PI)
767 if (!Visited.
count(MBB)) {
771 }
while (!Preds.
empty());
777 MDT = &getAnalysis<MachineDominatorTree>();
784 if (!MDT->isReachableFromEntry(MBB))
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)
static void collectEphemeralValues(const Loop *L, AssumptionCache *AC, SmallPtrSetImpl< const Value *> &EphValues)
Collect a loop's ephemeral values (those used only by an assume or similar intrinsics in the loop)...
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
A parsed version of the target data layout string in and methods for querying it. ...
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Type * getSrcTy() const
Return the source type, as a convenience.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
bool hasLocalLinkage() const
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
void swapSuccessors()
Swap the successors of this branch instruction.
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
void initializePPCCTRLoopsVerifyPass(PassRegistry &)
ArrayRef< BasicBlock *>::const_iterator block_iterator
LLVM_NODISCARD bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
A Module instance is used to store all the information related to an LLVM module. ...
static bool memAddrUsesCTR(const PPCTargetMachine &TM, const Value *MemAddr)
void push_back(const T &Elt)
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
unsigned getReg() const
getReg - Returns the register number.
The main scalar evolution driver.
This class represents a function call, abstracting a target machine's calling convention.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
An immutable pass that tracks lazily created AssumptionCache objects.
BasicBlock * InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA)
InsertPreheaderForLoop - Once we discover that a loop doesn't have a preheader, this method is called...
LLVMContext & getContext() const
All values hold a context through their type.
BasicBlock * getSuccessor(unsigned i) const
STATISTIC(NumFunctions, "Total number of functions")
static IntegerType * getInt64Ty(LLVMContext &C)
Value * getCondition() const
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
ConstraintCodeVector Codes
Code - The constraint code, either the register name (in braces) or the constraint letter/number...
iterator begin()
Instruction iterator methods.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Provide an instruction scheduling machine model to CodeGen passes.
This is the base class for all instructions that perform data casts.
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
unsigned getNumOperands() const
Retuns the total number of operands.
static cl::opt< unsigned > SmallCTRLoopThreshold("min-ctr-loop-threshold", cl::init(4), cl::Hidden, cl::desc("Loops with a constant trip count smaller than " "this value will not use the count register."))
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
std::vector< Loop *>::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
FunctionPass * createPPCCTRLoops()
static cl::opt< int > CTRLoopLimit("ppc-max-ctrloop", cl::Hidden, cl::init(-1))
BlockT * getHeader() const
std::vector< Loop *>::const_iterator iterator
ppc ctr loops PowerPC CTR Loops Verify
INITIALIZE_PASS_BEGIN(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops", false, false) INITIALIZE_PASS_END(PPCCTRLoops
CHAIN = BDNZ CHAIN, DESTBB - These are used to create counter-based loops.
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
Examine each PHI in the given block and delete it if it is dead.
FunctionPass * createPPCCTRLoopsVerify()
iterator getLastNonDebugInstr()
Returns an iterator to the last non-debug instruction in the basic block, or end().
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
ConstraintPrefix Type
Type - The basic type of the constraint: input/output/clobber.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This is an important class for using LLVM in a threaded context.
Conditional or Unconditional Branch instruction.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Represent the analysis usage information of a pass.
void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI, const SmallPtrSetImpl< const Value *> &EphValues)
Add information about a block to the current state.
Value * expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I)
Insert code to directly compute the specified SCEV expression into the program.
FunctionPass class - This class is used to implement most global optimizations.
Interval::pred_iterator pred_end(Interval *I)
Class to represent integer types.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Common code between 32-bit and 64-bit PowerPC targets.
std::vector< MachineBasicBlock * >::iterator pred_iterator
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is a trivially dead instruction, delete it.
void getExitingBlocks(SmallVectorImpl< BlockT *> &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
pred_iterator pred_begin()
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool verify(const TargetRegisterInfo &TRI) const
Check that information hold by this instance make sense for the given TRI.
void initializePPCCTRLoopsPass(PassRegistry &)
std::vector< ConstraintInfo > ConstraintInfoVector
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Type * getType() const
Return the LLVM type of this SCEV expression.
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
Utility to calculate the size and a few similar metrics for a set of basic blocks.
LLVM_NODISCARD T pop_back_val()
Type * getDestTy() const
Return the destination type, as a convenience.
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
This class uses information about analyze scalars to rewrite expressions in canonical form...
LoopT * getParentLoop() const
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
bool isVector() const
Return true if this is a vector value type.
static bool asmClobbersCTR(InlineAsm *IA)
This class represents an analyzed expression in the program.
static IntegerType * getInt32Ty(LLVMContext &C)
LLVM_NODISCARD bool empty() const
StringRef getName() const
Represents a single loop in the control flow graph.
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
block_iterator block_end() const
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Wrapper class to LoopBlocksDFS that provides a standard begin()/end() interface for the DFS reverse p...
void setCondition(Value *V)
static bool clobbersCTR(const MachineInstr &MI)
std::string getFullName() const
Return a formatted string to identify this block and its parent function.
static bool isLargeIntegerTy(bool Is32Bit, Type *Ty)
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
The legacy pass manager's analysis pass to compute loop information.
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Legacy analysis pass which computes a DominatorTree.
void perform(LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
const MachineOperand & getOperand(unsigned i) const
unsigned NumInsts
Number of instructions in the analyzed blocks.
iterator_range< block_iterator > blocks() const
block_iterator block_begin() const
static bool verifyCTRBranch(MachineBasicBlock *MBB, MachineBasicBlock::iterator I)
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
static ConstraintInfoVector ParseConstraints(StringRef ConstraintString)
ParseConstraints - Split up the constraint string into the specific constraints and their prefixes...
ppc ctr PowerPC CTR Loops
This class represents a constant integer value.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.