diff --git a/llvm/lib/Transforms/Scalar/AlignmentFromAssumptions.cpp b/llvm/lib/Transforms/Scalar/AlignmentFromAssumptions.cpp --- a/llvm/lib/Transforms/Scalar/AlignmentFromAssumptions.cpp +++ b/llvm/lib/Transforms/Scalar/AlignmentFromAssumptions.cpp @@ -141,7 +141,7 @@ // What we really want to know is the overall offset to the aligned // address. This address is displaced by the provided offset. - DiffSCEV = SE->getMinusSCEV(DiffSCEV, OffSCEV); + DiffSCEV = SE->getAddExpr(DiffSCEV, OffSCEV); LLVM_DEBUG(dbgs() << "AFI: alignment of " << *Ptr << " relative to " << *AlignSCEV << " and offset " << *OffSCEV diff --git a/llvm/test/Transforms/AlignmentFromAssumptions/simple.ll b/llvm/test/Transforms/AlignmentFromAssumptions/simple.ll --- a/llvm/test/Transforms/AlignmentFromAssumptions/simple.ll +++ b/llvm/test/Transforms/AlignmentFromAssumptions/simple.ll @@ -16,7 +16,7 @@ define i32 @foo2(i32* nocapture %a) nounwind uwtable readonly { entry: tail call void @llvm.assume(i1 true) ["align"(i32* %a, i32 32, i32 24)] - %arrayidx = getelementptr inbounds i32, i32* %a, i64 2 + %arrayidx = getelementptr inbounds i32, i32* %a, i64 -2 %0 = load i32, i32* %arrayidx, align 4 ret i32 %0 @@ -28,7 +28,7 @@ define i32 @foo2a(i32* nocapture %a) nounwind uwtable readonly { entry: tail call void @llvm.assume(i1 true) ["align"(i32* %a, i32 32, i32 28)] - %arrayidx = getelementptr inbounds i32, i32* %a, i64 -1 + %arrayidx = getelementptr inbounds i32, i32* %a, i64 1 %0 = load i32, i32* %arrayidx, align 4 ret i32 %0 @@ -37,6 +37,19 @@ ; CHECK: ret i32 } +; TODO: this can be 8-bytes aligned +define i32 @foo2b(i32* nocapture %a) nounwind uwtable readonly { +entry: + tail call void @llvm.assume(i1 true) ["align"(i32* %a, i32 32, i32 28)] + %arrayidx = getelementptr inbounds i32, i32* %a, i64 -1 + %0 = load i32, i32* %arrayidx, align 4 + ret i32 %0 + +; CHECK-LABEL: @foo2b +; CHECK: load i32, i32* {{[^,]+}}, align 4 +; CHECK: ret i32 +} + define i32 @goo(i32* nocapture %a) nounwind uwtable readonly { entry: tail call void @llvm.assume(i1 true) ["align"(i32* %a, i32 32, i32 0)] diff --git a/llvm/test/Transforms/AlignmentFromAssumptions/simple32.ll b/llvm/test/Transforms/AlignmentFromAssumptions/simple32.ll --- a/llvm/test/Transforms/AlignmentFromAssumptions/simple32.ll +++ b/llvm/test/Transforms/AlignmentFromAssumptions/simple32.ll @@ -23,13 +23,13 @@ ; CHECK-SAME: (i32* nocapture [[A:%.*]]) #0 ; CHECK-NEXT: entry: ; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(i32* [[A]], i64 32, i64 24) ] -; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[A]], i64 2 +; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[A]], i64 -2 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* [[ARRAYIDX]], align 16 ; CHECK-NEXT: ret i32 [[TMP0]] ; entry: call void @llvm.assume(i1 true) ["align"(i32* %a, i64 32, i64 24)] - %arrayidx = getelementptr inbounds i32, i32* %a, i64 2 + %arrayidx = getelementptr inbounds i32, i32* %a, i64 -2 %0 = load i32, i32* %arrayidx, align 4 ret i32 %0 @@ -40,10 +40,28 @@ ; CHECK-SAME: (i32* nocapture [[A:%.*]]) #0 ; CHECK-NEXT: entry: ; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(i32* [[A]], i64 32, i64 28) ] -; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[A]], i64 -1 +; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[A]], i64 1 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* [[ARRAYIDX]], align 32 ; CHECK-NEXT: ret i32 [[TMP0]] ; +entry: + call void @llvm.assume(i1 true) ["align"(i32* %a, i64 32, i64 28)] + %arrayidx = getelementptr inbounds i32, i32* %a, i64 1 + %0 = load i32, i32* %arrayidx, align 4 + ret i32 %0 + +} + +; TODO: this can be 8-bytes aligned +define i32 @foo2b(i32* nocapture %a) nounwind uwtable readonly { +; CHECK-LABEL: define {{[^@]+}}@foo2b +; CHECK-SAME: (i32* nocapture [[A:%.*]]) #0 +; CHECK-NEXT: entry: +; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(i32* [[A]], i64 32, i64 28) ] +; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[A]], i64 -1 +; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* [[ARRAYIDX]], align 4 +; CHECK-NEXT: ret i32 [[TMP0]] +; entry: call void @llvm.assume(i1 true) ["align"(i32* %a, i64 32, i64 28)] %arrayidx = getelementptr inbounds i32, i32* %a, i64 -1