26 #define DEBUG_TYPE "ctrloops"
68 STATISTIC(NumCTRLoops,
"Number of loops converted to CTR loops");
106 bool convertToCTRLoop(
Loop *L);
119 int PPCCTRLoops::Counter = 0;
155 return new PPCCTRLoops(TM);
160 "PowerPC CTR Loops Verify",
false,
false)
166 return new PPCCTRLoopsVerify();
170 bool PPCCTRLoops::runOnFunction(
Function &
F) {
171 LI = &getAnalysis<LoopInfo>();
172 SE = &getAnalysis<ScalarEvolution>();
173 DT = &getAnalysis<DominatorTree>();
174 TD = getAnalysisIfAvailable<DataLayout>();
175 LibInfo = getAnalysisIfAvailable<TargetLibraryInfo>();
177 bool MadeChange =
false;
183 MadeChange |= convertToCTRLoop(L);
192 if (
CallInst *CI = dyn_cast<CallInst>(J)) {
193 if (
InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue())) {
196 for (
unsigned i = 0, ie = CIV.size(); i < ie; ++i) {
199 for (
unsigned j = 0, je = C.
Codes.size(); j < je; ++j)
211 if (
Function *F = CI->getCalledFunction()) {
220 #if defined(_MSC_VER) && defined(setjmp) && \
221 !defined(setjmp_undefined_for_msvc)
222 # pragma push_macro("setjmp")
224 # define setjmp_undefined_for_msvc
229 #if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)
231 # pragma pop_macro("setjmp")
232 # undef setjmp_undefined_for_msvc
257 if (CI->getArgOperand(0)->getType()->getScalarType()->
280 LibInfo->hasOptimizedCodeGen(Func)) {
282 if (!CI->onlyReadsMemory())
286 if (!CI->getArgOperand(0)->getType()->isFloatingPointTy())
290 default:
return true;
346 }
else if (isa<BinaryOperator>(J) &&
347 J->getType()->getScalarType()->isPPC_FP128Ty()) {
350 }
else if (isa<UIToFPInst>(J) || isa<SIToFPInst>(J) ||
351 isa<FPToUIInst>(J) || isa<FPToSIInst>(J)) {
361 J->getType()->getScalarType()->isIntegerTy(64) &&
362 (J->getOpcode() == Instruction::UDiv ||
363 J->getOpcode() == Instruction::SDiv ||
364 J->getOpcode() == Instruction::URem ||
365 J->getOpcode() == Instruction::SRem)) {
367 }
else if (isa<IndirectBrInst>(J) || isa<InvokeInst>(J)) {
370 }
else if (
SwitchInst *SI = dyn_cast<SwitchInst>(J)) {
384 bool PPCCTRLoops::convertToCTRLoop(
Loop *L) {
385 bool MadeChange =
false;
394 MadeChange |= convertToCTRLoop(*
I);
415 if (mightUseCTR(TT, *
I))
422 const SCEV *ExitCount = 0;
425 IE = ExitingBlocks.
end();
I !=
IE; ++
I) {
426 const SCEV *EC = SE->getExitCount(L, *
I);
427 DEBUG(
dbgs() <<
"Exit Count for " << *L <<
" from block " <<
428 (*I)->getName() <<
": " << *EC <<
"\n");
429 if (isa<SCEVCouldNotCompute>(EC))
431 if (
const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) {
432 if (ConstEC->getValue()->isZero())
434 }
else if (!SE->isLoopInvariant(EC, L))
447 bool NotAlways =
false;
453 if (!DT->dominates(*
I, *PI)) {
467 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
468 if (!BI->isConditional())
471 CountedExitBranch = BI;
477 CountedExitBlock = *
I;
482 if (!CountedExitBlock)
491 if (!Preheader || mightUseCTR(TT, Preheader))
496 DEBUG(
dbgs() <<
"Preheader for exit count: " << Preheader->
getName() <<
"\n");
507 ExitCount->
getType() != CountType)
508 ExitCount = SE->getZeroExtendExpr(ExitCount, CountType);
509 ExitCount = SE->getAddExpr(ExitCount,
510 SE->getConstant(CountType, 1));
511 Value *ECValue = SCEVE.expandCodeFor(ExitCount, CountType,
518 CountBuilder.CreateCall(MTCTRFunc, ECValue);
523 Value *NewCond = CondBuilder.CreateCall(DecFunc);
563 if (I == MBB->
begin()) {
576 unsigned Opc = I->getOpcode();
577 if (Opc == PPC::MTCTRloop || Opc == PPC::MTCTR8loop) {
585 " clobbers CTR, invalidating " <<
"BB#" <<
586 BI->getParent()->getNumber() <<
" (" <<
587 BI->getParent()->getFullName() <<
") instruction " <<
596 if (!CheckPreds && Preds.
empty())
602 DEBUG(
dbgs() <<
"Unable to find a MTCTR instruction for BB#" <<
603 BI->getParent()->getNumber() <<
" (" <<
604 BI->getParent()->getFullName() <<
") instruction " <<
610 PIE = MBB->
pred_end(); PI != PIE; ++PI)
616 if (!Visited.
count(MBB)) {
620 }
while (!Preds.
empty());
626 MDT = &getAnalysis<MachineDominatorTree>();
633 if (!MDT->isReachableFromEntry(MBB))
638 unsigned Opc =
MII->getOpcode();
639 if (Opc == PPC::BDNZ8 || Opc ==
PPC::BDNZ ||
640 Opc == PPC::BDZ8 || Opc ==
PPC::BDZ)
void push_back(const T &Elt)
const MachineFunction * getParent() const
AnalysisUsage & addPreserved()
static PassRegistry * getPassRegistry()
void swapSuccessors()
Swap the successors of this branch instruction.
void initializePPCCTRLoopsVerifyPass(PassRegistry &)
The main container class for the LLVM Intermediate Representation.
LoopInfoBase< BasicBlock, Loop >::iterator iterator
long double copysignl(long double x, long double y);
long double rintl(long double x);
iterator getFirstTerminator()
long double truncl(long double x);
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=0)
LoopT * getParentLoop() const
const Function * getParent() const
Return the enclosing method, or null if none.
double nearbyint(double x);
bool supportJumpTables() const
Return whether the target can generate code for jump tables.
static IntegerType * getInt64Ty(LLVMContext &C)
BlockT * getHeader() const
LoopInfoBase< BlockT, LoopT > * LI
long double nearbyintl(long double x);
ConstraintCodeVector Codes
StringRef getName() const
long double roundl(long double x);
long double fabsl(long double x);
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
ppc ctr loops PowerPC CTR Loops Verify
BasicBlock * InsertPreheaderForLoop(Loop *L, Pass *P)
Base class of casting instructions.
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
#define llvm_unreachable(msg)
double copysign(double x, double y);
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
MVT getScalarType() const
static cl::opt< int > CTRLoopLimit("ppc-max-ctrloop", cl::Hidden, cl::init(-1))
ID
LLVM Calling Convention Representation.
unsigned getNumOperands() const
MVT getSimpleValueType(Type *Ty, bool AllowUnknown=false) const
Return the MVT corresponding to this LLVM type. See getValueType.
bool isArch64Bit() const
Test whether the architecture is 64-bit.
STATISTIC(NumCTRLoops,"Number of loops converted to CTR loops")
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
BasicBlock * getSuccessor(unsigned i) const
FunctionPass * createPPCCTRLoopsVerify()
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
iterator getLastNonDebugInstr()
int getMinimumJumpTableEntries() const
std::vector< MachineBasicBlock * >::iterator pred_iterator
bool isPPC_FP128Ty() const
isPPC_FP128Ty - Return true if this is powerpc long double.
ppc ctr PowerPC CTR false
bundle_iterator< MachineInstr, instr_iterator > iterator
initializer< Ty > init(const Ty &Val)
BlockT * getLoopPreheader() const
LLVM Basic Block Representation.
long double floorl(long double x);
bool isOperationLegalOrCustom(unsigned Op, EVT VT) const
unsigned getIntrinsicID() const LLVM_READONLY
bool isVector() const
isVector - Return true if this is a vector value type.
const MachineOperand & getOperand(unsigned i) const
Interval::pred_iterator pred_begin(Interval *I)
bool contains(const LoopT *L) const
Interval::pred_iterator pred_end(Interval *I)
float nearbyintf(float x);
Type * getSrcTy() const
Return the source type, as a convenience.
static bool clobbersCTR(const MachineInstr *MI)
pred_iterator pred_begin()
INITIALIZE_PASS_BEGIN(PPCCTRLoops,"ppc-ctr-loops","PowerPC CTR Loops", false, false) INITIALIZE_PASS_END(PPCCTRLoops
void initializePPCCTRLoopsPass(PassRegistry &)
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
std::string getFullName() const
Return a hopefully unique identifier for this block.
FunctionPass * createPPCCTRLoops(PPCTargetMachine &TM)
Type * getDestTy() const
Return the destination type, as a convenience.
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
std::vector< BlockT * >::const_iterator block_iterator
bool count(const T &V) const
count - Return true if the element is in the set.
block_iterator block_end() const
long double ceill(long double x);
Value * getCondition() const
bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
virtual void getAnalysisUsage(AnalysisUsage &AU) const
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
const Type * getScalarType() const
bool hasLocalLinkage() const
void setCondition(Value *V)
unsigned getReg() const
getReg - Returns the register number.
LLVM Value Representation.
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=0)
BasicBlockListType::iterator iterator
block_iterator block_begin() const
std::vector< LoopT * >::const_iterator iterator
bool isArch32Bit() const
Test whether the architecture is 32-bit.
float copysignf(float x, float y);
static bool verifyCTRBranch(MachineBasicBlock *MBB, MachineBasicBlock::iterator I)
std::vector< ConstraintInfo > ConstraintInfoVector
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
ppc ctr PowerPC CTR Loops
long double sqrtl(long double x);