Strange code generation issue

Hi,

I am using the LLVM release_37 branch and have a strange issue.

Please see the two IR outputs I have saved below:

https://github.com/dibyendumajumdar/ravi/tree/master/ravi-tests/comp_issue

The original.ll is the code I am generating.
The aftercompiling.ll is the output from LLVM with opt level 0.

The issue is that LLVM is deciding to remove the blocks with labels
updatei and updatei.64 (lines 433 and 454 in original.ll) for some
reason that I cannot fathom. This is then causing the program to
misbehave.

I will check if the same issue occurs in LLVM 3.6.

I would appreciate any help on understanding what might be causing this issue.

Many thanks

Regards
Dibyendu

I seem to get the same issue in 3.6.2.

The issue appears to be in the following area:

Original code:

forloop34_: ; preds = %forbody27_, %forbody21_
%200 = load i64, i64* %iidx53, !tbaa !17
%next.idx = add nsw i64 1, %200
store i64 %next.idx, i64* %iidx53, !tbaa !17
%201 = load i64, i64* %ilimit52, !tbaa !17
%idx.gt.limit = icmp sgt i64 %next.idx, %201
br i1 %idx.gt.limit, label %exit_iforloop, label %updatei

updatei: ; preds = %forloop34_
%202 = load %struct.TValue*, %struct.TValue** %base, !tbaa !14
%203 = getelementptr inbounds %struct.TValue, %struct.TValue* %202, i32 12
%204 = load i64, i64* %iidx53, !tbaa !17
%205 = bitcast %struct.TValue* %203 to i64*
store i64 %204, i64* %205, !tbaa !18
%dest.tt61 = getelementptr inbounds %struct.TValue, %struct.TValue*
%203, i32 0, i32 1
store i32 19, i32* %dest.tt61, !tbaa !15
br label %forbody27_

Transformed code:

forloop34_: ; preds = %forbody27_, %forbody21_
%208 = load i64, i64* %iidx53, !tbaa !17
%next.idx = add nsw i64 1, %208
store i64 %next.idx, i64* %iidx53, !tbaa !17
%209 = load i64, i64* %ilimit52, !tbaa !17
%idx.gt.limit = icmp sgt i64 %next.idx, %209
br i1 %idx.gt.limit, label %forloop35_, label %forbody27_

forbody21_: ; preds = %forloop35_
  %sunkaddr137 = ptrtoint %struct.CallInfo* %0 to i64
  %sunkaddr138 = add i64 %sunkaddr137, 32
  %sunkaddr139 = inttoptr i64 %sunkaddr138 to %struct.TValue**
  %104 = load %struct.TValue*, %struct.TValue** %sunkaddr139, !tbaa !14
  %105 = getelementptr inbounds %struct.TValue, %struct.TValue* %104, i32 8
  %106 = load i64, i64* %iidx, !tbaa !17
  %107 = bitcast %struct.TValue* %105 to i64*
  store i64 %106, i64* %107, !tbaa !18
  %dest.tt65 = getelementptr inbounds %struct.TValue, %struct.TValue*
%105, i32 0, i32 1
  store i32 19, i32* %dest.tt65, !tbaa !15

So in the transformed code %105 should be %203 in the original code ...

Regards

Correction below:

Hi,

I am using the LLVM release_37 branch and have a strange issue.

Please see the two IR outputs I have saved below:

https://github.com/dibyendumajumdar/ravi/tree/master/ravi-tests/comp_issue

The original.ll is the code I am generating.
The aftercompiling.ll is the output from LLVM with opt level 0.

The issue is that LLVM is deciding to remove the blocks with labels
updatei and updatei.64 (lines 433 and 454 in original.ll) for some
reason that I cannot fathom. This is then causing the program to
misbehave.

I will check if the same issue occurs in LLVM 3.6.

I seem to get the same issue in 3.6.2.

The issue appears to be in the following area:

Original code:

forloop34_: ; preds = %forbody27_, %forbody21_
%200 = load i64, i64* %iidx53, !tbaa !17
%next.idx = add nsw i64 1, %200
store i64 %next.idx, i64* %iidx53, !tbaa !17
%201 = load i64, i64* %ilimit52, !tbaa !17
%idx.gt.limit = icmp sgt i64 %next.idx, %201
br i1 %idx.gt.limit, label %exit_iforloop, label %updatei

updatei: ; preds = %forloop34_
%202 = load %struct.TValue*, %struct.TValue** %base, !tbaa !14
%203 = getelementptr inbounds %struct.TValue, %struct.TValue* %202, i32 12
%204 = load i64, i64* %iidx53, !tbaa !17
%205 = bitcast %struct.TValue* %203 to i64*
store i64 %204, i64* %205, !tbaa !18
%dest.tt61 = getelementptr inbounds %struct.TValue, %struct.TValue*
%203, i32 0, i32 1
store i32 19, i32* %dest.tt61, !tbaa !15
br label %forbody27_

Transformed code:

forloop34_: ; preds = %forbody27_, %forbody21_
%208 = load i64, i64* %iidx53, !tbaa !17
%next.idx = add nsw i64 1, %208
store i64 %next.idx, i64* %iidx53, !tbaa !17
%209 = load i64, i64* %ilimit52, !tbaa !17
%idx.gt.limit = icmp sgt i64 %next.idx, %209
br i1 %idx.gt.limit, label %forloop35_, label %forbody27_

forbody27_: ; preds = %forloop34_
  %sunkaddr134 = ptrtoint %struct.CallInfo* %0 to i64
  %sunkaddr135 = add i64 %sunkaddr134, 32
  %sunkaddr136 = inttoptr i64 %sunkaddr135 to %struct.TValue**
  %141 = load %struct.TValue*, %struct.TValue** %sunkaddr136, !tbaa !14
  %142 = getelementptr inbounds %struct.TValue, %struct.TValue* %141, i32 12
  %143 = load i64, i64* %iidx53, !tbaa !17
  %144 = bitcast %struct.TValue* %142 to i64*
  store i64 %143, i64* %144, !tbaa !18
  %dest.tt61 = getelementptr inbounds %struct.TValue, %struct.TValue*
%142, i32 0, i32 1
  store i32 19, i32* %dest.tt61, !tbaa !15

So it seems correct as far as I can make out as presumably
%sunkaddr136 is same as %base.

Regards

Found the issue - bug is in the original code I am generating.

Apologies for the noise.

Regards