Let EarlyCSE fold equivalent freeze instructions

Summary:
This patch makes EarlyCSE fold equivalent freeze instructions.

Another optimization that I think will be useful is to remove freeze if its operand is used as a branch condition or at llvm.assume:

```
  %c = ...
  br i1 %c, label %A, ..
A:
  %d = freeze %c ; %d can be optimized to %c because %c cannot be poison or undef (or 'br %c' would be UB otherwise)
```

If it make sense for EarlyCSE to support this as well, I will make a patch for this.

Reviewers: spatel, reames, lebedev.ri

Reviewed By: lebedev.ri

Subscribers: lebedev.ri, hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D75334
This commit is contained in:
Juneyoung Lee 2020-02-28 18:47:57 +09:00
parent 9a1fd96887
commit 274ef2efad
2 changed files with 17 additions and 2 deletions

View File

@ -114,7 +114,7 @@ struct SimpleValue {
isa<CmpInst>(Inst) || isa<SelectInst>(Inst) ||
isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) ||
isa<ShuffleVectorInst>(Inst) || isa<ExtractValueInst>(Inst) ||
isa<InsertValueInst>(Inst);
isa<InsertValueInst>(Inst) || isa<FreezeInst>(Inst);
}
};
@ -268,6 +268,9 @@ static unsigned getHashValueImpl(SimpleValue Val) {
if (CastInst *CI = dyn_cast<CastInst>(Inst))
return hash_combine(CI->getOpcode(), CI->getType(), CI->getOperand(0));
if (FreezeInst *FI = dyn_cast<FreezeInst>(Inst))
return hash_combine(FI->getOpcode(), FI->getOperand(0));
if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst))
return hash_combine(EVI->getOpcode(), EVI->getOperand(0),
hash_combine_range(EVI->idx_begin(), EVI->idx_end()));
@ -279,7 +282,8 @@ static unsigned getHashValueImpl(SimpleValue Val) {
assert((isa<CallInst>(Inst) || isa<GetElementPtrInst>(Inst) ||
isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) ||
isa<ShuffleVectorInst>(Inst) || isa<UnaryOperator>(Inst)) &&
isa<ShuffleVectorInst>(Inst) || isa<UnaryOperator>(Inst) ||
isa<FreezeInst>(Inst)) &&
"Invalid/unknown instruction");
// Mix in the opcode.

View File

@ -291,3 +291,14 @@ entry:
store i32 2, i32* @c, align 4
ret void
}
define i1 @cse_freeze(i1 %a) {
entry:
; CHECK-LABEL: @cse_freeze(
; CHECK: %b = freeze i1 %a
; CHECK: ret i1 %b
%b = freeze i1 %a
%c = freeze i1 %a
%and = and i1 %b, %c
ret i1 %and
}