2727#include " llvm/CodeGen/MachineFrameInfo.h"
2828#include " llvm/CodeGen/MachineFunctionPass.h"
2929#include " llvm/CodeGen/MachineInstrBuilder.h"
30+ #include " llvm/CodeGen/MachineLoopInfo.h"
3031#include " llvm/CodeGen/MachineMemOperand.h"
3132#include " llvm/CodeGen/MachineRegisterInfo.h"
3233#include " llvm/CodeGen/PseudoSourceValue.h"
@@ -1255,13 +1256,28 @@ MCInst PPCInstrInfo::getNop() const {
12551256}
12561257
12571258// Branch analysis.
1258- // Note: If the condition register is set to CTR or CTR8 then this is a
1259- // BDNZ (imm == 1) or BDZ (imm == 0) branch.
1259+ bool PPCInstrInfo::isCondBranchPredictable (const MachineInstr &CondBr,
1260+ const MachineLoopInfo &MLI) const {
1261+ MachineLoop *Loop = MLI.getLoopFor (CondBr.getParent ());
1262+ if (!Loop)
1263+ return false ;
1264+ return Loop->isLoopInvariant (CondBr, /* ExcludeReg=*/ 0 , /* RecursionDepth=*/ 2 );
1265+ }
1266+
12601267bool PPCInstrInfo::analyzeBranch (MachineBasicBlock &MBB,
12611268 MachineBasicBlock *&TBB,
12621269 MachineBasicBlock *&FBB,
12631270 SmallVectorImpl<MachineOperand> &Cond,
12641271 bool AllowModify) const {
1272+ return analyzeBranch (MBB, TBB, FBB, Cond, nullptr , nullptr , AllowModify);
1273+ }
1274+
1275+ // Note: If the condition register is set to CTR or CTR8 then this is a
1276+ // BDNZ (imm == 1) or BDZ (imm == 0) branch.
1277+ bool PPCInstrInfo::analyzeBranch (
1278+ MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
1279+ SmallVectorImpl<MachineOperand> &Cond, bool *IsPredictable,
1280+ const MachineLoopInfo *MLI, bool AllowModify) const {
12651281 bool isPPC64 = Subtarget.isPPC64 ();
12661282
12671283 // If the block has no terminators, it just falls into the block after it.
@@ -1303,6 +1319,8 @@ bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
13031319 TBB = LastInst.getOperand (2 ).getMBB ();
13041320 Cond.push_back (LastInst.getOperand (0 ));
13051321 Cond.push_back (LastInst.getOperand (1 ));
1322+ if (IsPredictable && MLI)
1323+ *IsPredictable = isCondBranchPredictable (LastInst, *MLI);
13061324 return false ;
13071325 } else if (LastInst.getOpcode () == PPC::BC) {
13081326 if (!LastInst.getOperand (1 ).isMBB ())
@@ -1311,6 +1329,8 @@ bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
13111329 TBB = LastInst.getOperand (1 ).getMBB ();
13121330 Cond.push_back (MachineOperand::CreateImm (PPC::PRED_BIT_SET));
13131331 Cond.push_back (LastInst.getOperand (0 ));
1332+ if (IsPredictable && MLI)
1333+ *IsPredictable = isCondBranchPredictable (LastInst, *MLI);
13141334 return false ;
13151335 } else if (LastInst.getOpcode () == PPC::BCn) {
13161336 if (!LastInst.getOperand (1 ).isMBB ())
@@ -1319,6 +1339,8 @@ bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
13191339 TBB = LastInst.getOperand (1 ).getMBB ();
13201340 Cond.push_back (MachineOperand::CreateImm (PPC::PRED_BIT_UNSET));
13211341 Cond.push_back (LastInst.getOperand (0 ));
1342+ if (IsPredictable && MLI)
1343+ *IsPredictable = isCondBranchPredictable (LastInst, *MLI);
13221344 return false ;
13231345 } else if (LastInst.getOpcode () == PPC::BDNZ8 ||
13241346 LastInst.getOpcode () == PPC::BDNZ) {
@@ -1365,6 +1387,8 @@ bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
13651387 Cond.push_back (SecondLastInst.getOperand (0 ));
13661388 Cond.push_back (SecondLastInst.getOperand (1 ));
13671389 FBB = LastInst.getOperand (0 ).getMBB ();
1390+ if (IsPredictable && MLI)
1391+ *IsPredictable = isCondBranchPredictable (SecondLastInst, *MLI);
13681392 return false ;
13691393 } else if (SecondLastInst.getOpcode () == PPC::BC &&
13701394 LastInst.getOpcode () == PPC::B) {
@@ -1375,6 +1399,8 @@ bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
13751399 Cond.push_back (MachineOperand::CreateImm (PPC::PRED_BIT_SET));
13761400 Cond.push_back (SecondLastInst.getOperand (0 ));
13771401 FBB = LastInst.getOperand (0 ).getMBB ();
1402+ if (IsPredictable && MLI)
1403+ *IsPredictable = isCondBranchPredictable (SecondLastInst, *MLI);
13781404 return false ;
13791405 } else if (SecondLastInst.getOpcode () == PPC::BCn &&
13801406 LastInst.getOpcode () == PPC::B) {
@@ -1385,6 +1411,8 @@ bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
13851411 Cond.push_back (MachineOperand::CreateImm (PPC::PRED_BIT_UNSET));
13861412 Cond.push_back (SecondLastInst.getOperand (0 ));
13871413 FBB = LastInst.getOperand (0 ).getMBB ();
1414+ if (IsPredictable && MLI)
1415+ *IsPredictable = isCondBranchPredictable (SecondLastInst, *MLI);
13881416 return false ;
13891417 } else if ((SecondLastInst.getOpcode () == PPC::BDNZ8 ||
13901418 SecondLastInst.getOpcode () == PPC::BDNZ) &&
0 commit comments