Skip to content

Commit

Permalink
[InstCombine] Call simplifyLoadInst()
Browse files Browse the repository at this point in the history
InstCombine is supposed to be a superset of InstSimplify, but
failed to invoke load simplification.

Unfortunately, this causes a minor compile-time regression, which
will be mitigated in a future commit.
  • Loading branch information
nikic committed Feb 20, 2023
1 parent 9196908 commit be88b58
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
4 changes: 4 additions & 0 deletions llvm/include/llvm/Analysis/InstructionSimplify.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ Value *simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q);
/// If not, this returns null.
Value *simplifyFreezeInst(Value *Op, const SimplifyQuery &Q);

/// Given a load instruction and its pointer operand, fold the result or return
/// null.
Value *simplifyLoadInst(LoadInst *LI, Value *PtrOp, const SimplifyQuery &Q);

/// See if we can compute a simplified version of this instruction. If not,
/// return null.
Value *simplifyInstruction(Instruction *I, const SimplifyQuery &Q,
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6590,8 +6590,8 @@ Value *llvm::simplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
return ::simplifyFreezeInst(Op0, Q);
}

static Value *simplifyLoadInst(LoadInst *LI, Value *PtrOp,
const SimplifyQuery &Q) {
Value *llvm::simplifyLoadInst(LoadInst *LI, Value *PtrOp,
const SimplifyQuery &Q) {
if (LI->isVolatile())
return nullptr;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,8 @@ static bool canSimplifyNullLoadOrGEP(LoadInst &LI, Value *Op) {

Instruction *InstCombinerImpl::visitLoadInst(LoadInst &LI) {
Value *Op = LI.getOperand(0);
if (Value *Res = simplifyLoadInst(&LI, Op, SQ.getWithInstruction(&LI)))
return replaceInstUsesWith(LI, Res);

// Try to canonicalize the loaded type.
if (Instruction *Res = combineLoadToOperationType(*this, LI))
Expand Down
9 changes: 2 additions & 7 deletions llvm/test/Transforms/InstCombine/load.ll
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,7 @@ define <2 x i64> @test24(ptr %P) {

define i16 @load_from_zero_with_dynamic_offset(i64 %idx) {
; CHECK-LABEL: @load_from_zero_with_dynamic_offset(
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i16, ptr @GLOBAL, i64 [[IDX:%.*]]
; CHECK-NEXT: [[V:%.*]] = load i16, ptr [[GEP]], align 2
; CHECK-NEXT: ret i16 [[V]]
; CHECK-NEXT: ret i16 0
;
%gep = getelementptr i16, ptr @GLOBAL, i64 %idx
%v = load i16, ptr %gep
Expand All @@ -408,10 +406,7 @@ declare ptr @llvm.strip.invariant.group.p0(ptr %p)

define i32 @load_via_strip_invariant_group() {
; CHECK-LABEL: @load_via_strip_invariant_group(
; CHECK-NEXT: [[A:%.*]] = call ptr @llvm.strip.invariant.group.p0(ptr nonnull @Y)
; CHECK-NEXT: [[B:%.*]] = getelementptr i8, ptr [[A]], i64 8
; CHECK-NEXT: [[D:%.*]] = load i32, ptr [[B]], align 4
; CHECK-NEXT: ret i32 [[D]]
; CHECK-NEXT: ret i32 37
;
%a = call ptr @llvm.strip.invariant.group.p0(ptr @Y)
%b = getelementptr i8, ptr %a, i64 8
Expand Down
4 changes: 1 addition & 3 deletions llvm/test/Transforms/InstCombine/memcpy-from-global.ll
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,7 @@ entry:
define float @test11(i64 %i) {
; CHECK-LABEL: @test11(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[G:%.*]] = getelementptr [4 x float], ptr addrspace(1) @I, i64 0, i64 [[I:%.*]]
; CHECK-NEXT: [[R:%.*]] = load float, ptr addrspace(1) [[G]], align 4
; CHECK-NEXT: ret float [[R]]
; CHECK-NEXT: ret float 0.000000e+00
;

entry:
Expand Down

0 comments on commit be88b58

Please sign in to comment.