/// 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,
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;
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))
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
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
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: