llvm.org GIT mirror llvm / 809a10d
[llvm][IRBuilder] Introspection for CreateAlignmentAssumption*() functions Summary: Clang calls these functions to produce IR for assume-aligned attributes. I would like to teach UBSAN to verify these assumptions. For that, i need to access the final pointer on which the check is performed, and the actual `icmp` that does the check. The alternative to this would be to fully re-implement this in clang. This is a second commit, the original one was r351104, which was mass-reverted in r351159 because 2 compiler-rt tests were failing. Reviewers: spatel, dneilson, craig.topper, dblaikie, hfinkel Reviewed By: hfinkel Subscribers: hfinkel, llvm-commits Differential Revision: https://reviews.llvm.org/D54588 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351176 91177308-0d34-0410-b5e6-96231b3b80d8 Roman Lebedev 1 year, 10 months ago
1 changed file(s) with 19 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
22332233 private:
22342234 /// Helper function that creates an assume intrinsic call that
22352235 /// represents an alignment assumption on the provided Ptr, Mask, Type
2236 /// and Offset.
2236 /// and Offset. It may be sometimes useful to do some other logic
2237 /// based on this alignment check, thus it can be stored into 'TheCheck'.
22372238 CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
22382239 Value *PtrValue, Value *Mask,
2239 Type *IntPtrTy,
2240 Value *OffsetValue) {
2240 Type *IntPtrTy, Value *OffsetValue,
2241 Value **TheCheck) {
22412242 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
22422243
22432244 if (OffsetValue) {
22562257 Value *Zero = ConstantInt::get(IntPtrTy, 0);
22572258 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
22582259 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
2260 if (TheCheck)
2261 *TheCheck = InvCond;
2262
22592263 return CreateAssumption(InvCond);
22602264 }
22612265
22662270 /// An optional offset can be provided, and if it is provided, the offset
22672271 /// must be subtracted from the provided pointer to get the pointer with the
22682272 /// specified alignment.
2273 ///
2274 /// It may be sometimes useful to do some other logic
2275 /// based on this alignment check, thus it can be stored into 'TheCheck'.
22692276 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
22702277 unsigned Alignment,
2271 Value *OffsetValue = nullptr) {
2278 Value *OffsetValue = nullptr,
2279 Value **TheCheck = nullptr) {
22722280 assert(isa(PtrValue->getType()) &&
22732281 "trying to create an alignment assumption on a non-pointer?");
22742282 auto *PtrTy = cast(PtrValue->getType());
22762284
22772285 Value *Mask = ConstantInt::get(IntPtrTy, Alignment > 0 ? Alignment - 1 : 0);
22782286 return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
2279 OffsetValue);
2287 OffsetValue, TheCheck);
22802288 }
22812289
22822290 /// Create an assume intrinsic call that represents an alignment
22862294 /// must be subtracted from the provided pointer to get the pointer with the
22872295 /// specified alignment.
22882296 ///
2297 /// It may be sometimes useful to do some other logic
2298 /// based on this alignment check, thus it can be stored into 'TheCheck'.
2299 ///
22892300 /// This overload handles the condition where the Alignment is dependent
22902301 /// on an existing value rather than a static value.
22912302 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
22922303 Value *Alignment,
2293 Value *OffsetValue = nullptr) {
2304 Value *OffsetValue = nullptr,
2305 Value **TheCheck = nullptr) {
22942306 assert(isa(PtrValue->getType()) &&
22952307 "trying to create an alignment assumption on a non-pointer?");
22962308 auto *PtrTy = cast(PtrValue->getType());
23082320 ConstantInt::get(IntPtrTy, 0), "mask");
23092321
23102322 return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
2311 OffsetValue);
2323 OffsetValue, TheCheck);
23122324 }
23132325 };
23142326