Skip to content

Commit 45a0b81

Browse files
committed
[InstCombine] Remove early constant fold
InstCombine currently performs a constant folding attempt as part of the main InstCombine loop, before visiting the instruction. However, each visit method will also attempt to simplify the instruction, which will in turn constant fold it. (Additionally, we also constant fold instructions before the main InstCombine loop and use a constant folding IR builder, so this is doubly redundant.) There is one place where InstCombine visit methods currently don't call into simplification, and that's casts. To be conservative, I've added an explicit constant folding call there (though it has no impact on tests). This makes for a mild compile-time improvement and in particular mitigates the compile-time regression from enabling load simplification in be88b58. Differential Revision: https://reviews.llvm.org/D144369
1 parent 2e0f917 commit 45a0b81

File tree

3 files changed

+5
-19
lines changed

3 files changed

+5
-19
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ Instruction *InstCombinerImpl::commonCastTransforms(CastInst &CI) {
308308
Value *Src = CI.getOperand(0);
309309
Type *Ty = CI.getType();
310310

311+
if (auto *SrcC = dyn_cast<Constant>(Src))
312+
if (Constant *Res = ConstantFoldCastOperand(CI.getOpcode(), SrcC, Ty, DL))
313+
return replaceInstUsesWith(CI, Res);
314+
311315
// Try to eliminate a cast of a cast.
312316
if (auto *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
313317
if (Instruction::CastOps NewOpc = isEliminableCastPair(CSrc, &CI)) {

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4221,23 +4221,6 @@ bool InstCombinerImpl::run() {
42214221
if (!DebugCounter::shouldExecute(VisitCounter))
42224222
continue;
42234223

4224-
// Instruction isn't dead, see if we can constant propagate it.
4225-
if (!I->use_empty() &&
4226-
(I->getNumOperands() == 0 || isa<Constant>(I->getOperand(0)))) {
4227-
if (Constant *C = ConstantFoldInstruction(I, DL, &TLI)) {
4228-
LLVM_DEBUG(dbgs() << "IC: ConstFold to: " << *C << " from: " << *I
4229-
<< '\n');
4230-
4231-
// Add operands to the worklist.
4232-
replaceInstUsesWith(*I, C);
4233-
++NumConstProp;
4234-
if (isInstructionTriviallyDead(I, &TLI))
4235-
eraseInstFromFunction(*I);
4236-
MadeIRChange = true;
4237-
continue;
4238-
}
4239-
}
4240-
42414224
// See if we can trivially sink this instruction to its user if we can
42424225
// prove that the successor is not executed more frequently than our block.
42434226
// Return the UserBlock if successful.

llvm/test/Transforms/InstCombine/pr38677.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ define i32 @foo(i1 %which, ptr %dst) {
1111
; CHECK: delay:
1212
; CHECK-NEXT: br label [[FINAL]]
1313
; CHECK: final:
14-
; CHECK-NEXT: [[USE2:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ select (i1 icmp eq (ptr @A, ptr @B), i32 2, i32 1), [[DELAY]] ]
1514
; CHECK-NEXT: store i1 false, ptr [[DST:%.*]], align 1
16-
; CHECK-NEXT: ret i32 [[USE2]]
15+
; CHECK-NEXT: ret i32 1
1716
;
1817
entry:
1918
br i1 true, label %final, label %delay

0 commit comments

Comments
 (0)