Skip to content

Commit 559d47a

Browse files
Mikhail Gudimnikic
authored andcommitted
[SCCP] Constant propagation through freeze instruction
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
1 parent b3e38a1 commit 559d47a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

llvm/lib/Transforms/Utils/SCCPSolver.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/Analysis/InstructionSimplify.h"
1818
#include "llvm/Analysis/ValueLattice.h"
1919
#include "llvm/Analysis/ValueLatticeUtils.h"
20+
#include "llvm/Analysis/ValueTracking.h"
2021
#include "llvm/IR/InstVisitor.h"
2122
#include "llvm/Support/Casting.h"
2223
#include "llvm/Support/Debug.h"
@@ -612,6 +613,7 @@ class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
612613
void visitCastInst(CastInst &I);
613614
void visitSelectInst(SelectInst &I);
614615
void visitUnaryOperator(Instruction &I);
616+
void visitFreezeInst(FreezeInst &I);
615617
void visitBinaryOperator(Instruction &I);
616618
void visitCmpInst(CmpInst &I);
617619
void visitExtractValueInst(ExtractValueInst &EVI);
@@ -1404,6 +1406,25 @@ void SCCPInstVisitor::visitUnaryOperator(Instruction &I) {
14041406
markOverdefined(&I);
14051407
}
14061408

1409+
void SCCPInstVisitor::visitFreezeInst(FreezeInst &I) {
1410+
ValueLatticeElement V0State = getValueState(I.getOperand(0));
1411+
ValueLatticeElement &IV = ValueState[&I];
1412+
// resolvedUndefsIn might mark I as overdefined. Bail out, even if we would
1413+
// discover a concrete value later.
1414+
if (SCCPSolver::isOverdefined(IV))
1415+
return (void)markOverdefined(&I);
1416+
1417+
// If something is unknown/undef, wait for it to resolve.
1418+
if (V0State.isUnknownOrUndef())
1419+
return;
1420+
1421+
if (SCCPSolver::isConstant(V0State) &&
1422+
isGuaranteedNotToBeUndefOrPoison(getConstant(V0State)))
1423+
return (void)markConstant(IV, &I, getConstant(V0State));
1424+
1425+
markOverdefined(&I);
1426+
}
1427+
14071428
// Handle Binary Operators.
14081429
void SCCPInstVisitor::visitBinaryOperator(Instruction &I) {
14091430
ValueLatticeElement V1State = getValueState(I.getOperand(0));

llvm/test/Transforms/SCCP/freeze.ll

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt -passes=ipsccp -S %s | FileCheck %s
33

4+
@g = global i64 0
45
declare void @use(i1)
56

67
define i1 @freeze_undef_i1() {
@@ -39,6 +40,48 @@ define <2 x i32> @freeze_undef_vector() {
3940
ret <2 x i32> %fr
4041
}
4142

43+
define i1 @freeze_const_i1() {
44+
; CHECK-LABEL: @freeze_const_i1(
45+
; CHECK-NEXT: ret i1 true
46+
;
47+
%fr = freeze i1 1
48+
ret i1 %fr
49+
}
50+
51+
define ptr @freeze_const_ptr() {
52+
; CHECK-LABEL: @freeze_const_ptr(
53+
; CHECK-NEXT: ret ptr inttoptr (i32 256 to ptr)
54+
;
55+
%fr = freeze ptr inttoptr (i32 256 to ptr)
56+
ret ptr %fr
57+
}
58+
59+
define float @freeze_const_float() {
60+
; CHECK-LABEL: @freeze_const_float(
61+
; CHECK-NEXT: ret float 2.500000e-01
62+
;
63+
%fr = freeze float 2.500000e-01
64+
ret float %fr
65+
}
66+
67+
define <2 x i32> @freeze_const_vector() {
68+
; CHECK-LABEL: @freeze_const_vector(
69+
; CHECK-NEXT: ret <2 x i32> <i32 1, i32 2>
70+
;
71+
%fr = freeze <2 x i32> <i32 1, i32 2>
72+
ret <2 x i32> %fr
73+
}
74+
75+
; make sure we don't constant-propagate values that could potentially be poison
76+
define i64 @maybe_poison() {
77+
; CHECK-LABEL: @maybe_poison(
78+
; CHECK-NEXT: [[FR:%.*]] = freeze i64 add nuw (i64 ptrtoint (ptr @g to i64), i64 123)
79+
; CHECK-NEXT: ret i64 [[FR]]
80+
;
81+
%fr = freeze i64 add nuw (i64 ptrtoint (ptr @g to i64), i64 123)
82+
ret i64 %fr
83+
}
84+
4285
define i1 @propagate_range_from_and_through_freeze(i32 %x, i32 %y) {
4386
; CHECK-LABEL: @propagate_range_from_and_through_freeze(
4487
; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 3

0 commit comments

Comments
 (0)