Skip to content

Commit 8e8f1d7

Browse files
committed
LICM: generalize further strengthening check
1 parent 2bc99bf commit 8e8f1d7

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,13 +2803,14 @@ static bool hoistMulAddAssociation(Instruction &I, Loop &L,
28032803

28042804
/// Reassociate associative binary expressions of the form
28052805
///
2806-
/// 1. "(LV op C1) op C2" ==> "LV op (C1 op C2)" if op is an associative BinOp
2807-
/// 2. "(C1 op LV) op C2" ==> "LV op (C1 op C2)" if op is a commutative BinOp
2808-
/// 3. "C2 op (C1 op LV)" ==> "(C2 op C1) op LV" if op an associative BinOp
2809-
/// 4. "C2 op (LV op C1)" ==> "(C2 op C1) op LV" if op is a commutative BinOp
2806+
/// 1. "(LV op C1) op C2" ==> "LV op (C1 op C2)"
2807+
/// 2. "(C1 op LV) op C2" ==> "LV op (C1 op C2)"
2808+
/// 3. "C2 op (C1 op LV)" ==> "LV op (C1 op C2)"
2809+
/// 4. "C2 op (LV op C1)" ==> "LV op (C1 op C2)"
28102810
///
2811-
/// where LV is a loop variant, and C1 and C2 are loop invariants that we want
2812-
/// to hoist.
2811+
/// where op is an associative BinOp, LV is a loop variant, and C1 and C2 are
2812+
/// loop invariants that we want to hoist, noting that associativity implies
2813+
/// commutativity.
28132814
static bool hoistBOAssociation(Instruction &I, Loop &L,
28142815
ICFLoopSafetyInfo &SafetyInfo,
28152816
MemorySSAUpdater &MSSAU, AssumptionCache *AC,
@@ -2833,35 +2834,21 @@ static bool hoistBOAssociation(Instruction &I, Loop &L,
28332834
Value *C1 = BO0->getOperand(1);
28342835
Value *C2 = BO->getOperand(!BinOpInRHS);
28352836

2836-
if (!L.isLoopInvariant(C2))
2837-
return false;
2838-
if (!L.isLoopInvariant(LV) && L.isLoopInvariant(C1)) {
2839-
if (BinOpInRHS)
2840-
assert(BO0->isCommutative() && "Associativity implies commutativity");
2841-
} else if (L.isLoopInvariant(LV) && !L.isLoopInvariant(C1)) {
2842-
if (!BinOpInRHS)
2843-
assert(BO0->isCommutative() && "Associativity implies commutativity");
2837+
assert(BO->isCommutative() && BO0->isCommutative() &&
2838+
"Associativity implies commutativity");
2839+
if (L.isLoopInvariant(LV) && !L.isLoopInvariant(C1))
28442840
std::swap(LV, C1);
2845-
} else {
2841+
if (L.isLoopInvariant(LV) || !L.isLoopInvariant(C1) || !L.isLoopInvariant(C2))
28462842
return false;
2847-
}
28482843

28492844
auto *Preheader = L.getLoopPreheader();
28502845
assert(Preheader && "Loop is not in simplify form?");
28512846

2852-
// To create C2 op C1, instead of C1 op C2.
2853-
if (BinOpInRHS)
2854-
std::swap(C1, C2);
2855-
28562847
IRBuilder<> Builder(Preheader->getTerminator());
28572848
auto *Inv = Builder.CreateBinOp(Opcode, C1, C2, "invariant.op");
28582849

2859-
auto *NewBO =
2860-
BinOpInRHS
2861-
? BinaryOperator::Create(Opcode, Inv, LV, BO->getName() + ".reass",
2862-
BO->getIterator())
2863-
: BinaryOperator::Create(Opcode, LV, Inv, BO->getName() + ".reass",
2864-
BO->getIterator());
2850+
auto *NewBO = BinaryOperator::Create(
2851+
Opcode, LV, Inv, BO->getName() + ".reass", BO->getIterator());
28652852

28662853
// Copy NUW for ADDs if both instructions have it.
28672854
if (Opcode == Instruction::Add && BO->hasNoUnsignedWrap() &&

llvm/test/Transforms/LICM/hoist-binop.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ loop:
9797
define void @add_nuw_comm2(i64 %c1, i64 %c2) {
9898
; CHECK-LABEL: @add_nuw_comm2(
9999
; CHECK-NEXT: entry:
100-
; CHECK-NEXT: [[INVARIANT_OP:%.*]] = add nuw i64 [[C2:%.*]], [[C1:%.*]]
100+
; CHECK-NEXT: [[INVARIANT_OP:%.*]] = add nuw i64 [[C1:%.*]], [[C2:%.*]]
101101
; CHECK-NEXT: br label [[LOOP:%.*]]
102102
; CHECK: loop:
103103
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDEX_NEXT_REASS:%.*]], [[LOOP]] ]
104104
; CHECK-NEXT: [[STEP_ADD:%.*]] = add nuw i64 [[INDEX]], [[C1]]
105105
; CHECK-NEXT: call void @use(i64 [[STEP_ADD]])
106-
; CHECK-NEXT: [[INDEX_NEXT_REASS]] = add nuw i64 [[INVARIANT_OP]], [[INDEX]]
106+
; CHECK-NEXT: [[INDEX_NEXT_REASS]] = add nuw i64 [[INDEX]], [[INVARIANT_OP]]
107107
; CHECK-NEXT: br label [[LOOP]]
108108
;
109109
entry:
@@ -122,13 +122,13 @@ loop:
122122
define void @add_nuw_comm3(i64 %c1, i64 %c2) {
123123
; CHECK-LABEL: @add_nuw_comm3(
124124
; CHECK-NEXT: entry:
125-
; CHECK-NEXT: [[INVARIANT_OP:%.*]] = add nuw i64 [[C2:%.*]], [[C1:%.*]]
125+
; CHECK-NEXT: [[INVARIANT_OP:%.*]] = add nuw i64 [[C1:%.*]], [[C2:%.*]]
126126
; CHECK-NEXT: br label [[LOOP:%.*]]
127127
; CHECK: loop:
128128
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDEX_NEXT_REASS:%.*]], [[LOOP]] ]
129129
; CHECK-NEXT: [[STEP_ADD:%.*]] = add nuw i64 [[C1]], [[INDEX]]
130130
; CHECK-NEXT: call void @use(i64 [[STEP_ADD]])
131-
; CHECK-NEXT: [[INDEX_NEXT_REASS]] = add nuw i64 [[INVARIANT_OP]], [[INDEX]]
131+
; CHECK-NEXT: [[INDEX_NEXT_REASS]] = add nuw i64 [[INDEX]], [[INVARIANT_OP]]
132132
; CHECK-NEXT: br label [[LOOP]]
133133
;
134134
entry:
@@ -196,13 +196,13 @@ loop:
196196
define void @mul_nuw_comm2(i64 %c1, i64 %c2) {
197197
; CHECK-LABEL: @mul_nuw_comm2(
198198
; CHECK-NEXT: entry:
199-
; CHECK-NEXT: [[INVARIANT_OP:%.*]] = mul i64 [[C2:%.*]], [[C1:%.*]]
199+
; CHECK-NEXT: [[INVARIANT_OP:%.*]] = mul i64 [[C1:%.*]], [[C2:%.*]]
200200
; CHECK-NEXT: br label [[LOOP:%.*]]
201201
; CHECK: loop:
202202
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDEX_NEXT_REASS:%.*]], [[LOOP]] ]
203203
; CHECK-NEXT: [[STEP_ADD:%.*]] = mul nuw i64 [[INDEX]], [[C1]]
204204
; CHECK-NEXT: call void @use(i64 [[STEP_ADD]])
205-
; CHECK-NEXT: [[INDEX_NEXT_REASS]] = mul i64 [[INVARIANT_OP]], [[INDEX]]
205+
; CHECK-NEXT: [[INDEX_NEXT_REASS]] = mul i64 [[INDEX]], [[INVARIANT_OP]]
206206
; CHECK-NEXT: br label [[LOOP]]
207207
;
208208
entry:
@@ -221,13 +221,13 @@ loop:
221221
define void @mul_nuw_comm3(i64 %c1, i64 %c2) {
222222
; CHECK-LABEL: @mul_nuw_comm3(
223223
; CHECK-NEXT: entry:
224-
; CHECK-NEXT: [[INVARIANT_OP:%.*]] = mul i64 [[C2:%.*]], [[C1:%.*]]
224+
; CHECK-NEXT: [[INVARIANT_OP:%.*]] = mul i64 [[C1:%.*]], [[C2:%.*]]
225225
; CHECK-NEXT: br label [[LOOP:%.*]]
226226
; CHECK: loop:
227227
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDEX_NEXT_REASS:%.*]], [[LOOP]] ]
228228
; CHECK-NEXT: [[STEP_ADD:%.*]] = mul nuw i64 [[C1]], [[INDEX]]
229229
; CHECK-NEXT: call void @use(i64 [[STEP_ADD]])
230-
; CHECK-NEXT: [[INDEX_NEXT_REASS]] = mul i64 [[INVARIANT_OP]], [[INDEX]]
230+
; CHECK-NEXT: [[INDEX_NEXT_REASS]] = mul i64 [[INDEX]], [[INVARIANT_OP]]
231231
; CHECK-NEXT: br label [[LOOP]]
232232
;
233233
entry:

0 commit comments

Comments
 (0)