blob: f0ea3e20be619d35b2bd01d088a419b93d6d7b0d [file] [log] [blame]
Richard Sandiford312425f2013-05-20 14:23:08 +00001//===-- SystemZLongBranch.cpp - Branch lengthening for SystemZ ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +000010// This pass does three things:
11// (1) try to remove compares if CC already contains the required information
12// (2) fuse compares and branches into COMPARE AND BRANCH instructions
13// (3) make sure that all branches are in range.
Richard Sandiford8a757bb2013-07-31 12:11:07 +000014//
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +000015// We do (1) here rather than earlier because some transformations can
16// change the set of available CC values and we generally want those
17// transformations to have priority over (1). This is especially true in
18// the commonest case where the CC value is used by a single in-range branch
19// instruction, since (2) will then be able to fuse the compare and the
20// branch instead.
Richard Sandiford8a757bb2013-07-31 12:11:07 +000021//
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +000022// For example, two-address NILF can sometimes be converted into
23// three-address RISBLG. NILF produces a CC value that indicates whether
24// the low word is zero, but RISBLG does not modify CC at all. On the
25// other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG.
26// The CC value produced by NILL isn't useful for our purposes, but the
27// value produced by RISBG can be used for any comparison with zero
28// (not just equality). So there are some transformations that lose
29// CC values (while still being worthwhile) and others that happen to make
30// the CC result more useful than it was originally.
31//
32// We do (2) here rather than earlier because the fused form prevents
33// predication. It also has to happen after (1).
34//
35// Doing (2) so late makes it more likely that a register will be reused
Richard Sandiford8a757bb2013-07-31 12:11:07 +000036// between the compare and the branch, but it isn't clear whether preventing
37// that would be a win or not.
38//
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +000039// There are several ways in which (3) could be done. One aggressive
Richard Sandiford8a757bb2013-07-31 12:11:07 +000040// approach is to assume that all branches are in range and successively
41// replace those that turn out not to be in range with a longer form
42// (branch relaxation). A simple implementation is to continually walk
43// through the function relaxing branches until no more changes are
44// needed and a fixed point is reached. However, in the pathological
45// worst case, this implementation is quadratic in the number of blocks;
46// relaxing branch N can make branch N-1 go out of range, which in turn
47// can make branch N-2 go out of range, and so on.
Richard Sandiford312425f2013-05-20 14:23:08 +000048//
49// An alternative approach is to assume that all branches must be
50// converted to their long forms, then reinstate the short forms of
51// branches that, even under this pessimistic assumption, turn out to be
52// in range (branch shortening). This too can be implemented as a function
53// walk that is repeated until a fixed point is reached. In general,
54// the result of shortening is not as good as that of relaxation, and
55// shortening is also quadratic in the worst case; shortening branch N
56// can bring branch N-1 in range of the short form, which in turn can do
57// the same for branch N-2, and so on. The main advantage of shortening
58// is that each walk through the function produces valid code, so it is
59// possible to stop at any point after the first walk. The quadraticness
60// could therefore be handled with a maximum pass count, although the
61// question then becomes: what maximum count should be used?
62//
63// On SystemZ, long branches are only needed for functions bigger than 64k,
64// which are relatively rare to begin with, and the long branch sequences
65// are actually relatively cheap. It therefore doesn't seem worth spending
66// much compilation time on the problem. Instead, the approach we take is:
67//
Richard Sandiford03528f32013-05-22 09:57:57 +000068// (1) Work out the address that each block would have if no branches
69// need relaxing. Exit the pass early if all branches are in range
70// according to this assumption.
71//
72// (2) Work out the address that each block would have if all branches
73// need relaxing.
74//
75// (3) Walk through the block calculating the final address of each instruction
76// and relaxing those that need to be relaxed. For backward branches,
77// this check uses the final address of the target block, as calculated
78// earlier in the walk. For forward branches, this check uses the
79// address of the target block that was calculated in (2). Both checks
80// give a conservatively-correct range.
Richard Sandiford312425f2013-05-20 14:23:08 +000081//
82//===----------------------------------------------------------------------===//
83
84#define DEBUG_TYPE "systemz-long-branch"
85
86#include "SystemZTargetMachine.h"
87#include "llvm/ADT/Statistic.h"
88#include "llvm/CodeGen/MachineFunctionPass.h"
89#include "llvm/CodeGen/MachineInstrBuilder.h"
90#include "llvm/IR/Function.h"
91#include "llvm/Support/CommandLine.h"
92#include "llvm/Support/MathExtras.h"
93#include "llvm/Target/TargetInstrInfo.h"
94#include "llvm/Target/TargetMachine.h"
95#include "llvm/Target/TargetRegisterInfo.h"
96
97using namespace llvm;
98
99STATISTIC(LongBranches, "Number of long branches.");
100
101namespace {
102 typedef MachineBasicBlock::iterator Iter;
103
104 // Represents positional information about a basic block.
105 struct MBBInfo {
Richard Sandiford03528f32013-05-22 09:57:57 +0000106 // The address that we currently assume the block has.
Richard Sandiford312425f2013-05-20 14:23:08 +0000107 uint64_t Address;
108
109 // The size of the block in bytes, excluding terminators.
110 // This value never changes.
111 uint64_t Size;
112
113 // The minimum alignment of the block, as a log2 value.
114 // This value never changes.
115 unsigned Alignment;
116
117 // The number of terminators in this block. This value never changes.
118 unsigned NumTerminators;
119
120 MBBInfo()
121 : Address(0), Size(0), Alignment(0), NumTerminators(0) {}
122 };
123
124 // Represents the state of a block terminator.
125 struct TerminatorInfo {
126 // If this terminator is a relaxable branch, this points to the branch
127 // instruction, otherwise it is null.
128 MachineInstr *Branch;
129
Richard Sandiford03528f32013-05-22 09:57:57 +0000130 // The address that we currently assume the terminator has.
Richard Sandiford312425f2013-05-20 14:23:08 +0000131 uint64_t Address;
132
133 // The current size of the terminator in bytes.
134 uint64_t Size;
135
136 // If Branch is nonnull, this is the number of the target block,
137 // otherwise it is unused.
138 unsigned TargetBlock;
139
140 // If Branch is nonnull, this is the length of the longest relaxed form,
141 // otherwise it is zero.
142 unsigned ExtraRelaxSize;
143
144 TerminatorInfo() : Branch(0), Size(0), TargetBlock(0), ExtraRelaxSize(0) {}
145 };
146
147 // Used to keep track of the current position while iterating over the blocks.
148 struct BlockPosition {
Richard Sandiford03528f32013-05-22 09:57:57 +0000149 // The address that we assume this position has.
Richard Sandiford312425f2013-05-20 14:23:08 +0000150 uint64_t Address;
151
152 // The number of low bits in Address that are known to be the same
153 // as the runtime address.
154 unsigned KnownBits;
155
156 BlockPosition(unsigned InitialAlignment)
157 : Address(0), KnownBits(InitialAlignment) {}
158 };
159
160 class SystemZLongBranch : public MachineFunctionPass {
161 public:
162 static char ID;
163 SystemZLongBranch(const SystemZTargetMachine &tm)
Bill Wendling637d97d2013-06-07 20:42:15 +0000164 : MachineFunctionPass(ID), TII(0) {}
Richard Sandiford312425f2013-05-20 14:23:08 +0000165
166 virtual const char *getPassName() const {
167 return "SystemZ Long Branch";
168 }
169
170 bool runOnMachineFunction(MachineFunction &F);
171
172 private:
173 void skipNonTerminators(BlockPosition &Position, MBBInfo &Block);
174 void skipTerminator(BlockPosition &Position, TerminatorInfo &Terminator,
175 bool AssumeRelaxed);
176 TerminatorInfo describeTerminator(MachineInstr *MI);
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000177 bool optimizeCompareZero(MachineInstr *PrevCCSetter, MachineInstr *Compare);
Richard Sandiford8a757bb2013-07-31 12:11:07 +0000178 bool fuseCompareAndBranch(MachineInstr *Compare);
Richard Sandiford312425f2013-05-20 14:23:08 +0000179 uint64_t initMBBInfo();
Richard Sandiford03528f32013-05-22 09:57:57 +0000180 bool mustRelaxBranch(const TerminatorInfo &Terminator, uint64_t Address);
Richard Sandiford312425f2013-05-20 14:23:08 +0000181 bool mustRelaxABranch();
182 void setWorstCaseAddresses();
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000183 void splitCompareBranch(MachineInstr *MI, unsigned CompareOpcode);
Richard Sandiford312425f2013-05-20 14:23:08 +0000184 void relaxBranch(TerminatorInfo &Terminator);
185 void relaxBranches();
186
187 const SystemZInstrInfo *TII;
188 MachineFunction *MF;
189 SmallVector<MBBInfo, 16> MBBs;
190 SmallVector<TerminatorInfo, 16> Terminators;
191 };
192
193 char SystemZLongBranch::ID = 0;
194
195 const uint64_t MaxBackwardRange = 0x10000;
196 const uint64_t MaxForwardRange = 0xfffe;
197} // end of anonymous namespace
198
199FunctionPass *llvm::createSystemZLongBranchPass(SystemZTargetMachine &TM) {
200 return new SystemZLongBranch(TM);
201}
202
203// Position describes the state immediately before Block. Update Block
204// accordingly and move Position to the end of the block's non-terminator
205// instructions.
206void SystemZLongBranch::skipNonTerminators(BlockPosition &Position,
207 MBBInfo &Block) {
208 if (Block.Alignment > Position.KnownBits) {
209 // When calculating the address of Block, we need to conservatively
210 // assume that Block had the worst possible misalignment.
211 Position.Address += ((uint64_t(1) << Block.Alignment) -
212 (uint64_t(1) << Position.KnownBits));
213 Position.KnownBits = Block.Alignment;
214 }
215
216 // Align the addresses.
217 uint64_t AlignMask = (uint64_t(1) << Block.Alignment) - 1;
218 Position.Address = (Position.Address + AlignMask) & ~AlignMask;
219
220 // Record the block's position.
221 Block.Address = Position.Address;
222
223 // Move past the non-terminators in the block.
224 Position.Address += Block.Size;
225}
226
227// Position describes the state immediately before Terminator.
228// Update Terminator accordingly and move Position past it.
229// Assume that Terminator will be relaxed if AssumeRelaxed.
230void SystemZLongBranch::skipTerminator(BlockPosition &Position,
231 TerminatorInfo &Terminator,
232 bool AssumeRelaxed) {
233 Terminator.Address = Position.Address;
234 Position.Address += Terminator.Size;
235 if (AssumeRelaxed)
236 Position.Address += Terminator.ExtraRelaxSize;
237}
238
239// Return a description of terminator instruction MI.
240TerminatorInfo SystemZLongBranch::describeTerminator(MachineInstr *MI) {
241 TerminatorInfo Terminator;
242 Terminator.Size = TII->getInstSizeInBytes(MI);
243 if (MI->isConditionalBranch() || MI->isUnconditionalBranch()) {
Richard Sandiford312425f2013-05-20 14:23:08 +0000244 switch (MI->getOpcode()) {
245 case SystemZ::J:
246 // Relaxes to JG, which is 2 bytes longer.
Richard Sandiford312425f2013-05-20 14:23:08 +0000247 Terminator.ExtraRelaxSize = 2;
248 break;
249 case SystemZ::BRC:
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000250 // Relaxes to BRCL, which is 2 bytes longer.
Richard Sandiford312425f2013-05-20 14:23:08 +0000251 Terminator.ExtraRelaxSize = 2;
252 break;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000253 case SystemZ::CRJ:
254 // Relaxes to a CR/BRCL sequence, which is 2 bytes longer.
255 Terminator.ExtraRelaxSize = 2;
256 break;
257 case SystemZ::CGRJ:
258 // Relaxes to a CGR/BRCL sequence, which is 4 bytes longer.
259 Terminator.ExtraRelaxSize = 4;
260 break;
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000261 case SystemZ::CIJ:
262 case SystemZ::CGIJ:
263 // Relaxes to a C(G)HI/BRCL sequence, which is 4 bytes longer.
264 Terminator.ExtraRelaxSize = 4;
265 break;
Richard Sandiford312425f2013-05-20 14:23:08 +0000266 default:
267 llvm_unreachable("Unrecognized branch instruction");
268 }
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000269 Terminator.Branch = MI;
270 Terminator.TargetBlock =
271 TII->getBranchInfo(MI).Target->getMBB()->getNumber();
Richard Sandiford312425f2013-05-20 14:23:08 +0000272 }
273 return Terminator;
274}
275
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000276// Return true if CC is live out of MBB.
277static bool isCCLiveOut(MachineBasicBlock *MBB) {
278 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
279 SE = MBB->succ_end(); SI != SE; ++SI)
280 if ((*SI)->isLiveIn(SystemZ::CC))
281 return true;
282 return false;
283}
284
Richard Sandiford8a757bb2013-07-31 12:11:07 +0000285// Return true if CC is live after MBBI.
286static bool isCCLiveAfter(MachineBasicBlock::iterator MBBI,
287 const TargetRegisterInfo *TRI) {
288 if (MBBI->killsRegister(SystemZ::CC, TRI))
289 return false;
290
291 MachineBasicBlock *MBB = MBBI->getParent();
292 MachineBasicBlock::iterator MBBE = MBB->end();
293 for (++MBBI; MBBI != MBBE; ++MBBI) {
294 if (MBBI->readsRegister(SystemZ::CC, TRI))
295 return true;
296 if (MBBI->definesRegister(SystemZ::CC, TRI))
297 return false;
298 }
299
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000300 return isCCLiveOut(MBB);
301}
Richard Sandiford8a757bb2013-07-31 12:11:07 +0000302
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000303// Return true if all uses of the CC value produced by MBBI could make do
304// with the CC values in ReusableCCMask. When returning true, point AlterMasks
305// to the "CC valid" and "CC mask" operands for each condition.
306static bool canRestrictCCMask(MachineBasicBlock::iterator MBBI,
307 unsigned ReusableCCMask,
308 SmallVectorImpl<MachineOperand *> &AlterMasks,
309 const TargetRegisterInfo *TRI) {
310 MachineBasicBlock *MBB = MBBI->getParent();
311 MachineBasicBlock::iterator MBBE = MBB->end();
312 for (++MBBI; MBBI != MBBE; ++MBBI) {
313 if (MBBI->readsRegister(SystemZ::CC, TRI)) {
314 // Fail if this isn't a use of CC that we understand.
315 unsigned MBBIFlags = MBBI->getDesc().TSFlags;
316 unsigned FirstOpNum;
317 if (MBBIFlags & SystemZII::CCMaskFirst)
318 FirstOpNum = 0;
319 else if (MBBIFlags & SystemZII::CCMaskLast)
320 FirstOpNum = MBBI->getNumExplicitOperands() - 2;
321 else
322 return false;
323
324 // Check whether the instruction predicate treats all CC values
325 // outside of ReusableCCMask in the same way. In that case it
326 // doesn't matter what those CC values mean.
327 unsigned CCValid = MBBI->getOperand(FirstOpNum).getImm();
328 unsigned CCMask = MBBI->getOperand(FirstOpNum + 1).getImm();
329 unsigned OutValid = ~ReusableCCMask & CCValid;
330 unsigned OutMask = ~ReusableCCMask & CCMask;
331 if (OutMask != 0 && OutMask != OutValid)
332 return false;
333
334 AlterMasks.push_back(&MBBI->getOperand(FirstOpNum));
335 AlterMasks.push_back(&MBBI->getOperand(FirstOpNum + 1));
336
337 // Succeed if this was the final use of the CC value.
338 if (MBBI->killsRegister(SystemZ::CC, TRI))
339 return true;
340 }
341 // Succeed if the instruction redefines CC.
342 if (MBBI->definesRegister(SystemZ::CC, TRI))
343 return true;
344 }
345 // Fail if there are other uses of CC that we didn't see.
346 return !isCCLiveOut(MBB);
347}
348
349// Try to make Compare redundant with PrevCCSetter, the previous setter of CC,
350// by looking for cases where Compare compares the result of PrevCCSetter
351// against zero. Return true on success and if Compare can therefore
352// be deleted.
353bool SystemZLongBranch::optimizeCompareZero(MachineInstr *PrevCCSetter,
354 MachineInstr *Compare) {
355 if (MF->getTarget().getOptLevel() == CodeGenOpt::None)
356 return false;
357
358 // Check whether this is a comparison against zero.
359 if (Compare->getNumExplicitOperands() != 2 ||
360 !Compare->getOperand(1).isImm() ||
361 Compare->getOperand(1).getImm() != 0)
362 return false;
363
364 // See which compare-style condition codes are available after PrevCCSetter.
365 unsigned PrevFlags = PrevCCSetter->getDesc().TSFlags;
366 unsigned ReusableCCMask = 0;
367 if (PrevFlags & SystemZII::CCHasZero)
368 ReusableCCMask |= SystemZ::CCMASK_CMP_EQ;
369
370 // For unsigned comparisons with zero, only equality makes sense.
371 unsigned CompareFlags = Compare->getDesc().TSFlags;
372 if (!(CompareFlags & SystemZII::IsLogical) &&
373 (PrevFlags & SystemZII::CCHasOrder))
374 ReusableCCMask |= SystemZ::CCMASK_CMP_LT | SystemZ::CCMASK_CMP_GT;
375
376 if (ReusableCCMask == 0)
377 return false;
378
379 // Make sure that PrevCCSetter sets the value being compared.
380 unsigned SrcReg = Compare->getOperand(0).getReg();
381 unsigned SrcSubReg = Compare->getOperand(0).getSubReg();
382 if (!PrevCCSetter->getOperand(0).isReg() ||
383 !PrevCCSetter->getOperand(0).isDef() ||
384 PrevCCSetter->getOperand(0).getReg() != SrcReg ||
385 PrevCCSetter->getOperand(0).getSubReg() != SrcSubReg)
386 return false;
387
388 // Make sure that SrcReg survives until Compare.
389 MachineBasicBlock::iterator MBBI = PrevCCSetter, MBBE = Compare;
390 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
391 for (++MBBI; MBBI != MBBE; ++MBBI)
392 if (MBBI->modifiesRegister(SrcReg, TRI))
393 return false;
394
395 // See whether all uses of Compare's CC value could make do with
396 // the values produced by PrevCCSetter.
397 SmallVector<MachineOperand *, 4> AlterMasks;
398 if (!canRestrictCCMask(Compare, ReusableCCMask, AlterMasks, TRI))
399 return false;
400
401 // Alter the CC masks that canRestrictCCMask says need to be altered.
402 unsigned CCValues = SystemZII::getCCValues(PrevFlags);
403 assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues");
404 for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) {
405 AlterMasks[I]->setImm(CCValues);
406 unsigned CCMask = AlterMasks[I + 1]->getImm();
407 if (CCMask & ~ReusableCCMask)
408 AlterMasks[I + 1]->setImm((CCMask & ReusableCCMask) |
409 (CCValues & ~ReusableCCMask));
410 }
411
412 // CC is now live after PrevCCSetter.
413 int CCDef = PrevCCSetter->findRegisterDefOperandIdx(SystemZ::CC, false,
414 true, TRI);
415 assert(CCDef >= 0 && "Couldn't find CC set");
416 PrevCCSetter->getOperand(CCDef).setIsDead(false);
417
418 // Clear any intervening kills of CC.
419 MBBI = PrevCCSetter;
420 for (++MBBI; MBBI != MBBE; ++MBBI)
421 MBBI->clearRegisterKills(SystemZ::CC, TRI);
422
423 return true;
Richard Sandiford8a757bb2013-07-31 12:11:07 +0000424}
425
426// Try to fuse compare instruction Compare into a later branch. Return
427// true on success and if Compare is therefore redundant.
428bool SystemZLongBranch::fuseCompareAndBranch(MachineInstr *Compare) {
429 if (MF->getTarget().getOptLevel() == CodeGenOpt::None)
430 return false;
431
432 unsigned FusedOpcode = TII->getCompareAndBranch(Compare->getOpcode(),
433 Compare);
434 if (!FusedOpcode)
435 return false;
436
437 unsigned SrcReg = Compare->getOperand(0).getReg();
438 unsigned SrcReg2 = (Compare->getOperand(1).isReg() ?
439 Compare->getOperand(1).getReg() : 0);
440 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
441 MachineBasicBlock *MBB = Compare->getParent();
442 MachineBasicBlock::iterator MBBI = Compare, MBBE = MBB->end();
443 for (++MBBI; MBBI != MBBE; ++MBBI) {
444 if (MBBI->getOpcode() == SystemZ::BRC && !isCCLiveAfter(MBBI, TRI)) {
445 // Read the branch mask and target.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000446 MachineOperand CCMask(MBBI->getOperand(1));
447 MachineOperand Target(MBBI->getOperand(2));
448 assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 &&
449 "Invalid condition-code mask for integer comparison");
Richard Sandiford8a757bb2013-07-31 12:11:07 +0000450
451 // Clear out all current operands.
452 int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI);
453 assert(CCUse >= 0 && "BRC must use CC");
454 MBBI->RemoveOperand(CCUse);
Richard Sandiford3d768e32013-07-31 12:30:20 +0000455 MBBI->RemoveOperand(2);
Richard Sandiford8a757bb2013-07-31 12:11:07 +0000456 MBBI->RemoveOperand(1);
457 MBBI->RemoveOperand(0);
458
459 // Rebuild MBBI as a fused compare and branch.
460 MBBI->setDesc(TII->get(FusedOpcode));
461 MachineInstrBuilder(*MBB->getParent(), MBBI)
462 .addOperand(Compare->getOperand(0))
463 .addOperand(Compare->getOperand(1))
464 .addOperand(CCMask)
465 .addOperand(Target);
466
467 // Clear any intervening kills of SrcReg and SrcReg2.
468 MBBI = Compare;
469 for (++MBBI; MBBI != MBBE; ++MBBI) {
470 MBBI->clearRegisterKills(SrcReg, TRI);
471 if (SrcReg2)
472 MBBI->clearRegisterKills(SrcReg2, TRI);
473 }
474 return true;
475 }
476
477 // Stop if we find another reference to CC before a branch.
478 if (MBBI->readsRegister(SystemZ::CC, TRI) ||
479 MBBI->modifiesRegister(SystemZ::CC, TRI))
480 return false;
481
482 // Stop if we find another assignment to the registers before the branch.
483 if (MBBI->modifiesRegister(SrcReg, TRI) ||
484 (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI)))
485 return false;
486 }
487 return false;
488}
489
Richard Sandiford312425f2013-05-20 14:23:08 +0000490// Fill MBBs and Terminators, setting the addresses on the assumption
491// that no branches need relaxation. Return the size of the function under
492// this assumption.
493uint64_t SystemZLongBranch::initMBBInfo() {
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000494 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
495
Richard Sandiford312425f2013-05-20 14:23:08 +0000496 MF->RenumberBlocks();
497 unsigned NumBlocks = MF->size();
498
499 MBBs.clear();
500 MBBs.resize(NumBlocks);
501
502 Terminators.clear();
503 Terminators.reserve(NumBlocks);
504
505 BlockPosition Position(MF->getAlignment());
506 for (unsigned I = 0; I < NumBlocks; ++I) {
507 MachineBasicBlock *MBB = MF->getBlockNumbered(I);
508 MBBInfo &Block = MBBs[I];
509
510 // Record the alignment, for quick access.
511 Block.Alignment = MBB->getAlignment();
512
513 // Calculate the size of the fixed part of the block.
514 MachineBasicBlock::iterator MI = MBB->begin();
515 MachineBasicBlock::iterator End = MBB->end();
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000516 MachineInstr *PrevCCSetter = 0;
Richard Sandiford312425f2013-05-20 14:23:08 +0000517 while (MI != End && !MI->isTerminator()) {
Richard Sandiford8a757bb2013-07-31 12:11:07 +0000518 MachineInstr *Current = MI;
Richard Sandiford312425f2013-05-20 14:23:08 +0000519 ++MI;
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000520 if (Current->isCompare()) {
521 if ((PrevCCSetter && optimizeCompareZero(PrevCCSetter, Current)) ||
522 fuseCompareAndBranch(Current)) {
523 Current->removeFromParent();
524 continue;
525 }
526 }
527 if (Current->modifiesRegister(SystemZ::CC, TRI))
528 PrevCCSetter = Current;
529 Block.Size += TII->getInstSizeInBytes(Current);
Richard Sandiford312425f2013-05-20 14:23:08 +0000530 }
531 skipNonTerminators(Position, Block);
532
533 // Add the terminators.
534 while (MI != End) {
535 if (!MI->isDebugValue()) {
536 assert(MI->isTerminator() && "Terminator followed by non-terminator");
537 Terminators.push_back(describeTerminator(MI));
538 skipTerminator(Position, Terminators.back(), false);
539 ++Block.NumTerminators;
540 }
541 ++MI;
542 }
543 }
544
545 return Position.Address;
546}
547
Richard Sandiford03528f32013-05-22 09:57:57 +0000548// Return true if, under current assumptions, Terminator would need to be
549// relaxed if it were placed at address Address.
550bool SystemZLongBranch::mustRelaxBranch(const TerminatorInfo &Terminator,
551 uint64_t Address) {
Richard Sandiford312425f2013-05-20 14:23:08 +0000552 if (!Terminator.Branch)
553 return false;
554
555 const MBBInfo &Target = MBBs[Terminator.TargetBlock];
Richard Sandiford03528f32013-05-22 09:57:57 +0000556 if (Address >= Target.Address) {
557 if (Address - Target.Address <= MaxBackwardRange)
Richard Sandiford312425f2013-05-20 14:23:08 +0000558 return false;
559 } else {
Richard Sandiford03528f32013-05-22 09:57:57 +0000560 if (Target.Address - Address <= MaxForwardRange)
Richard Sandiford312425f2013-05-20 14:23:08 +0000561 return false;
562 }
563
564 return true;
565}
566
567// Return true if, under current assumptions, any terminator needs
568// to be relaxed.
569bool SystemZLongBranch::mustRelaxABranch() {
Craig Topperaf0dea12013-07-04 01:31:24 +0000570 for (SmallVectorImpl<TerminatorInfo>::iterator TI = Terminators.begin(),
Richard Sandiford312425f2013-05-20 14:23:08 +0000571 TE = Terminators.end(); TI != TE; ++TI)
Richard Sandiford03528f32013-05-22 09:57:57 +0000572 if (mustRelaxBranch(*TI, TI->Address))
Richard Sandiford312425f2013-05-20 14:23:08 +0000573 return true;
574 return false;
575}
576
577// Set the address of each block on the assumption that all branches
578// must be long.
579void SystemZLongBranch::setWorstCaseAddresses() {
580 SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin();
581 BlockPosition Position(MF->getAlignment());
Craig Topperaf0dea12013-07-04 01:31:24 +0000582 for (SmallVectorImpl<MBBInfo>::iterator BI = MBBs.begin(), BE = MBBs.end();
Richard Sandiford312425f2013-05-20 14:23:08 +0000583 BI != BE; ++BI) {
584 skipNonTerminators(Position, *BI);
585 for (unsigned BTI = 0, BTE = BI->NumTerminators; BTI != BTE; ++BTI) {
586 skipTerminator(Position, *TI, true);
587 ++TI;
588 }
589 }
590}
591
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000592// Split MI into the comparison given by CompareOpcode followed
593// a BRCL on the result.
594void SystemZLongBranch::splitCompareBranch(MachineInstr *MI,
595 unsigned CompareOpcode) {
596 MachineBasicBlock *MBB = MI->getParent();
597 DebugLoc DL = MI->getDebugLoc();
598 BuildMI(*MBB, MI, DL, TII->get(CompareOpcode))
599 .addOperand(MI->getOperand(0))
600 .addOperand(MI->getOperand(1));
601 MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL))
Richard Sandiford3d768e32013-07-31 12:30:20 +0000602 .addImm(SystemZ::CCMASK_ICMP)
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000603 .addOperand(MI->getOperand(2))
604 .addOperand(MI->getOperand(3));
605 // The implicit use of CC is a killing use.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000606 BRCL->addRegisterKilled(SystemZ::CC, &TII->getRegisterInfo());
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000607 MI->eraseFromParent();
608}
609
Richard Sandiford312425f2013-05-20 14:23:08 +0000610// Relax the branch described by Terminator.
611void SystemZLongBranch::relaxBranch(TerminatorInfo &Terminator) {
612 MachineInstr *Branch = Terminator.Branch;
613 switch (Branch->getOpcode()) {
Richard Sandiford3b105a02013-05-21 08:48:24 +0000614 case SystemZ::J:
615 Branch->setDesc(TII->get(SystemZ::JG));
616 break;
617 case SystemZ::BRC:
618 Branch->setDesc(TII->get(SystemZ::BRCL));
619 break;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000620 case SystemZ::CRJ:
621 splitCompareBranch(Branch, SystemZ::CR);
622 break;
623 case SystemZ::CGRJ:
624 splitCompareBranch(Branch, SystemZ::CGR);
625 break;
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000626 case SystemZ::CIJ:
627 splitCompareBranch(Branch, SystemZ::CHI);
628 break;
629 case SystemZ::CGIJ:
630 splitCompareBranch(Branch, SystemZ::CGHI);
631 break;
Richard Sandiford3b105a02013-05-21 08:48:24 +0000632 default:
633 llvm_unreachable("Unrecognized branch");
634 }
Richard Sandiford312425f2013-05-20 14:23:08 +0000635
636 Terminator.Size += Terminator.ExtraRelaxSize;
637 Terminator.ExtraRelaxSize = 0;
638 Terminator.Branch = 0;
639
640 ++LongBranches;
641}
642
Richard Sandiford03528f32013-05-22 09:57:57 +0000643// Run a shortening pass and relax any branches that need to be relaxed.
Richard Sandiford312425f2013-05-20 14:23:08 +0000644void SystemZLongBranch::relaxBranches() {
Richard Sandiford03528f32013-05-22 09:57:57 +0000645 SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin();
646 BlockPosition Position(MF->getAlignment());
Craig Topperaf0dea12013-07-04 01:31:24 +0000647 for (SmallVectorImpl<MBBInfo>::iterator BI = MBBs.begin(), BE = MBBs.end();
Richard Sandiford03528f32013-05-22 09:57:57 +0000648 BI != BE; ++BI) {
649 skipNonTerminators(Position, *BI);
650 for (unsigned BTI = 0, BTE = BI->NumTerminators; BTI != BTE; ++BTI) {
651 assert(Position.Address <= TI->Address &&
652 "Addresses shouldn't go forwards");
653 if (mustRelaxBranch(*TI, Position.Address))
654 relaxBranch(*TI);
655 skipTerminator(Position, *TI, false);
656 ++TI;
657 }
658 }
Richard Sandiford312425f2013-05-20 14:23:08 +0000659}
660
661bool SystemZLongBranch::runOnMachineFunction(MachineFunction &F) {
Bill Wendling637d97d2013-06-07 20:42:15 +0000662 TII = static_cast<const SystemZInstrInfo *>(F.getTarget().getInstrInfo());
Richard Sandiford312425f2013-05-20 14:23:08 +0000663 MF = &F;
664 uint64_t Size = initMBBInfo();
665 if (Size <= MaxForwardRange || !mustRelaxABranch())
666 return false;
667
668 setWorstCaseAddresses();
669 relaxBranches();
670 return true;
671}