Skip to content

Commit 225f0c1

Browse files
committed
LICM: use PatternMatch, IRBuilder in hoist BO assoc
Use PatternMatch when matching binary operators in hoistBOAssociation, in order to make it easy to extend (this change is an NFC). Also use IRBuilder when creating the new invariant instruction, so that the constant-folder has an opportunity to constant-fold the new Instruction that we desire to create (this change is not an NFC, and is a minor improvement).
1 parent 5dcea46 commit 225f0c1

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,15 +2841,18 @@ static bool hoistBOAssociation(Instruction &I, Loop &L,
28412841
auto *Preheader = L.getLoopPreheader();
28422842
assert(Preheader && "Loop is not in simplify form?");
28432843

2844-
auto *Inv = BinaryOperator::Create(Opcode, C1, C2, "invariant.op",
2845-
Preheader->getTerminator()->getIterator());
2844+
IRBuilder<> Builder(Preheader->getTerminator());
2845+
auto *Inv = Builder.CreateBinOp(Opcode, C1, C2, "invariant.op");
2846+
28462847
auto *NewBO = BinaryOperator::Create(
28472848
Opcode, LV, Inv, BO->getName() + ".reass", BO->getIterator());
28482849

28492850
// Copy NUW for ADDs if both instructions have it.
28502851
if (Opcode == Instruction::Add && BO->hasNoUnsignedWrap() &&
28512852
BO0->hasNoUnsignedWrap()) {
2852-
Inv->setHasNoUnsignedWrap(true);
2853+
// If the constant-folder didn't kick in, and a new Instruction was created.
2854+
if (auto *I = dyn_cast<Instruction>(Inv))
2855+
I->setHasNoUnsignedWrap(true);
28532856
NewBO->setHasNoUnsignedWrap(true);
28542857
}
28552858

llvm/test/Transforms/LICM/sink-foldable.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ return:
7777
define ptr @test2(i32 %j, ptr readonly %P, ptr readnone %Q) {
7878
; CHECK-LABEL: @test2(
7979
; CHECK-NEXT: entry:
80-
; CHECK-NEXT: [[INVARIANT_OP:%.*]] = add i32 1, 1
8180
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
8281
; CHECK: for.cond:
8382
; CHECK-NEXT: [[I_ADDR_0:%.*]] = phi i32 [ [[ADD_REASS:%.*]], [[IF_END:%.*]] ]
@@ -98,7 +97,7 @@ define ptr @test2(i32 %j, ptr readonly %P, ptr readnone %Q) {
9897
; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds ptr, ptr [[ADD_PTR]], i64 [[IDX2_EXT]]
9998
; CHECK-NEXT: [[L1:%.*]] = load ptr, ptr [[ARRAYIDX2]], align 8
10099
; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt ptr [[L1]], [[Q]]
101-
; CHECK-NEXT: [[ADD_REASS]] = add i32 [[I_ADDR]], [[INVARIANT_OP]]
100+
; CHECK-NEXT: [[ADD_REASS]] = add i32 [[I_ADDR]], 2
102101
; CHECK-NEXT: br i1 [[CMP2]], label [[LOOPEXIT2:%.*]], label [[FOR_COND]]
103102
; CHECK: loopexit0:
104103
; CHECK-NEXT: [[P0:%.*]] = phi ptr [ null, [[FOR_COND]] ]

0 commit comments

Comments
 (0)