llvm.org GIT mirror llvm / 7d33b4c
Handle some non-exit blocks in tail merging. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36907 91177308-0d34-0410-b5e6-96231b3b80d8 Dale Johannesen 13 years ago
2 changed file(s) with 146 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
4949 private:
5050 // Tail Merging.
5151 bool TailMergeBlocks(MachineFunction &MF);
52 bool TryMergeBlocks(MachineBasicBlock* SuccBB,
53 MachineBasicBlock* PredBB);
5254 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
5355 MachineBasicBlock *NewDest);
5456 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
5557 MachineBasicBlock::iterator BBI1);
5658
59 std::vector > MergePotentials;
5760 const MRegisterInfo *RegInfo;
5861 RegScavenger *RS;
5962 // Branch optzn.
345348 return MBB1Time < MBB2Time;
346349 }
347350
348 bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
351 // See if any of the blocks in MergePotentials (which all have a common single
352 // successor, or all have no successor) can be tail-merged. If there is a
353 // successor, any blocks in MergePotentials that are not tail-merged and
354 // are not immediately before Succ must have an unconditional branch to
355 // Succ added (but the predecessor/successor lists need no adjustment).
356 // The lone predecessor of Succ that falls through into Succ,
357 // if any, is given in PredBB.
358
359 bool BranchFolder::TryMergeBlocks(MachineBasicBlock *SuccBB,
360 MachineBasicBlock* PredBB) {
349361 MadeChange = false;
350
351 if (!EnableTailMerge) return false;
352
353 // Find blocks with no successors.
354 std::vector > MergePotentials;
355 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
356 if (I->succ_empty())
357 MergePotentials.push_back(std::make_pair(HashEndOfMBB(I), I));
358 }
359362
360363 // Sort by hash value so that blocks with identical end sequences sort
361364 // together.
370373 // If there is nothing that matches the hash of the current basic block,
371374 // give up.
372375 if (CurHash != PrevHash) {
376 if (SuccBB && CurMBB != PredBB)
377 TII->InsertBranch(*CurMBB, SuccBB, NULL, std::vector());
373378 MergePotentials.pop_back();
374379 continue;
375380 }
400405 // If we didn't find anything that has at least two instructions matching
401406 // this one, bail out.
402407 if (FoundMatch == ~0U) {
408 // Put the unconditional branch back, if we need one.
409 if (SuccBB && CurMBB != PredBB)
410 TII->InsertBranch(*CurMBB, SuccBB, NULL, std::vector());
403411 MergePotentials.pop_back();
404412 continue;
405413 }
444452 }
445453 MadeChange = true;
446454 }
447
448455 return MadeChange;
449456 }
450457
458 bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
459 MadeChange = false;
460
461 if (!EnableTailMerge) return false;
462
463 // First find blocks with no successors.
464 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
465 if (I->succ_empty())
466 MergePotentials.push_back(std::make_pair(HashEndOfMBB(I), I));
467 }
468 // See if we can do any crossjumping on those.
469 MadeChange |= TryMergeBlocks(NULL, NULL);
470
471 MergePotentials.clear();
472 // Look at blocks with two predecessors, where each predecessor has either:
473 // - a single successor, or
474 // - two successors, where successor I is reached either by ubr or fallthrough.
475 // The two-successor case where successor I is reached by cbr
476 // from both blocks is handled by the preceding case (when we consider the
477 // other, fallthough block).
478 // FIXME: The two-successor case where I is reached by cbr
479 // from one block, and by fallthrough/ubr from the other, is not handled yet.
480 // Beware that sometimes blocks are in the predecessor list, but can't really
481 // jump to the "successor" we're looking at. Tolerate this.
482
483 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
484 if (!I->succ_empty() && I->pred_size() >= 2) {
485 MachineBasicBlock *IBB = I;
486 MachineBasicBlock *PredBB = prior(I);
487 for (MachineBasicBlock::pred_iterator P = I->pred_begin(), E2 = I->pred_end();
488 P != E2; ++P) {
489 MachineBasicBlock* PBB = *P;
490 MachineBasicBlock *TBB = 0, *FBB = 0;
491 std::vector Cond;
492 // Remove the unconditional branch at the end, if any.
493 if (!TII->AnalyzeBranch(*PBB, TBB, FBB, Cond) &&
494 ((!FBB && Cond.size()==0) || // single successor
495 (!FBB && TBB!=IBB) || // cbr elsewhere, fallthrough to I
496 (FBB && FBB==IBB))) { // cbr then branch to I
497 if (TBB) {
498 TII->RemoveBranch(*PBB);
499 if (TBB!=IBB)
500 // reinsert conditional branch only, for now
501 TII->InsertBranch(*PBB, TBB, 0, Cond);
502 }
503 MergePotentials.push_back(std::make_pair(HashEndOfMBB(PBB), *P));
504 }
505 }
506 if (MergePotentials.size() >= 2)
507 MadeChange |= TryMergeBlocks(I, PredBB);
508 // Reinsert an unconditional branch if needed.
509 // The 1 below can be either an original single predecessor, or a result
510 // of removing blocks in TryMergeBlocks.
511 if (MergePotentials.size()==1 &&
512 (MergePotentials.begin())->second != PredBB)
513 TII->InsertBranch(*((MergePotentials.begin())->second), I, NULL,
514 std::vector());
515 MergePotentials.clear();
516 }
517 }
518
519 return MadeChange;
520 }
451521
452522 //===----------------------------------------------------------------------===//
453523 // Branch Optimization
0 ; RUN: llvm-as < %s | llc -march=arm -enable-tail-merge | grep bl.*baz | wc -l | grep 1
1 ; RUN: llvm-as < %s | llc -march=arm -enable-tail-merge | grep bl.*quux | wc -l | grep 1
2 ; Check that calls to baz and quux are tail-merged.
3
4 ; ModuleID = 'tail.c'
5 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64"
6 target triple = "i686-apple-darwin8"
7
8 define i32 @f(i32 %i, i32 %q) {
9 entry:
10 %i_addr = alloca i32 ; [#uses=2]
11 %q_addr = alloca i32 ; [#uses=2]
12 %retval = alloca i32, align 4 ; [#uses=1]
13 "alloca point" = bitcast i32 0 to i32 ; [#uses=0]
14 store i32 %i, i32* %i_addr
15 store i32 %q, i32* %q_addr
16 %tmp = load i32* %i_addr ; [#uses=1]
17 %tmp1 = icmp ne i32 %tmp, 0 ; [#uses=1]
18 %tmp12 = zext i1 %tmp1 to i8 ; [#uses=1]
19 %toBool = icmp ne i8 %tmp12, 0 ; [#uses=1]
20 br i1 %toBool, label %cond_true, label %cond_false
21
22 cond_true: ; preds = %entry
23 %tmp3 = call i32 (...)* @bar( ) ; [#uses=0]
24 %tmp4 = call i32 (...)* @baz( i32 5, i32 6 ) ; [#uses=0]
25 br label %cond_next
26
27 cond_false: ; preds = %entry
28 %tmp5 = call i32 (...)* @foo( ) ; [#uses=0]
29 %tmp6 = call i32 (...)* @baz( i32 5, i32 6 ) ; [#uses=0]
30 br label %cond_next
31
32 cond_next: ; preds = %cond_false, %cond_true
33 %tmp7 = load i32* %q_addr ; [#uses=1]
34 %tmp8 = icmp ne i32 %tmp7, 0 ; [#uses=1]
35 %tmp89 = zext i1 %tmp8 to i8 ; [#uses=1]
36 %toBool10 = icmp ne i8 %tmp89, 0 ; [#uses=1]
37 br i1 %toBool10, label %cond_true11, label %cond_false15
38
39 cond_true11: ; preds = %cond_next
40 %tmp13 = call i32 (...)* @foo( ) ; [#uses=0]
41 %tmp14 = call i32 (...)* @quux( i32 3, i32 4 ) ; [#uses=0]
42 br label %cond_next18
43
44 cond_false15: ; preds = %cond_next
45 %tmp16 = call i32 (...)* @bar( ) ; [#uses=0]
46 %tmp17 = call i32 (...)* @quux( i32 3, i32 4 ) ; [#uses=0]
47 br label %cond_next18
48
49 cond_next18: ; preds = %cond_false15, %cond_true11
50 %tmp19 = call i32 (...)* @bar( ) ; [#uses=0]
51 br label %return
52
53 return: ; preds = %cond_next18
54 %retval20 = load i32* %retval ; [#uses=1]
55 ret i32 %retval20
56 }
57
58 declare i32 @bar(...)
59
60 declare i32 @baz(...)
61
62 declare i32 @foo(...)
63
64 declare i32 @quux(...)