llvm.org GIT mirror llvm / b3f0673
Teach valuetracking that byval arguments with a specified alignment are aligned. Teach memcpyopt to not give up all hope when confonted with an underaligned memcpy feeding an overaligned byval. If the *source* of the memcpy can be determined to be adequeately aligned, or if it can be forced to be, we can eliminate the memcpy. This addresses PR9794. We now compile the example into: define i32 @f(%struct.p* nocapture byval align 8 %q) nounwind ssp { entry: %call = call i32 @g(%struct.p* byval align 8 %q) nounwind ret i32 %call } in both x86-64 and x86-32 mode. We still don't get a tailcall though, because tailcalls apparently can't handle byval. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@131884 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 8 years ago
3 changed file(s) with 42 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
130130 }
131131 return;
132132 }
133
134 KnownZero.clearAllBits(); KnownOne.clearAllBits(); // Start out not knowing anything.
133
134 if (Argument *A = dyn_cast(V)) {
135 // Get alignment information off byval arguments if specified in the IR.
136 if (A->hasByValAttr())
137 if (unsigned Align = A->getParamAlignment())
138 KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
139 CountTrailingZeros_32(Align));
140 return;
141 }
142
143 // Start out not knowing anything.
144 KnownZero.clearAllBits(); KnownOne.clearAllBits();
135145
136146 if (Depth == MaxDepth || Mask == 0)
137147 return; // Limit search depth.
2222 #include "llvm/Analysis/AliasAnalysis.h"
2323 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
2424 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/Transforms/Utils/Local.h"
2526 #include "llvm/Support/Debug.h"
2627 #include "llvm/Support/GetElementPtrTypeIterator.h"
2728 #include "llvm/Support/IRBuilder.h"
865866 if (C1 == 0 || C1->getValue().getZExtValue() < ByValSize)
866867 return false;
867868
868 // Get the alignment of the byval. If it is greater than the memcpy, then we
869 // can't do the substitution. If the call doesn't specify the alignment, then
870 // it is some target specific value that we can't know.
869 // Get the alignment of the byval. If the call doesn't specify the alignment,
870 // then it is some target specific value that we can't know.
871871 unsigned ByValAlign = CS.getParamAlignment(ArgNo+1);
872 if (ByValAlign == 0 || MDep->getAlignment() < ByValAlign)
873 return false;
872 if (ByValAlign == 0) return false;
873
874 // If it is greater than the memcpy, then we check to see if we can force the
875 // source of the memcpy to the alignment we need. If we fail, we bail out.
876 if (MDep->getAlignment() < ByValAlign &&
877 getOrEnforceKnownAlignment(MDep->getSource(),ByValAlign, TD) < ByValAlign)
878 return false;
874879
875880 // Verify that the copied-from memory doesn't change in between the memcpy and
876881 // the byval call.
108108 ; CHECK-NEXT: ret void
109109 }
110110
111
112 ; PR9794 - Should forward memcpy into byval argument even though the memcpy
113 ; isn't itself 8 byte aligned.
114 %struct.p = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
115
116 define i32 @test7(%struct.p* nocapture byval align 8 %q) nounwind ssp {
117 entry:
118 %agg.tmp = alloca %struct.p, align 4
119 %tmp = bitcast %struct.p* %agg.tmp to i8*
120 %tmp1 = bitcast %struct.p* %q to i8*
121 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %tmp, i8* %tmp1, i64 48, i32 4, i1 false)
122 %call = call i32 @g(%struct.p* byval align 8 %agg.tmp) nounwind
123 ret i32 %call
124 ; CHECK: @test7
125 ; CHECK: call i32 @g(%struct.p* byval align 8 %q) nounwind
126 }
127
128 declare i32 @g(%struct.p* byval align 8)
129
130