Skip to content

[LICM] Prevent LICM of ptrtoint and inttoptr when using non-integral pointers #97272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion llvm/lib/Transforms/Scalar/LICM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/PredIteratorCache.h"
#include "llvm/InitializePasses.h"
Expand Down Expand Up @@ -1328,6 +1329,20 @@ bool llvm::canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT,
}
}
return true;
} else if (auto *PTII = dyn_cast<PtrToIntInst>(&I)) {
const DataLayout &DL = I.getModule()->getDataLayout();
// Non-integral pointers may not have a stable bit representation, therefore
// casting them to an integer is not loop invariant.
if (DL.isNonIntegralPointerType(PTII->getPointerOperand()->getType())) {
return false;
}
} else if (auto *ITPI = dyn_cast<IntToPtrInst>(&I)) {
const DataLayout &DL = I.getModule()->getDataLayout();
// Non-integral pointers may not have a stable bit representation, therefore
// casting an integer to a non-integral pointer type is not loop invariant.
if (DL.isNonIntegralPointerType(ITPI->getType())) {
return false;
}
}

assert(!I.mayReadOrWriteMemory() && "unhandled aliasing");
Expand Down
67 changes: 67 additions & 0 deletions llvm/test/Transforms/LICM/non-integral-pointers.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
; RUN: opt -passes=licm -S < %s | FileCheck %s

target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-ni:1"

declare void @use_i64(i64 %0)
declare void @use_p1(ptr addrspace(1) %0)
declare i1 @cond()

define void @dont_hoist_ptrtoint(ptr addrspace(1) %p) {
; CHECK-LABEL: @dont_hoist_ptrtoint
; CHECK-LABEL: loop
; CHECK: ptrtoint
entry:
br label %loop

loop:
%p.int = ptrtoint ptr addrspace(1) %p to i64
call void @use_i64(i64 %p.int)
br label %loop
}

define void @dont_hoist_inttoptr(i64 %p.int) {
; CHECK-LABEL: @dont_hoist_inttoptr
; CHECK-LABEL: loop
; CHECK: inttoptr
entry:
br label %loop

loop:
%p = inttoptr i64 %p.int to ptr addrspace(1)
call void @use_p1(ptr addrspace(1) %p)
br label %loop
}

define i64 @dont_sink_ptrtoint(ptr addrspace(1) %p) {
; CHECK-LABEL: @dont_sink_ptrtoint
; CHECK-LABEL: loop
; CHECK: ptrtoint
; CHECK-LABEL: exit
entry:
br label %loop

loop:
%p.int = ptrtoint ptr addrspace(1) %p to i64
%c = call i1 @cond()
br i1 %c, label %loop, label %exit

exit:
ret i64 %p.int
}

define ptr addrspace(1) @dont_sink_inttoptr(i64 %p.int) {
; CHECK-LABEL: @dont_sink_inttoptr
; CHECK-LABEL: loop
; CHECK: inttoptr
; CHECK-LABEL: exit
entry:
br label %loop

loop:
%p = inttoptr i64 %p.int to ptr addrspace(1)
%c = call i1 @cond()
br i1 %c, label %loop, label %exit

exit:
ret ptr addrspace(1) %p
}
Loading