Skip to content

Commit 8c5defd

Browse files
committed
Have loop-rotate simplify instructions (yay instsimplify!) as it clones
them into the loop preheader, eliminating silly instructions like "icmp i32 0, 100" in fixed tripcount loops. This also better exposes the bigger problem with loop rotate that I'd like to fix: once this has been folded, the duplicated conditional branch *often* turns into an uncond branch. Not aggressively handling this is pessimizing later loop optimizations somethin' fierce by making "dominates all exit blocks" checks fail. llvm-svn: 123060
1 parent 75c82cb commit 8c5defd

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

llvm/lib/Transforms/Scalar/LoopRotation.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
#define DEBUG_TYPE "loop-rotate"
1515
#include "llvm/Transforms/Scalar.h"
1616
#include "llvm/Function.h"
17-
#include "llvm/Analysis/LoopPass.h"
18-
#include "llvm/Analysis/DominanceFrontier.h"
1917
#include "llvm/Analysis/CodeMetrics.h"
18+
#include "llvm/Analysis/DominanceFrontier.h"
19+
#include "llvm/Analysis/LoopPass.h"
20+
#include "llvm/Analysis/InstructionSimplify.h"
2021
#include "llvm/Analysis/ScalarEvolution.h"
2122
#include "llvm/Transforms/Utils/Local.h"
2223
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -205,9 +206,24 @@ bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
205206
// Otherwise, create a duplicate of the instruction.
206207
Instruction *C = Inst->clone();
207208

208-
C->setName(Inst->getName());
209-
C->insertBefore(LoopEntryBranch);
210-
ValueMap[Inst] = C;
209+
// Eagerly remap the operands of the instruction.
210+
RemapInstruction(C, ValueMap,
211+
RF_NoModuleLevelChanges|RF_IgnoreMissingEntries);
212+
213+
// With the operands remapped, see if the instruction constant folds or is
214+
// otherwise simplifyable. This commonly occurs because the entry from PHI
215+
// nodes allows icmps and other instructions to fold.
216+
if (Value *V = SimplifyInstruction(C)) {
217+
// If so, then delete the temporary instruction and stick the folded value
218+
// in the map.
219+
delete C;
220+
ValueMap[Inst] = V;
221+
} else {
222+
// Otherwise, stick the new instruction into the new block!
223+
C->setName(Inst->getName());
224+
C->insertBefore(LoopEntryBranch);
225+
ValueMap[Inst] = C;
226+
}
211227
}
212228

213229
// Along with all the other instructions, we just cloned OrigHeader's

llvm/test/Transforms/LoopRotate/phi-duplicate.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ for.end: ; preds = %for.cond
3434

3535
; CHECK: define void @test
3636
; CHECK-NEXT: entry:
37-
; CHECK-NEXT: icmp slt i64
38-
; CHECK-NEXT: br i1
37+
; CHECK-NEXT: br i1 true, label %bb.nph, label %for.end
3938
; CHECK-NOT: :
4039
; CHECK: bb.nph:
4140
; CHECK-NEXT: br label %for.body
4241
; CHECK-NOT: :
4342
; CHECK: for.body:
44-
; CHECK-NEXT: %j.02 = phi i64
43+
; CHECK-NEXT: %j.01 = phi i64
4544
; CHECK-NOT: phi
4645
; CHECK: ret void
4746
; CHECK-NEXT: }

0 commit comments

Comments
 (0)