Knowledge (XXG)

restrict

Source 📝

666:...The default aliasing rules of the C and C++ languages do not help the compiler making these decisions (unless restrict is used, all pointer accesses are potential sources of aliasing). This is why Fortran is still a preferred language for numeric programming: it makes writing fast code easier. (In theory the restrict keyword introduced into the C language in the 1999 revision should solve the problem. Compilers have not caught up yet, though. The reason is mainly that too much incorrect code exists which would mislead the compiler and cause it to generate incorrect object code.) 398:
point to different locations and updating the memory location referenced by one pointer will not affect the memory locations referenced by the other pointers. The programmer, not the compiler, is responsible for ensuring that the pointers do not point to identical locations. The compiler can e.g.
56:
that for the lifetime of the pointer, no other pointer will be used to access the object to which it points. This allows the compiler to make optimizations (for example, vectorization) that would not otherwise have been possible.
515:
Note that the real generated code may have different behaviors. Benefit with the above mini-example tends to be small, and in real-life cases large loops doing heavy memory access tends to be what is really helped by restrict.
500:
is loaded only once. Also, since the compiler can rearrange the code more freely, the compiler can generate code that executes faster. In the second version of the above example, the
632:
and library functions marked with it (EXP43-C) a probable source of software bugs, although as of November 2019 no vulnerabilities are known to have been caused by this.
752: 620:
To help prevent incorrect code, some compilers and other tools try to detect when overlapping arguments have been passed to functions with parameters marked
721: 399:
rearrange the code, first loading all memory locations, then performing the operations before committing the results back to memory.
811: 773: 45: 37: 83:
If the compiler knows that there is only one pointer to a memory block, it can produce better optimized code. For instance:
71:. If the declaration of intent is not followed and the object is accessed by an independent pointer, this will result in 20: 781: 696: 574:
mean exactly the same as their C counterpart. Extensions include allowing them to be applied to reference types and
562:
is supported by those three compilers. The exact interpretation of these alternative keywords vary by the compiler:
27: 523:, the compiler only ensures the generated code works properly if the code follows the declaration of intent. 539: 625: 508:
operations, ensuring that the processor won't have to block in the middle of the code to wait until the
649: 68: 520: 72: 678: 745: 193: 64: 538:, but many compilers have equivalents that usually work in both C++ and C, such as the 49: 805: 551: 606: 19:
This article is about the C programming language keyword. For other uses, see
795: 273:; 'load' may have to wait until preceding 'store' completes. 722:"EXP43-C. Avoid undefined behavior when using restrict-qualified pointers" 53: 661: 653: 531: 543: 41: 581:
In Visual C++, multiple no-alias qualifiers are provided:
587:
applies to the function declaration and hints that the
261:; Store r3 to memory location ptrA, updating the value. 321:
keyword is used and the above function is declared as
629: 621: 602: 598: 594: 584: 575: 571: 567: 285:; Have to load a second time to ensure consistency. 519:As mentioned above, how incorrect code behaves is 196:, so the compiler may generate less optimal code: 601:, but the no-alias hint does not propagate as in 425:; Also, all 'load's in the beginning ... 566:In Unix-style compilers such as GCC and Clang, 798:: the original rationale behind the definition 658:What every programmer should know about memory 8: 496:The above assembly code is shorter because 504:operations are all taking place after the 782:"How to Use the restrict Qualifier in C" 654:"Memory part 5: What programmers can do" 413:; Note that val is now only loaded once. 683:Using the GNU Compiler Collection (GCC) 641: 482:; ... all 'store's in the end. 7: 534:does not have standard support for 16:Keyword in C (programming language) 746:"ISO/IEC 9899:TC2 Committee Draft" 249:; Perform addition: r3 = r3 + r12. 14: 776:: explanation and examples of use 774:Demystifying The Restrict Keyword 382:then the compiler is allowed to 177:In the above code, the pointers 755:. May 6, 2005. pp. 108–112 1: 697:"Warning Options: -Wrestrict" 597:is used in the same place as 48:declarations. By adding this 225:; Load memory at ptrA to r3. 213:; Load memory at val to r12. 201:; Hypothetical RISC Machine. 52:, a programmer hints to the 21:Restriction (disambiguation) 828: 726:SEI CERT C Coding Standard 605:. It is also extended for 18: 512:operations are complete. 812:C (programming language) 796:Restricted Pointers in C 527:Support by C++ compilers 401: 323: 198: 85: 591:pointer is not aliased. 626:CERT C Coding Standard 63:limits the effects of 44:, that can be used in 28:C programming language 679:"Restricted Pointers" 652:(October 23, 2007). 628:considers misuse of 585:__declspec(restrict) 556:__declspec(restrict) 194:same memory location 40:, introduced by the 73:undefined behavior 616:Compiler warnings 819: 792: 790: 789: 780:Walls, Douglas. 763: 761: 760: 750: 737: 736: 734: 732: 718: 712: 711: 709: 707: 693: 687: 686: 675: 669: 668: 646: 631: 623: 604: 600: 596: 586: 577: 573: 569: 561: 557: 549: 537: 511: 507: 503: 499: 492: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 417: 414: 411: 408: 405: 397: 393: 389: 378: 375: 372: 369: 366: 363: 360: 357: 354: 351: 348: 345: 342: 339: 336: 333: 330: 327: 320: 317:However, if the 313: 310: 307: 304: 301: 298: 295: 292: 289: 286: 283: 280: 277: 274: 271: 268: 265: 262: 259: 256: 253: 250: 247: 244: 241: 238: 235: 232: 229: 226: 223: 220: 217: 214: 211: 208: 205: 202: 188: 184: 180: 173: 170: 167: 164: 161: 158: 155: 152: 149: 146: 143: 140: 137: 134: 131: 128: 125: 122: 119: 116: 113: 110: 107: 104: 101: 98: 95: 92: 89: 65:pointer aliasing 62: 35: 827: 826: 822: 821: 820: 818: 817: 816: 802: 801: 787: 785: 779: 770: 758: 756: 748: 744: 741: 740: 730: 728: 720: 719: 715: 705: 703: 695: 694: 690: 677: 676: 672: 650:Drepper, Ulrich 648: 647: 643: 638: 618: 559: 558:. In addition, 555: 547: 535: 529: 509: 505: 501: 497: 494: 493: 490: 487: 484: 481: 478: 475: 472: 469: 466: 463: 460: 457: 454: 451: 448: 445: 442: 439: 436: 433: 430: 427: 424: 421: 418: 415: 412: 409: 406: 403: 395: 391: 387: 380: 379: 376: 373: 370: 367: 364: 361: 358: 355: 352: 349: 346: 343: 340: 337: 334: 331: 328: 325: 318: 315: 314: 311: 308: 305: 302: 299: 296: 293: 290: 287: 284: 281: 278: 275: 272: 269: 266: 263: 260: 257: 254: 251: 248: 245: 242: 239: 236: 233: 230: 227: 224: 221: 218: 215: 212: 209: 206: 203: 200: 186: 182: 178: 175: 174: 171: 168: 165: 162: 159: 156: 153: 150: 147: 144: 141: 138: 135: 132: 129: 126: 123: 120: 117: 114: 111: 108: 105: 102: 99: 96: 93: 90: 87: 81: 60: 31: 24: 17: 12: 11: 5: 825: 823: 815: 814: 804: 803: 800: 799: 793: 777: 769: 768:External links 766: 765: 764: 739: 738: 713: 688: 670: 640: 639: 637: 634: 617: 614: 613: 612: 611: 610: 592: 579: 528: 525: 402: 324: 199: 86: 80: 77: 50:type qualifier 15: 13: 10: 9: 6: 4: 3: 2: 824: 813: 810: 809: 807: 797: 794: 783: 778: 775: 772: 771: 767: 754: 747: 743: 742: 727: 723: 717: 714: 702: 698: 692: 689: 684: 680: 674: 671: 667: 663: 659: 655: 651: 645: 642: 635: 633: 627: 615: 608: 593: 590: 583: 582: 580: 565: 564: 563: 553: 545: 541: 533: 526: 524: 522: 517: 513: 400: 385: 322: 197: 195: 192:refer to the 191: 84: 78: 76: 74: 70: 69:optimizations 66: 58: 55: 51: 47: 43: 39: 34: 29: 22: 786:. Retrieved 757:. Retrieved 729:. Retrieved 725: 716: 704:. Retrieved 700: 691: 682: 673: 665: 657: 644: 619: 588: 572:__restrict__ 548:__restrict__ 530: 518: 514: 495: 383: 381: 316: 189: 176: 82: 79:Optimization 59: 42:C99 standard 32: 25: 731:19 November 706:19 November 607:union types 788:2012-11-21 759:2008-12-22 636:References 595:__restrict 568:__restrict 560:__restrict 552:Visual C++ 329:updatePtrs 91:updatePtrs 784:. Oracle™ 521:undefined 67:, aiding 806:Category 630:restrict 622:restrict 603:restrict 599:restrict 589:returned 536:restrict 371:restrict 356:restrict 341:restrict 319:restrict 61:restrict 54:compiler 33:restrict 662:lwn.net 542:'s and 46:pointer 38:keyword 26:In the 624:. The 550:, and 394:, and 384:assume 365:size_t 350:size_t 335:size_t 246:  185:, and 121:size_t 109:size_t 97:size_t 749:(PDF) 544:Clang 510:store 502:store 386:that 190:might 36:is a 733:2019 708:2019 576:this 570:and 506:load 392:ptrB 388:ptrA 359:ptrB 344:ptrA 326:void 183:ptrB 179:ptrA 157:ptrB 139:ptrA 115:ptrB 103:ptrA 88:void 753:ISO 701:GCC 554:'s 546:'s 540:GCC 532:C++ 498:val 485:str 473:str 470:r12 455:add 452:r12 437:add 428:ldr 416:ldr 407:r12 404:ldr 396:val 374:val 306:str 303:r12 288:add 279:r12 276:ldr 264:ldr 252:str 243:r12 228:add 216:ldr 207:r12 204:ldr 187:val 166:val 148:val 127:val 808:: 751:. 724:. 699:. 681:. 664:. 660:. 656:. 488:r4 476:r3 464:r4 458:r4 446:r3 440:r3 431:r4 419:r3 390:, 377:); 309:r3 297:r3 291:r3 267:r3 255:r3 237:r3 231:r3 219:r3 181:, 160:+= 142:+= 75:. 30:, 791:. 762:. 735:. 710:. 685:. 609:. 578:. 491:, 479:, 467:, 461:, 449:, 443:, 434:, 422:, 410:, 368:* 362:, 353:* 347:, 338:* 332:( 312:, 300:, 294:, 282:, 270:, 258:, 240:, 234:, 222:, 210:, 172:} 169:; 163:* 154:* 151:; 145:* 136:* 133:{ 130:) 124:* 118:, 112:* 106:, 100:* 94:( 23:.

Index

Restriction (disambiguation)
C programming language
keyword
C99 standard
pointer
type qualifier
compiler
pointer aliasing
optimizations
undefined behavior
same memory location
undefined
C++
GCC
Clang
Visual C++
union types
CERT C Coding Standard
Drepper, Ulrich
"Memory part 5: What programmers can do"
lwn.net
"Restricted Pointers"
"Warning Options: -Wrestrict"
"EXP43-C. Avoid undefined behavior when using restrict-qualified pointers"
"ISO/IEC 9899:TC2 Committee Draft"
ISO
Demystifying The Restrict Keyword
"How to Use the restrict Qualifier in C"
Restricted Pointers in C
Category

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.