Skip to content

Commit

Permalink
[SCCP] Constant propagation through freeze instruction
Browse files Browse the repository at this point in the history
The freeze instruction has not been handled by SCCPInstVisitor.
This patch adds SCCPInstVisitor::visitFreezeInst(FreezeInst &I)
method to handle freeze instructions.

Differential Revision: https://reviews.llvm.org/D151659
  • Loading branch information
Mikhail Gudim authored and nikic committed Jun 1, 2023
1 parent b3e38a1 commit 559d47a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
21 changes: 21 additions & 0 deletions llvm/lib/Transforms/Utils/SCCPSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/ValueLattice.h"
#include "llvm/Analysis/ValueLatticeUtils.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
Expand Down Expand Up @@ -612,6 +613,7 @@ class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
void visitCastInst(CastInst &I);
void visitSelectInst(SelectInst &I);
void visitUnaryOperator(Instruction &I);
void visitFreezeInst(FreezeInst &I);
void visitBinaryOperator(Instruction &I);
void visitCmpInst(CmpInst &I);
void visitExtractValueInst(ExtractValueInst &EVI);
Expand Down Expand Up @@ -1404,6 +1406,25 @@ void SCCPInstVisitor::visitUnaryOperator(Instruction &I) {
markOverdefined(&I);
}

void SCCPInstVisitor::visitFreezeInst(FreezeInst &I) {
ValueLatticeElement V0State = getValueState(I.getOperand(0));
ValueLatticeElement &IV = ValueState[&I];
// resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
// discover a concrete value later.
if (SCCPSolver::isOverdefined(IV))
return (void)markOverdefined(&I);

// If something is unknown/undef, wait for it to resolve.
if (V0State.isUnknownOrUndef())
return;

if (SCCPSolver::isConstant(V0State) &&
isGuaranteedNotToBeUndefOrPoison(getConstant(V0State)))
return (void)markConstant(IV, &I, getConstant(V0State));

markOverdefined(&I);
}

// Handle Binary Operators.
void SCCPInstVisitor::visitBinaryOperator(Instruction &I) {
ValueLatticeElement V1State = getValueState(I.getOperand(0));
Expand Down
43 changes: 43 additions & 0 deletions llvm/test/Transforms/SCCP/freeze.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=ipsccp -S %s | FileCheck %s

@g = global i64 0
declare void @use(i1)

define i1 @freeze_undef_i1() {
Expand Down Expand Up @@ -39,6 +40,48 @@ define <2 x i32> @freeze_undef_vector() {
ret <2 x i32> %fr
}

define i1 @freeze_const_i1() {
; CHECK-LABEL: @freeze_const_i1(
; CHECK-NEXT: ret i1 true
;
%fr = freeze i1 1
ret i1 %fr
}

define ptr @freeze_const_ptr() {
; CHECK-LABEL: @freeze_const_ptr(
; CHECK-NEXT: ret ptr inttoptr (i32 256 to ptr)
;
%fr = freeze ptr inttoptr (i32 256 to ptr)
ret ptr %fr
}

define float @freeze_const_float() {
; CHECK-LABEL: @freeze_const_float(
; CHECK-NEXT: ret float 2.500000e-01
;
%fr = freeze float 2.500000e-01
ret float %fr
}

define <2 x i32> @freeze_const_vector() {
; CHECK-LABEL: @freeze_const_vector(
; CHECK-NEXT: ret <2 x i32> <i32 1, i32 2>
;
%fr = freeze <2 x i32> <i32 1, i32 2>
ret <2 x i32> %fr
}

; make sure we don't constant-propagate values that could potentially be poison
define i64 @maybe_poison() {
; CHECK-LABEL: @maybe_poison(
; CHECK-NEXT: [[FR:%.*]] = freeze i64 add nuw (i64 ptrtoint (ptr @g to i64), i64 123)
; CHECK-NEXT: ret i64 [[FR]]
;
%fr = freeze i64 add nuw (i64 ptrtoint (ptr @g to i64), i64 123)
ret i64 %fr
}

define i1 @propagate_range_from_and_through_freeze(i32 %x, i32 %y) {
; CHECK-LABEL: @propagate_range_from_and_through_freeze(
; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3
Expand Down

0 comments on commit 559d47a

Please sign in to comment.