llvm.org GIT mirror llvm / 11af4b4
Add a dereferenceable attribute This attribute indicates that the parameter or return pointer is dereferenceable. Practically speaking, loads from such a pointer within the associated byte range are safe to speculatively execute. Such pointer parameters are common in source languages (C++ references, for example). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213385 91177308-0d34-0410-b5e6-96231b3b80d8 Hal Finkel 5 years ago
22 changed file(s) with 388 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
967967 checked or enforced by LLVM, the caller must ensure that the pointer
968968 passed in is non-null, or the callee must ensure that the returned pointer
969969 is non-null.
970
971 ``dereferenceable()``
972 This indicates that the parameter or return pointer is dereferenceable. This
973 attribute may only be applied to pointer typed parameters. A pointer that
974 is dereferenceable can be loaded from speculatively without a risk of
975 trapping. The number of bytes known to be dereferenceable must be provided
976 in parentheses. It is legal for the number of bytes to be less than the
977 size of the pointee type. The ``nonnull`` attribute does not imply
978 dereferenceability (consider a pointer to one element past the end of an
979 array), however ``dereferenceable()`` does imply ``nonnull`` in
980 ``addrspace(0)`` (which is the default address space).
970981
971982 .. _gc:
972983
373373 ATTR_KIND_OPTIMIZE_NONE = 37,
374374 ATTR_KIND_IN_ALLOCA = 38,
375375 ATTR_KIND_NON_NULL = 39,
376 ATTR_KIND_JUMP_TABLE = 40
376 ATTR_KIND_JUMP_TABLE = 40,
377 ATTR_KIND_DEREFERENCEABLE = 41
377378 };
378379
379380 enum ComdatSelectionKindCodes {
5555 unsigned getArgNo() const;
5656
5757 /// \brief Return true if this argument has the nonnull attribute on it in
58 /// its containing function.
58 /// its containing function. Also returns true if at least one byte is known
59 /// to be dereferenceable and the pointer is in addrspace(0).
5960 bool hasNonNullAttr() const;
61
62 /// \brief If this argument has the dereferenceable attribute on it in its
63 /// containing function, return the number of bytes known to be
64 /// dereferenceable. Otherwise, zero is returned.
65 uint64_t getDereferenceableBytes() const;
6066
6167 /// \brief Return true if this argument has the byval attribute on it in its
6268 /// containing function.
8787 NonLazyBind, ///< Function is called early and/or
8888 ///< often, so lazy binding isn't worthwhile
8989 NonNull, ///< Pointer is known to be not null
90 Dereferenceable, ///< Pointer is known to be dereferenceable
9091 NoRedZone, ///< Disable redzone
9192 NoReturn, ///< Mark the function as not returning
9293 NoUnwind, ///< Function doesn't unwind stack
132133 /// alignment set.
133134 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
134135 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
136 static Attribute getWithDereferenceableBytes(LLVMContext &Context,
137 uint64_t Bytes);
135138
136139 //===--------------------------------------------------------------------===//
137140 // Attribute Accessors
176179 /// \brief Returns the stack alignment field of an attribute as a byte
177180 /// alignment value.
178181 unsigned getStackAlignment() const;
182
183 /// \brief Returns the number of dereferenceable bytes from the
184 /// dereferenceable attribute (or zero if unknown).
185 uint64_t getDereferenceableBytes() const;
179186
180187 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
181188 /// is, presumably, for writing out the mnemonics for the assembly writer.
315322 /// \brief Get the stack alignment.
316323 unsigned getStackAlignment(unsigned Index) const;
317324
325 /// \brief Get the number of dereferenceable bytes (or zero if unknown).
326 uint64_t getDereferenceableBytes(unsigned Index) const;
327
318328 /// \brief Return the attributes at the index as a string.
319329 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
320330
394404 std::map TargetDepAttrs;
395405 uint64_t Alignment;
396406 uint64_t StackAlignment;
407 uint64_t DerefBytes;
397408 public:
398 AttrBuilder() : Attrs(0), Alignment(0), StackAlignment(0) {}
409 AttrBuilder() : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {}
399410 explicit AttrBuilder(uint64_t Val)
400 : Attrs(0), Alignment(0), StackAlignment(0) {
411 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
401412 addRawValue(Val);
402413 }
403 AttrBuilder(const Attribute &A) : Attrs(0), Alignment(0), StackAlignment(0) {
414 AttrBuilder(const Attribute &A)
415 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
404416 addAttribute(A);
405417 }
406418 AttrBuilder(AttributeSet AS, unsigned Idx);
454466 /// \brief Retrieve the stack alignment attribute, if it exists.
455467 uint64_t getStackAlignment() const { return StackAlignment; }
456468
469 /// \brief Retrieve the number of dereferenceable bytes, if the dereferenceable
470 /// attribute exists (zero is returned otherwise).
471 uint64_t getDereferenceableBytes() const { return DerefBytes; }
472
457473 /// \brief This turns an int alignment (which must be a power of 2) into the
458474 /// form used internally in Attribute.
459475 AttrBuilder &addAlignmentAttr(unsigned Align);
461477 /// \brief This turns an int stack alignment (which must be a power of 2) into
462478 /// the form used internally in Attribute.
463479 AttrBuilder &addStackAlignmentAttr(unsigned Align);
480
481 /// \brief This turns the number of dereferenceable bytes into the form used
482 /// internally in Attribute.
483 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
464484
465485 /// \brief Return true if the builder contains no target-independent
466486 /// attributes.
216216 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
217217 }
218218
219 /// @brief Extract the number of dereferenceable bytes for a call or
220 /// parameter (0=unknown).
221 uint64_t getDereferenceableBytes(uint16_t i) const {
222 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
223 }
224
219225 /// \brief Return true if the call should not be treated as a call to a
220226 /// builtin.
221227 bool isNoBuiltin() const {
299305 bool onlyReadsMemory(unsigned ArgNo) const {
300306 return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
301307 paramHasAttr(ArgNo + 1, Attribute::ReadNone);
308 }
309
310 /// @brief Return true if the return value is known to be not null.
311 /// This may be because it has the nonnull attribute, or because at least
312 /// one byte is dereferenceable and the pointer is in addrspace(0).
313 bool isReturnNonNull() const {
314 if (paramHasAttr(0, Attribute::NonNull))
315 return true;
316 else if (getDereferenceableBytes(0) > 0 &&
317 getType()->getPointerAddressSpace() == 0)
318 return true;
319
320 return false;
302321 }
303322
304323 /// hasArgument - Returns true if this CallSite passes the given Value* as an
232232 return AttributeSets.getParamAlignment(i);
233233 }
234234
235 /// @brief Extract the number of dereferenceable bytes for a call or
236 /// parameter (0=unknown).
237 uint64_t getDereferenceableBytes(unsigned i) const {
238 return AttributeSets.getDereferenceableBytes(i);
239 }
240
235241 /// @brief Determine if the function does not access memory.
236242 bool doesNotAccessMemory() const {
237243 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
13731373 /// \brief Extract the alignment for a call or parameter (0=unknown).
13741374 unsigned getParamAlignment(unsigned i) const {
13751375 return AttributeList.getParamAlignment(i);
1376 }
1377
1378 /// \brief Extract the number of dereferenceable bytes for a call or
1379 /// parameter (0=unknown).
1380 uint64_t getDereferenceableBytes(unsigned i) const {
1381 return AttributeList.getDereferenceableBytes(i);
13761382 }
13771383
13781384 /// \brief Return true if the call should not be treated as a call to a
30503056 return AttributeList.getParamAlignment(i);
30513057 }
30523058
3059 /// \brief Extract the number of dereferenceable bytes for a call or
3060 /// parameter (0=unknown).
3061 uint64_t getDereferenceableBytes(unsigned i) const {
3062 return AttributeList.getDereferenceableBytes(i);
3063 }
3064
30533065 /// \brief Return true if the call should not be treated as a call to a
30543066 /// builtin.
30553067 bool isNoBuiltin() const {
167167 LLVMInAllocaAttribute = 1ULL << 36,
168168 LLVMNonNullAttribute = 1ULL << 37,
169169 LLVMJumpTableAttribute = 1ULL << 38,
170 LLVMDereferenceableAttribute = 1ULL << 39,
170171 */
171172 } LLVMAttribute;
172173
20852085 return !GV->hasExternalWeakLinkage();
20862086
20872087 if (ImmutableCallSite CS = V)
2088 if (CS.paramHasAttr(0, Attribute::NonNull))
2088 if (CS.isReturnNonNull())
20892089 return true;
20902090
20912091 // operator new never returns null.
611611 KEYWORD(byval);
612612 KEYWORD(inalloca);
613613 KEYWORD(cold);
614 KEYWORD(dereferenceable);
614615 KEYWORD(inlinehint);
615616 KEYWORD(inreg);
616617 KEYWORD(jumptable);
10511051 "invalid use of attribute on a function");
10521052 break;
10531053 case lltok::kw_byval:
1054 case lltok::kw_dereferenceable:
10541055 case lltok::kw_inalloca:
10551056 case lltok::kw_nest:
10561057 case lltok::kw_noalias:
12071208 if (Val64 != unsigned(Val64))
12081209 return TokError("expected 32-bit integer (too large)");
12091210 Val = Val64;
1211 Lex.Lex();
1212 return false;
1213 }
1214
1215 /// ParseUInt64
1216 /// ::= uint64
1217 bool LLParser::ParseUInt64(uint64_t &Val) {
1218 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1219 return TokError("expected integer");
1220 Val = Lex.getAPSIntVal().getLimitedValue();
12101221 Lex.Lex();
12111222 return false;
12121223 }
12831294 continue;
12841295 }
12851296 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
1297 case lltok::kw_dereferenceable: {
1298 uint64_t Bytes;
1299 if (ParseOptionalDereferenceableBytes(Bytes))
1300 return true;
1301 B.addDereferenceableAttr(Bytes);
1302 continue;
1303 }
12861304 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
12871305 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
12881306 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
13401358 switch (Token) {
13411359 default: // End of attributes.
13421360 return HaveError;
1361 case lltok::kw_dereferenceable: {
1362 uint64_t Bytes;
1363 if (ParseOptionalDereferenceableBytes(Bytes))
1364 return true;
1365 B.addDereferenceableAttr(Bytes);
1366 continue;
1367 }
13431368 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
13441369 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
13451370 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
16051630 return false;
16061631 }
16071632
1633 /// ParseOptionalDereferenceableBytes
1634 /// ::= /* empty */
1635 /// ::= 'dereferenceable' '(' 4 ')'
1636 bool LLParser::ParseOptionalDereferenceableBytes(uint64_t &Bytes) {
1637 Bytes = 0;
1638 if (!EatIfPresent(lltok::kw_dereferenceable))
1639 return false;
1640 LocTy ParenLoc = Lex.getLoc();
1641 if (!EatIfPresent(lltok::lparen))
1642 return Error(ParenLoc, "expected '('");
1643 LocTy DerefLoc = Lex.getLoc();
1644 if (ParseUInt64(Bytes)) return true;
1645 ParenLoc = Lex.getLoc();
1646 if (!EatIfPresent(lltok::rparen))
1647 return Error(ParenLoc, "expected ')'");
1648 if (!Bytes)
1649 return Error(DerefLoc, "dereferenceable bytes must be non-zero");
1650 return false;
1651 }
1652
16081653 /// ParseOptionalCommaAlign
16091654 /// ::=
16101655 /// ::= ',' align 4
201201 Loc = Lex.getLoc();
202202 return ParseUInt32(Val);
203203 }
204 bool ParseUInt64(uint64_t &Val);
205 bool ParseUInt64(uint64_t &Val, LocTy &Loc) {
206 Loc = Lex.getLoc();
207 return ParseUInt64(Val);
208 }
204209
205210 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
206211 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
218223 bool ParseOptionalDLLStorageClass(unsigned &DLLStorageClass);
219224 bool ParseOptionalCallingConv(CallingConv::ID &CC);
220225 bool ParseOptionalAlignment(unsigned &Alignment);
226 bool ParseOptionalDereferenceableBytes(uint64_t &Bytes);
221227 bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
222228 AtomicOrdering &Ordering);
223229 bool ParseOrdering(AtomicOrdering &Ordering);
105105 kw_byval,
106106 kw_inalloca,
107107 kw_cold,
108 kw_dereferenceable,
108109 kw_inlinehint,
109110 kw_inreg,
110111 kw_jumptable,
587587 return Attribute::NonLazyBind;
588588 case bitc::ATTR_KIND_NON_NULL:
589589 return Attribute::NonNull;
590 case bitc::ATTR_KIND_DEREFERENCEABLE:
591 return Attribute::Dereferenceable;
590592 case bitc::ATTR_KIND_NO_RED_ZONE:
591593 return Attribute::NoRedZone;
592594 case bitc::ATTR_KIND_NO_RETURN:
688690 return EC;
689691 if (Kind == Attribute::Alignment)
690692 B.addAlignmentAttr(Record[++i]);
691 else
693 else if (Kind == Attribute::StackAlignment)
692694 B.addStackAlignmentAttr(Record[++i]);
695 else if (Kind == Attribute::Dereferenceable)
696 B.addDereferenceableAttr(Record[++i]);
693697 } else { // String attribute
694698 assert((Record[i] == 3 || Record[i] == 4) &&
695699 "Invalid attribute group entry");
200200 return bitc::ATTR_KIND_NON_LAZY_BIND;
201201 case Attribute::NonNull:
202202 return bitc::ATTR_KIND_NON_NULL;
203 case Attribute::Dereferenceable:
204 return bitc::ATTR_KIND_DEREFERENCEABLE;
203205 case Attribute::NoRedZone:
204206 return bitc::ATTR_KIND_NO_RED_ZONE;
205207 case Attribute::NoReturn:
115115 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
116116 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
117117 assert(
118 (Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
118 (Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
119 Kind == Attribute::Dereferenceable) &&
119120 "Wrong kind for int attribute!");
120121 }
121122
163164
164165 unsigned getAlignment() const;
165166 unsigned getStackAlignment() const;
167 uint64_t getDereferenceableBytes() const;
166168 std::string getAsString(bool InAttrGrp) const;
167169
168170 typedef const Attribute *iterator;
8787 return get(Context, StackAlignment, Align);
8888 }
8989
90 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
91 uint64_t Bytes) {
92 assert(Bytes && "Bytes must be non-zero.");
93 return get(Context, Dereferenceable, Bytes);
94 }
95
9096 //===----------------------------------------------------------------------===//
9197 // Attribute Accessor Methods
9298 //===----------------------------------------------------------------------===//
152158 unsigned Attribute::getStackAlignment() const {
153159 assert(hasAttribute(Attribute::StackAlignment) &&
154160 "Trying to get alignment from non-alignment attribute!");
161 return pImpl->getValueAsInt();
162 }
163
164 /// This returns the number of dereferenceable bytes.
165 uint64_t Attribute::getDereferenceableBytes() const {
166 assert(hasAttribute(Attribute::Dereferenceable) &&
167 "Trying to get dereferenceable bytes from "
168 "non-dereferenceable attribute!");
155169 return pImpl->getValueAsInt();
156170 }
157171
262276 return Result;
263277 }
264278
279 if (hasAttribute(Attribute::Dereferenceable)) {
280 std::string Result;
281 Result += "dereferenceable";
282 if (InAttrGrp) {
283 Result += "=";
284 Result += utostr(getValueAsInt());
285 } else {
286 Result += "(";
287 Result += utostr(getValueAsInt());
288 Result += ")";
289 }
290 return Result;
291 }
292
265293 // Convert target-dependent attributes to strings of the form:
266294 //
267295 // "kind"
397425 case Attribute::InAlloca: return 1ULL << 43;
398426 case Attribute::NonNull: return 1ULL << 44;
399427 case Attribute::JumpTable: return 1ULL << 45;
428 case Attribute::Dereferenceable:
429 llvm_unreachable("dereferenceable attribute not supported in raw format");
400430 }
401431 llvm_unreachable("Unsupported attribute type");
402432 }
481511 return 0;
482512 }
483513
514 uint64_t AttributeSetNode::getDereferenceableBytes() const {
515 for (iterator I = begin(), E = end(); I != E; ++I)
516 if (I->hasAttribute(Attribute::Dereferenceable))
517 return I->getDereferenceableBytes();
518 return 0;
519 }
520
484521 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
485522 std::string Str;
486523 for (iterator I = begin(), E = end(); I != E; ++I) {
514551 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
515552 else if (Kind == Attribute::StackAlignment)
516553 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
554 else if (Kind == Attribute::Dereferenceable)
555 llvm_unreachable("dereferenceable not supported in bit mask");
517556 else
518557 Mask |= AttributeImpl::getAttrMask(Kind);
519558 }
619658 else if (Kind == Attribute::StackAlignment)
620659 Attrs.push_back(std::make_pair(Index, Attribute::
621660 getWithStackAlignment(C, B.getStackAlignment())));
661 else if (Kind == Attribute::Dereferenceable)
662 Attrs.push_back(std::make_pair(Index,
663 Attribute::getWithDereferenceableBytes(C,
664 B.getDereferenceableBytes())));
622665 else
623666 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
624667 }
876919 return ASN ? ASN->getStackAlignment() : 0;
877920 }
878921
922 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
923 AttributeSetNode *ASN = getAttributes(Index);
924 return ASN ? ASN->getDereferenceableBytes() : 0;
925 }
926
879927 std::string AttributeSet::getAsString(unsigned Index,
880928 bool InAttrGrp) const {
881929 AttributeSetNode *ASN = getAttributes(Index);
9551003 //===----------------------------------------------------------------------===//
9561004
9571005 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
958 : Attrs(0), Alignment(0), StackAlignment(0) {
1006 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
9591007 AttributeSetImpl *pImpl = AS.pImpl;
9601008 if (!pImpl) return;
9611009
9721020
9731021 void AttrBuilder::clear() {
9741022 Attrs.reset();
975 Alignment = StackAlignment = 0;
1023 Alignment = StackAlignment = DerefBytes = 0;
9761024 }
9771025
9781026 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
9791027 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
9801028 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
981 "Adding alignment attribute without adding alignment value!");
1029 Val != Attribute::Dereferenceable &&
1030 "Adding integer attribute without adding a value!");
9821031 Attrs[Val] = true;
9831032 return *this;
9841033 }
9961045 Alignment = Attr.getAlignment();
9971046 else if (Kind == Attribute::StackAlignment)
9981047 StackAlignment = Attr.getStackAlignment();
1048 else if (Kind == Attribute::Dereferenceable)
1049 DerefBytes = Attr.getDereferenceableBytes();
9991050 return *this;
10001051 }
10011052
10121063 Alignment = 0;
10131064 else if (Val == Attribute::StackAlignment)
10141065 StackAlignment = 0;
1066 else if (Val == Attribute::Dereferenceable)
1067 DerefBytes = 0;
10151068
10161069 return *this;
10171070 }
10361089 Alignment = 0;
10371090 else if (Kind == Attribute::StackAlignment)
10381091 StackAlignment = 0;
1092 else if (Kind == Attribute::Dereferenceable)
1093 DerefBytes = 0;
10391094 } else {
10401095 assert(Attr.isStringAttribute() && "Invalid attribute type!");
10411096 std::map::iterator
10781133 return *this;
10791134 }
10801135
1136 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1137 if (Bytes == 0) return *this;
1138
1139 Attrs[Attribute::Dereferenceable] = true;
1140 DerefBytes = Bytes;
1141 return *this;
1142 }
1143
10811144 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
10821145 // FIXME: What if both have alignments, but they don't match?!
10831146 if (!Alignment)
10851148
10861149 if (!StackAlignment)
10871150 StackAlignment = B.StackAlignment;
1151
1152 if (!DerefBytes)
1153 DerefBytes = B.DerefBytes;
10881154
10891155 Attrs |= B.Attrs;
10901156
11411207 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
11421208 return false;
11431209
1144 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1210 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1211 DerefBytes == B.DerefBytes;
11451212 }
11461213
11471214 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
11501217
11511218 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
11521219 I = Attribute::AttrKind(I + 1)) {
1220 if (I == Attribute::Dereferenceable)
1221 continue;
11531222 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
11541223 Attrs[I] = true;
11551224
11831252 .addAttribute(Attribute::NoAlias)
11841253 .addAttribute(Attribute::NoCapture)
11851254 .addAttribute(Attribute::NonNull)
1255 .addDereferenceableAttr(1) // the int here is ignored
11861256 .addAttribute(Attribute::ReadNone)
11871257 .addAttribute(Attribute::ReadOnly)
11881258 .addAttribute(Attribute::StructRet)
7676 }
7777
7878 /// hasNonNullAttr - Return true if this argument has the nonnull attribute on
79 /// it in its containing function.
79 /// it in its containing function. Also returns true if at least one byte is
80 /// known to be dereferenceable and the pointer is in addrspace(0).
8081 bool Argument::hasNonNullAttr() const {
8182 if (!getType()->isPointerTy()) return false;
82 return getParent()->getAttributes().
83 hasAttribute(getArgNo()+1, Attribute::NonNull);
83 if (getParent()->getAttributes().
84 hasAttribute(getArgNo()+1, Attribute::NonNull))
85 return true;
86 else if (getDereferenceableBytes() > 0 &&
87 getType()->getPointerAddressSpace() == 0)
88 return true;
89 return false;
8490 }
8591
8692 /// hasByValAttr - Return true if this argument has the byval attribute on it
110116 assert(getType()->isPointerTy() && "Only pointers have alignments");
111117 return getParent()->getParamAlignment(getArgNo()+1);
112118
119 }
120
121 uint64_t Argument::getDereferenceableBytes() const {
122 assert(getType()->isPointerTy() &&
123 "Only pointers have dereferenceable bytes");
124 return getParent()->getDereferenceableBytes(getArgNo()+1);
113125 }
114126
115127 /// hasNestAttr - Return true if this argument has the nest attribute on
1414 #include "LLVMContextImpl.h"
1515 #include "llvm/ADT/DenseMap.h"
1616 #include "llvm/ADT/SmallString.h"
17 #include "llvm/IR/CallSite.h"
1718 #include "llvm/IR/Constant.h"
1819 #include "llvm/IR/Constants.h"
1920 #include "llvm/IR/DataLayout.h"
503504 if (const GlobalVariable *GV = dyn_cast(V))
504505 return !GV->hasExternalWeakLinkage();
505506
506 // byval arguments are ok.
507 if (const Argument *A = dyn_cast(V))
508 return A->hasByValAttr();
507 // byval arguments are okay. Arguments specifically marked as
508 // dereferenceable are okay too.
509 if (const Argument *A = dyn_cast(V)) {
510 if (A->hasByValAttr())
511 return true;
512 else if (uint64_t Bytes = A->getDereferenceableBytes()) {
513 Type *Ty = V->getType()->getPointerElementType();
514 if (Ty->isSized() && DL && DL->getTypeStoreSize(Ty) <= Bytes)
515 return true;
516 }
517
518 return false;
519 }
520
521 // Return values from call sites specifically marked as dereferenceable are
522 // also okay.
523 if (ImmutableCallSite CS = V) {
524 if (uint64_t Bytes = CS.getDereferenceableBytes(0)) {
525 Type *Ty = V->getType()->getPointerElementType();
526 if (Ty->isSized() && DL && DL->getTypeStoreSize(Ty) <= Bytes)
527 return true;
528 }
529 }
509530
510531 // For GEPs, determine if the indexing lands within the allocated object.
511532 if (const GEPOperator *GEP = dyn_cast(V)) {
226226 ; CHECK: define void @f38() unnamed_addr #24
227227 call void bitcast (void (i8*)* @f36 to void ()*)()
228228 unreachable
229 }
230
231 define dereferenceable(2) i8* @f39(i8* dereferenceable(1) %a) {
232 ; CHECK: define dereferenceable(2) i8* @f39(i8* dereferenceable(1) %a) {
233 ret i8* %a
234 }
235
236 define dereferenceable(18446744073709551606) i8* @f40(i8* dereferenceable(18446744073709551615) %a) {
237 ; CHECK: define dereferenceable(18446744073709551606) i8* @f40(i8* dereferenceable(18446744073709551615) %a) {
238 ret i8* %a
229239 }
230240
231241 ; CHECK: attributes #0 = { noreturn }
873873 ; CHECK: ret i1 false
874874 }
875875
876 define i1 @nonnull_deref_arg(i32* dereferenceable(4) %i) {
877 %cmp = icmp eq i32* %i, null
878 ret i1 %cmp
879 ; CHECK-LABEL: @nonnull_deref_arg
880 ; CHECK: ret i1 false
881 }
882
883 define i1 @nonnull_deref_as_arg(i32 addrspace(1)* dereferenceable(4) %i) {
884 %cmp = icmp eq i32 addrspace(1)* %i, null
885 ret i1 %cmp
886 ; CHECK-LABEL: @nonnull_deref_as_arg
887 ; CHECK: icmp
888 ; CHECK ret
889 }
890
876891 declare nonnull i32* @returns_nonnull_helper()
877892 define i1 @returns_nonnull() {
878893 %call = call nonnull i32* @returns_nonnull_helper()
880895 ret i1 %cmp
881896 ; CHECK-LABEL: @returns_nonnull
882897 ; CHECK: ret i1 false
898 }
899
900 declare dereferenceable(4) i32* @returns_nonnull_deref_helper()
901 define i1 @returns_nonnull_deref() {
902 %call = call dereferenceable(4) i32* @returns_nonnull_deref_helper()
903 %cmp = icmp eq i32* %call, null
904 ret i1 %cmp
905 ; CHECK-LABEL: @returns_nonnull_deref
906 ; CHECK: ret i1 false
907 }
908
909 declare dereferenceable(4) i32 addrspace(1)* @returns_nonnull_deref_as_helper()
910 define i1 @returns_nonnull_as_deref() {
911 %call = call dereferenceable(4) i32 addrspace(1)* @returns_nonnull_deref_as_helper()
912 %cmp = icmp eq i32 addrspace(1)* %call, null
913 ret i1 %cmp
914 ; CHECK-LABEL: @returns_nonnull_as_deref
915 ; CHECK: icmp
916 ; CHECK: ret
883917 }
884918
885919 ; If a bit is known to be zero for A and known to be one for B,
0 ; RUN: opt -S -basicaa -licm < %s | FileCheck %s
1 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
2 target triple = "x86_64-unknown-linux-gnu"
3
4 ; This test represents the following function:
5 ; void test1(int * __restrict__ a, int * __restrict__ b, int &c, int n) {
6 ; for (int i = 0; i < n; ++i)
7 ; if (a[i] > 0)
8 ; a[i] = c*b[i];
9 ; }
10 ; and we want to hoist the load of %c out of the loop. This can be done only
11 ; because the dereferenceable attribute is on %c.
12
13 ; CHECK-LABEL: @test1
14 ; CHECK: load i32* %c, align 4
15 ; CHECK: for.body:
16
17 define void @test1(i32* noalias nocapture %a, i32* noalias nocapture readonly %b, i32* nocapture readonly nonnull dereferenceable(4) %c, i32 %n) #0 {
18 entry:
19 %cmp11 = icmp sgt i32 %n, 0
20 br i1 %cmp11, label %for.body, label %for.end
21
22 for.body: ; preds = %entry, %for.inc
23 %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
24 %arrayidx = getelementptr inbounds i32* %a, i64 %indvars.iv
25 %0 = load i32* %arrayidx, align 4
26 %cmp1 = icmp sgt i32 %0, 0
27 br i1 %cmp1, label %if.then, label %for.inc
28
29 if.then: ; preds = %for.body
30 %1 = load i32* %c, align 4
31 %arrayidx3 = getelementptr inbounds i32* %b, i64 %indvars.iv
32 %2 = load i32* %arrayidx3, align 4
33 %mul = mul nsw i32 %2, %1
34 store i32 %mul, i32* %arrayidx, align 4
35 br label %for.inc
36
37 for.inc: ; preds = %for.body, %if.then
38 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
39 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
40 %exitcond = icmp eq i32 %lftr.wideiv, %n
41 br i1 %exitcond, label %for.end, label %for.body
42
43 for.end: ; preds = %for.inc, %entry
44 ret void
45 }
46
47 ; This is the same as @test1, but without the dereferenceable attribute on %c.
48 ; Without this attribute, we should not hoist the load of %c.
49
50 ; CHECK-LABEL: @test2
51 ; CHECK: if.then:
52 ; CHECK: load i32* %c, align 4
53
54 define void @test2(i32* noalias nocapture %a, i32* noalias nocapture readonly %b, i32* nocapture readonly nonnull %c, i32 %n) #0 {
55 entry:
56 %cmp11 = icmp sgt i32 %n, 0
57 br i1 %cmp11, label %for.body, label %for.end
58
59 for.body: ; preds = %entry, %for.inc
60 %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
61 %arrayidx = getelementptr inbounds i32* %a, i64 %indvars.iv
62 %0 = load i32* %arrayidx, align 4
63 %cmp1 = icmp sgt i32 %0, 0
64 br i1 %cmp1, label %if.then, label %for.inc
65
66 if.then: ; preds = %for.body
67 %1 = load i32* %c, align 4
68 %arrayidx3 = getelementptr inbounds i32* %b, i64 %indvars.iv
69 %2 = load i32* %arrayidx3, align 4
70 %mul = mul nsw i32 %2, %1
71 store i32 %mul, i32* %arrayidx, align 4
72 br label %for.inc
73
74 for.inc: ; preds = %for.body, %if.then
75 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
76 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
77 %exitcond = icmp eq i32 %lftr.wideiv, %n
78 br i1 %exitcond, label %for.end, label %for.body
79
80 for.end: ; preds = %for.inc, %entry
81 ret void
82 }
83
84 attributes #0 = { nounwind uwtable }
85