Skip to content

Commit

Permalink
Reapply [InstCombine] Remove early constant fold
Browse files Browse the repository at this point in the history
The reported compile-time regression has been address in
47f9109.

Additionally, this contains a change to immediately fold zext
with constant operand, even if it's used in a trunc. I'm not sure
if this is relevant for anything, but I noticed it as a behavioral
discrepancy when investigating this issue.

-----

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
  • Loading branch information
nikic committed Feb 27, 2023
1 parent 7f635b9 commit ee2f9d6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 20 deletions.
7 changes: 6 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Expand Up @@ -308,6 +308,10 @@ Instruction *InstCombinerImpl::commonCastTransforms(CastInst &CI) {
Value *Src = CI.getOperand(0);
Type *Ty = CI.getType();

if (auto *SrcC = dyn_cast<Constant>(Src))
if (Constant *Res = ConstantFoldCastOperand(CI.getOpcode(), SrcC, Ty, DL))
return replaceInstUsesWith(CI, Res);

// Try to eliminate a cast of a cast.
if (auto *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
if (Instruction::CastOps NewOpc = isEliminableCastPair(CSrc, &CI)) {
Expand Down Expand Up @@ -1243,7 +1247,8 @@ static bool canEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear,
Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) {
// If this zero extend is only used by a truncate, let the truncate be
// eliminated before we try to optimize this zext.
if (Zext.hasOneUse() && isa<TruncInst>(Zext.user_back()))
if (Zext.hasOneUse() && isa<TruncInst>(Zext.user_back()) &&
!isa<Constant>(Zext.getOperand(0)))
return nullptr;

// If one of the common conversion will work, do it.
Expand Down
17 changes: 0 additions & 17 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Expand Up @@ -4238,23 +4238,6 @@ bool InstCombinerImpl::run() {
if (!DebugCounter::shouldExecute(VisitCounter))
continue;

// Instruction isn't dead, see if we can constant propagate it.
if (!I->use_empty() &&
(I->getNumOperands() == 0 || isa<Constant>(I->getOperand(0)))) {
if (Constant *C = ConstantFoldInstruction(I, DL, &TLI)) {
LLVM_DEBUG(dbgs() << "IC: ConstFold to: " << *C << " from: " << *I
<< '\n');

// Add operands to the worklist.
replaceInstUsesWith(*I, C);
++NumConstProp;
if (isInstructionTriviallyDead(I, &TLI))
eraseInstFromFunction(*I);
MadeIRChange = true;
continue;
}
}

// See if we can trivially sink this instruction to its user if we can
// prove that the successor is not executed more frequently than our block.
// Return the UserBlock if successful.
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/InstCombine/pr38677.ll
Expand Up @@ -11,9 +11,8 @@ define i32 @foo(i1 %which, ptr %dst) {
; CHECK: delay:
; CHECK-NEXT: br label [[FINAL]]
; CHECK: final:
; CHECK-NEXT: [[USE2:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ select (i1 icmp eq (ptr @A, ptr @B), i32 2, i32 1), [[DELAY]] ]
; CHECK-NEXT: store i1 false, ptr [[DST:%.*]], align 1
; CHECK-NEXT: ret i32 [[USE2]]
; CHECK-NEXT: ret i32 1
;
entry:
br i1 true, label %final, label %delay
Expand Down

0 comments on commit ee2f9d6

Please sign in to comment.