Lines Matching +full:block +full:- +full:offset

1 //===-- PPCBranchSelector.cpp - Emit long conditional branches ------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // target basic block. It does this in two passes; a calculation of basic block
15 //===----------------------------------------------------------------------===//
31 #define DEBUG_TYPE "ppc-branch-select"
50 // The first block number which has imprecise instruction address.
51 int FirstImpreciseBlock = -1;
53 unsigned GetAlignmentAdjustment(MachineBasicBlock &MBB, unsigned Offset);
73 INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector",
76 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
84 /// original Offset.
86 unsigned Offset) { in GetAlignmentAdjustment() argument
91 const Align ParentAlign = MBB.getParent()->getAlignment(); in GetAlignmentAdjustment()
94 return offsetToAlignment(Offset, Alignment); in GetAlignmentAdjustment()
100 return Alignment.value() + offsetToAlignment(Offset, Alignment); in GetAlignmentAdjustment()
103 /// We need to be careful about the offset of the first block in the function
106 /// two-instruction sequence to the start of the function.
124 // The end of the previous block may have extra nops if this block has an in ComputeBlockSizes()
129 auto &BS = BlockSizes[MBB.getNumber()-1]; in ComputeBlockSizes()
139 unsigned MINumBytes = TII->getInstSizeInBytes(MI); in ComputeBlockSizes()
142 if (TII->isPrefixed(MI.getOpcode())) { in ComputeBlockSizes()
167 UnalignedBytesRemaining -= std::min(UnalignedBytesRemaining, MINumBytes); in ComputeBlockSizes()
178 /// Modify the basic block align adjustment.
180 unsigned Offset = GetInitialOffset(Fn); in modifyAdjustment() local
183 auto &BS = BlockSizes[MBB.getNumber()-1]; in modifyAdjustment()
184 BS.first -= BS.second; in modifyAdjustment()
185 Offset -= BS.second; in modifyAdjustment()
187 unsigned AlignExtra = GetAlignmentAdjustment(MBB, Offset); in modifyAdjustment()
192 Offset += AlignExtra; in modifyAdjustment()
195 Offset += BlockSizes[MBB.getNumber()].first; in modifyAdjustment()
199 /// Determine the offset from the branch in Src block to the Dest block.
200 /// BrOffset is the offset of the branch instruction inside Src block.
208 if (Dest->getNumber() <= Src->getNumber()) { in computeBranchSize()
209 // If this is a backwards branch, the delta is the offset from the in computeBranchSize()
210 // start of this block to this branch, plus the sizes of all blocks in computeBranchSize()
211 // from this block to the dest. in computeBranchSize()
213 MaxAlign = std::max(MaxAlign, Src->getAlignment()); in computeBranchSize()
215 int DestBlock = Dest->getNumber(); in computeBranchSize()
217 for (unsigned i = DestBlock+1, e = Src->getNumber(); i < e; ++i) { in computeBranchSize()
219 MaxAlign = std::max(MaxAlign, Fn.getBlockNumbered(i)->getAlignment()); in computeBranchSize()
225 // Otherwise, add the size of the blocks between this block and the in computeBranchSize()
226 // dest to the number of bytes left in this block. in computeBranchSize()
227 unsigned StartBlock = Src->getNumber(); in computeBranchSize()
228 BranchSize = BlockSizes[StartBlock].first - BrOffset; in computeBranchSize()
230 MaxAlign = std::max(MaxAlign, Dest->getAlignment()); in computeBranchSize()
231 for (unsigned i = StartBlock+1, e = Dest->getNumber(); i != e; ++i) { in computeBranchSize()
233 MaxAlign = std::max(MaxAlign, Fn.getBlockNumbered(i)->getAlignment()); in computeBranchSize()
237 (Src->getNumber() >= FirstImpreciseBlock); in computeBranchSize()
241 // inline assembly. Usually it causes larger computed branch offset. in computeBranchSize()
242 // But sometimes it may also causes smaller computed branch offset in computeBranchSize()
243 // than actual branch offset. If the offset is close to the limit of in computeBranchSize()
256 // Actual offset: 0x8108 - 0x100 = 0x8008 in computeBranchSize()
257 // Computed offset: 0x8108 - 0x10c = 0x7ffc in computeBranchSize()
260 // estimated offset and actual offset. If there is an aligned block in computeBranchSize()
263 // of previous block PBB. If the estimated FSIZE is multiple of in computeBranchSize()
266 // multiple of 2^<align>, then we need (2^<align> - 4) bytes of in computeBranchSize()
267 // padding. It also means the actual branch offset is (2^<align> - 4) in computeBranchSize()
268 // larger than computed offset. Other actual FSIZE needs less padding in computeBranchSize()
269 // bytes, so causes smaller gap between actual and computed offset. in computeBranchSize()
272 // between the branch block and destination block, the estimated address in computeBranchSize()
274 // needed for a later aligned block, the actual number of padding bytes in computeBranchSize()
276 // aligned block address is less than or equal to the estimated aligned in computeBranchSize()
277 // block address. So the actual branch offset is less than or equal to in computeBranchSize()
278 // computed branch offset. in computeBranchSize()
280 // The computed offset is at most ((1 << alignment) - 4) bytes smaller in computeBranchSize()
281 // than actual offset. So we add this number to the offset for safety. in computeBranchSize()
283 BranchSize += MaxAlign.value() - 4; in computeBranchSize()
291 // Give the blocks of the function a dense, in-order, numbering. in runOnMachineFunction()
294 FirstImpreciseBlock = -1; in runOnMachineFunction()
307 // For each conditional branch, if the offset to its destination is larger in runOnMachineFunction()
308 // than the offset field allows, transform it into a long branch sequence in runOnMachineFunction()
329 if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm()) in runOnMachineFunction()
330 Dest = I->getOperand(2).getMBB(); in runOnMachineFunction()
331 else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) && in runOnMachineFunction()
332 !I->getOperand(1).isImm()) in runOnMachineFunction()
333 Dest = I->getOperand(1).getMBB(); in runOnMachineFunction()
334 else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ || in runOnMachineFunction()
335 I->getOpcode() == PPC::BDZ8 || I->getOpcode() == PPC::BDZ) && in runOnMachineFunction()
336 !I->getOperand(0).isImm()) in runOnMachineFunction()
337 Dest = I->getOperand(0).getMBB(); in runOnMachineFunction()
340 MBBStartOffset += TII->getInstSizeInBytes(*I); in runOnMachineFunction()
344 // Determine the offset from the current branch to the destination in runOnMachineFunction()
345 // block. in runOnMachineFunction()
358 if (I->getOpcode() == PPC::BCC) { in runOnMachineFunction()
363 PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm(); in runOnMachineFunction()
364 Register CRReg = I->getOperand(1).getReg(); in runOnMachineFunction()
367 BuildMI(MBB, I, dl, TII->get(PPC::BCC)) in runOnMachineFunction()
369 } else if (I->getOpcode() == PPC::BC) { in runOnMachineFunction()
370 Register CRBit = I->getOperand(0).getReg(); in runOnMachineFunction()
371 BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2); in runOnMachineFunction()
372 } else if (I->getOpcode() == PPC::BCn) { in runOnMachineFunction()
373 Register CRBit = I->getOperand(0).getReg(); in runOnMachineFunction()
374 BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2); in runOnMachineFunction()
375 } else if (I->getOpcode() == PPC::BDNZ) { in runOnMachineFunction()
376 BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2); in runOnMachineFunction()
377 } else if (I->getOpcode() == PPC::BDNZ8) { in runOnMachineFunction()
378 BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2); in runOnMachineFunction()
379 } else if (I->getOpcode() == PPC::BDZ) { in runOnMachineFunction()
380 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2); in runOnMachineFunction()
381 } else if (I->getOpcode() == PPC::BDZ8) { in runOnMachineFunction()
382 BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2); in runOnMachineFunction()
388 I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest); in runOnMachineFunction()
393 // Remember that this instruction is 8-bytes, increase the size of the in runOnMachineFunction()
394 // block by 4, remember to iterate. in runOnMachineFunction()
404 // padding-based contributions to the block sizes. in runOnMachineFunction()