Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 1 | //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements some functions that will create standard C libcalls. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Weiming Zhao | 45d4cb9 | 2015-11-24 18:57:06 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constants.h" |
| 19 | #include "llvm/IR/DataLayout.h" |
| 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/IR/IRBuilder.h" |
| 22 | #include "llvm/IR/Intrinsics.h" |
| 23 | #include "llvm/IR/LLVMContext.h" |
| 24 | #include "llvm/IR/Module.h" |
| 25 | #include "llvm/IR/Type.h" |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "build-libcalls" |
| 30 | |
| 31 | //- Infer Attributes ---------------------------------------------------------// |
| 32 | |
| 33 | STATISTIC(NumReadNone, "Number of functions inferred as readnone"); |
| 34 | STATISTIC(NumReadOnly, "Number of functions inferred as readonly"); |
| 35 | STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly"); |
| 36 | STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind"); |
| 37 | STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture"); |
| 38 | STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly"); |
| 39 | STATISTIC(NumNoAlias, "Number of function returns inferred as noalias"); |
| 40 | STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns"); |
| 41 | |
| 42 | static bool setDoesNotAccessMemory(Function &F) { |
| 43 | if (F.doesNotAccessMemory()) |
| 44 | return false; |
| 45 | F.setDoesNotAccessMemory(); |
| 46 | ++NumReadNone; |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | static bool setOnlyReadsMemory(Function &F) { |
| 51 | if (F.onlyReadsMemory()) |
| 52 | return false; |
| 53 | F.setOnlyReadsMemory(); |
| 54 | ++NumReadOnly; |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | static bool setOnlyAccessesArgMemory(Function &F) { |
| 59 | if (F.onlyAccessesArgMemory()) |
| 60 | return false; |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 61 | F.setOnlyAccessesArgMemory(); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 62 | ++NumArgMemOnly; |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | static bool setDoesNotThrow(Function &F) { |
| 67 | if (F.doesNotThrow()) |
| 68 | return false; |
| 69 | F.setDoesNotThrow(); |
| 70 | ++NumNoUnwind; |
| 71 | return true; |
| 72 | } |
| 73 | |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 74 | static bool setRetDoesNotAlias(Function &F) { |
| 75 | if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias)) |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 76 | return false; |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 77 | F.addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 78 | ++NumNoAlias; |
| 79 | return true; |
| 80 | } |
| 81 | |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 82 | static bool setDoesNotCapture(Function &F, unsigned ArgNo) { |
| 83 | if (F.hasParamAttribute(ArgNo, Attribute::NoCapture)) |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 84 | return false; |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 85 | F.addParamAttr(ArgNo, Attribute::NoCapture); |
| 86 | ++NumNoCapture; |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) { |
| 91 | if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly)) |
| 92 | return false; |
| 93 | F.addParamAttr(ArgNo, Attribute::ReadOnly); |
| 94 | ++NumReadOnlyArg; |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | static bool setRetNonNull(Function &F) { |
| 99 | assert(F.getReturnType()->isPointerTy() && |
| 100 | "nonnull applies only to pointers"); |
| 101 | if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NonNull)) |
| 102 | return false; |
| 103 | F.addAttribute(AttributeList::ReturnIndex, Attribute::NonNull); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 104 | ++NumNonNull; |
| 105 | return true; |
| 106 | } |
| 107 | |
Sriraman Tallam | 182f2df | 2018-04-10 23:32:36 +0000 | [diff] [blame] | 108 | static bool setNonLazyBind(Function &F) { |
| 109 | if (F.hasFnAttribute(Attribute::NonLazyBind)) |
| 110 | return false; |
| 111 | F.addFnAttr(Attribute::NonLazyBind); |
| 112 | return true; |
| 113 | } |
| 114 | |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 115 | bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 116 | LibFunc TheLibFunc; |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 117 | if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc))) |
| 118 | return false; |
| 119 | |
| 120 | bool Changed = false; |
Sriraman Tallam | 182f2df | 2018-04-10 23:32:36 +0000 | [diff] [blame] | 121 | |
| 122 | if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT()) |
| 123 | Changed |= setNonLazyBind(F); |
| 124 | |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 125 | switch (TheLibFunc) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 126 | case LibFunc_strlen: |
Matthias Braun | 60b40b8 | 2017-05-05 20:25:50 +0000 | [diff] [blame] | 127 | case LibFunc_wcslen: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 128 | Changed |= setOnlyReadsMemory(F); |
| 129 | Changed |= setDoesNotThrow(F); |
Xin Tong | 9d2a5b1 | 2017-06-18 03:10:26 +0000 | [diff] [blame] | 130 | Changed |= setOnlyAccessesArgMemory(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 131 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 132 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 133 | case LibFunc_strchr: |
| 134 | case LibFunc_strrchr: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 135 | Changed |= setOnlyReadsMemory(F); |
| 136 | Changed |= setDoesNotThrow(F); |
| 137 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 138 | case LibFunc_strtol: |
| 139 | case LibFunc_strtod: |
| 140 | case LibFunc_strtof: |
| 141 | case LibFunc_strtoul: |
| 142 | case LibFunc_strtoll: |
| 143 | case LibFunc_strtold: |
| 144 | case LibFunc_strtoull: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 145 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 146 | Changed |= setDoesNotCapture(F, 1); |
| 147 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 148 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 149 | case LibFunc_strcpy: |
| 150 | case LibFunc_stpcpy: |
| 151 | case LibFunc_strcat: |
| 152 | case LibFunc_strncat: |
| 153 | case LibFunc_strncpy: |
| 154 | case LibFunc_stpncpy: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 155 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 156 | Changed |= setDoesNotCapture(F, 1); |
| 157 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 158 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 159 | case LibFunc_strxfrm: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 160 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 161 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 162 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 163 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 164 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 165 | case LibFunc_strcmp: // 0,1 |
| 166 | case LibFunc_strspn: // 0,1 |
| 167 | case LibFunc_strncmp: // 0,1 |
| 168 | case LibFunc_strcspn: // 0,1 |
| 169 | case LibFunc_strcoll: // 0,1 |
| 170 | case LibFunc_strcasecmp: // 0,1 |
| 171 | case LibFunc_strncasecmp: // |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 172 | Changed |= setOnlyReadsMemory(F); |
| 173 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 174 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 175 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 176 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 177 | case LibFunc_strstr: |
| 178 | case LibFunc_strpbrk: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 179 | Changed |= setOnlyReadsMemory(F); |
| 180 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 181 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 182 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 183 | case LibFunc_strtok: |
| 184 | case LibFunc_strtok_r: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 185 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 186 | Changed |= setDoesNotCapture(F, 1); |
| 187 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 188 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 189 | case LibFunc_scanf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 190 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 191 | Changed |= setDoesNotCapture(F, 0); |
| 192 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 193 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 194 | case LibFunc_setbuf: |
| 195 | case LibFunc_setvbuf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 196 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 197 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 198 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 199 | case LibFunc_strdup: |
| 200 | case LibFunc_strndup: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 201 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 202 | Changed |= setRetDoesNotAlias(F); |
| 203 | Changed |= setDoesNotCapture(F, 0); |
| 204 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 205 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 206 | case LibFunc_stat: |
| 207 | case LibFunc_statvfs: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 208 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 209 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 210 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 211 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 212 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 213 | case LibFunc_sscanf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 214 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 215 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 216 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 217 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 218 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 219 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 220 | case LibFunc_sprintf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 221 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 222 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 223 | Changed |= setDoesNotCapture(F, 1); |
| 224 | Changed |= setOnlyReadsMemory(F, 1); |
| 225 | return Changed; |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 226 | case LibFunc_snprintf: |
| 227 | Changed |= setDoesNotThrow(F); |
| 228 | Changed |= setDoesNotCapture(F, 0); |
| 229 | Changed |= setDoesNotCapture(F, 2); |
| 230 | Changed |= setOnlyReadsMemory(F, 2); |
| 231 | return Changed; |
| 232 | case LibFunc_setitimer: |
| 233 | Changed |= setDoesNotThrow(F); |
| 234 | Changed |= setDoesNotCapture(F, 1); |
| 235 | Changed |= setDoesNotCapture(F, 2); |
| 236 | Changed |= setOnlyReadsMemory(F, 1); |
| 237 | return Changed; |
| 238 | case LibFunc_system: |
| 239 | // May throw; "system" is a valid pthread cancellation point. |
| 240 | Changed |= setDoesNotCapture(F, 0); |
| 241 | Changed |= setOnlyReadsMemory(F, 0); |
| 242 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 243 | case LibFunc_malloc: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 244 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 245 | Changed |= setRetDoesNotAlias(F); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 246 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 247 | case LibFunc_memcmp: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 248 | Changed |= setOnlyReadsMemory(F); |
| 249 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 250 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 251 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 252 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 253 | case LibFunc_memchr: |
| 254 | case LibFunc_memrchr: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 255 | Changed |= setOnlyReadsMemory(F); |
| 256 | Changed |= setDoesNotThrow(F); |
| 257 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 258 | case LibFunc_modf: |
| 259 | case LibFunc_modff: |
| 260 | case LibFunc_modfl: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 261 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 262 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 263 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 264 | case LibFunc_memcpy: |
| 265 | case LibFunc_mempcpy: |
| 266 | case LibFunc_memccpy: |
| 267 | case LibFunc_memmove: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 268 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 269 | Changed |= setDoesNotCapture(F, 1); |
| 270 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 271 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 272 | case LibFunc_memcpy_chk: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 273 | Changed |= setDoesNotThrow(F); |
| 274 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 275 | case LibFunc_memalign: |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 276 | Changed |= setRetDoesNotAlias(F); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 277 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 278 | case LibFunc_mkdir: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 279 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 280 | Changed |= setDoesNotCapture(F, 0); |
| 281 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 282 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 283 | case LibFunc_mktime: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 284 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 285 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 286 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 287 | case LibFunc_realloc: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 288 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 289 | Changed |= setRetDoesNotAlias(F); |
| 290 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 291 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 292 | case LibFunc_read: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 293 | // May throw; "read" is a valid pthread cancellation point. |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 294 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 295 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 296 | case LibFunc_rewind: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 297 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 298 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 299 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 300 | case LibFunc_rmdir: |
| 301 | case LibFunc_remove: |
| 302 | case LibFunc_realpath: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 303 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 304 | Changed |= setDoesNotCapture(F, 0); |
| 305 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 306 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 307 | case LibFunc_rename: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 308 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 309 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 310 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 311 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 312 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 313 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 314 | case LibFunc_readlink: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 315 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 316 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 317 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 318 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 319 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 320 | case LibFunc_write: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 321 | // May throw; "write" is a valid pthread cancellation point. |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 322 | Changed |= setDoesNotCapture(F, 1); |
| 323 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 324 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 325 | case LibFunc_bcopy: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 326 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 327 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 328 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 329 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 330 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 331 | case LibFunc_bcmp: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 332 | Changed |= setDoesNotThrow(F); |
| 333 | Changed |= setOnlyReadsMemory(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 334 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 335 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 336 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 337 | case LibFunc_bzero: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 338 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 339 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 340 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 341 | case LibFunc_calloc: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 342 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 343 | Changed |= setRetDoesNotAlias(F); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 344 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 345 | case LibFunc_chmod: |
| 346 | case LibFunc_chown: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 347 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 348 | Changed |= setDoesNotCapture(F, 0); |
| 349 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 350 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 351 | case LibFunc_ctermid: |
| 352 | case LibFunc_clearerr: |
| 353 | case LibFunc_closedir: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 354 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 355 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 356 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 357 | case LibFunc_atoi: |
| 358 | case LibFunc_atol: |
| 359 | case LibFunc_atof: |
| 360 | case LibFunc_atoll: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 361 | Changed |= setDoesNotThrow(F); |
| 362 | Changed |= setOnlyReadsMemory(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 363 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 364 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 365 | case LibFunc_access: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 366 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 367 | Changed |= setDoesNotCapture(F, 0); |
| 368 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 369 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 370 | case LibFunc_fopen: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 371 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 372 | Changed |= setRetDoesNotAlias(F); |
| 373 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 374 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 375 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 376 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 377 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 378 | case LibFunc_fdopen: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 379 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 380 | Changed |= setRetDoesNotAlias(F); |
| 381 | Changed |= setDoesNotCapture(F, 1); |
| 382 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 383 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 384 | case LibFunc_feof: |
| 385 | case LibFunc_free: |
| 386 | case LibFunc_fseek: |
| 387 | case LibFunc_ftell: |
| 388 | case LibFunc_fgetc: |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 389 | case LibFunc_fgetc_unlocked: |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 390 | case LibFunc_fseeko: |
| 391 | case LibFunc_ftello: |
| 392 | case LibFunc_fileno: |
| 393 | case LibFunc_fflush: |
| 394 | case LibFunc_fclose: |
| 395 | case LibFunc_fsetpos: |
| 396 | case LibFunc_flockfile: |
| 397 | case LibFunc_funlockfile: |
| 398 | case LibFunc_ftrylockfile: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 399 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 400 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 401 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 402 | case LibFunc_ferror: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 403 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 404 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 405 | Changed |= setOnlyReadsMemory(F); |
| 406 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 407 | case LibFunc_fputc: |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 408 | case LibFunc_fputc_unlocked: |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 409 | case LibFunc_fstat: |
| 410 | case LibFunc_frexp: |
| 411 | case LibFunc_frexpf: |
| 412 | case LibFunc_frexpl: |
| 413 | case LibFunc_fstatvfs: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 414 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 415 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 416 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 417 | case LibFunc_fgets: |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 418 | case LibFunc_fgets_unlocked: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 419 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 420 | Changed |= setDoesNotCapture(F, 2); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 421 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 422 | case LibFunc_fread: |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 423 | case LibFunc_fread_unlocked: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 424 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 425 | Changed |= setDoesNotCapture(F, 0); |
| 426 | Changed |= setDoesNotCapture(F, 3); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 427 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 428 | case LibFunc_fwrite: |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 429 | case LibFunc_fwrite_unlocked: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 430 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 431 | Changed |= setDoesNotCapture(F, 0); |
| 432 | Changed |= setDoesNotCapture(F, 3); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 433 | // FIXME: readonly #1? |
| 434 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 435 | case LibFunc_fputs: |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 436 | case LibFunc_fputs_unlocked: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 437 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 438 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 439 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 440 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 441 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 442 | case LibFunc_fscanf: |
| 443 | case LibFunc_fprintf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 444 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 445 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 446 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 447 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 448 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 449 | case LibFunc_fgetpos: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 450 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 451 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 452 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 453 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 454 | case LibFunc_getc: |
| 455 | case LibFunc_getlogin_r: |
| 456 | case LibFunc_getc_unlocked: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 457 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 458 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 459 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 460 | case LibFunc_getenv: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 461 | Changed |= setDoesNotThrow(F); |
| 462 | Changed |= setOnlyReadsMemory(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 463 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 464 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 465 | case LibFunc_gets: |
| 466 | case LibFunc_getchar: |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 467 | case LibFunc_getchar_unlocked: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 468 | Changed |= setDoesNotThrow(F); |
| 469 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 470 | case LibFunc_getitimer: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 471 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 472 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 473 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 474 | case LibFunc_getpwnam: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 475 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 476 | Changed |= setDoesNotCapture(F, 0); |
| 477 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 478 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 479 | case LibFunc_ungetc: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 480 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 481 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 482 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 483 | case LibFunc_uname: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 484 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 485 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 486 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 487 | case LibFunc_unlink: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 488 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 489 | Changed |= setDoesNotCapture(F, 0); |
| 490 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 491 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 492 | case LibFunc_unsetenv: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 493 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 494 | Changed |= setDoesNotCapture(F, 0); |
| 495 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 496 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 497 | case LibFunc_utime: |
| 498 | case LibFunc_utimes: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 499 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 500 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 501 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 502 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 503 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 504 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 505 | case LibFunc_putc: |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 506 | case LibFunc_putc_unlocked: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 507 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 508 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 509 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 510 | case LibFunc_puts: |
| 511 | case LibFunc_printf: |
| 512 | case LibFunc_perror: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 513 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 514 | Changed |= setDoesNotCapture(F, 0); |
| 515 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 516 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 517 | case LibFunc_pread: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 518 | // May throw; "pread" is a valid pthread cancellation point. |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 519 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 520 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 521 | case LibFunc_pwrite: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 522 | // May throw; "pwrite" is a valid pthread cancellation point. |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 523 | Changed |= setDoesNotCapture(F, 1); |
| 524 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 525 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 526 | case LibFunc_putchar: |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 527 | case LibFunc_putchar_unlocked: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 528 | Changed |= setDoesNotThrow(F); |
| 529 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 530 | case LibFunc_popen: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 531 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 532 | Changed |= setRetDoesNotAlias(F); |
| 533 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 534 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 535 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 536 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 537 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 538 | case LibFunc_pclose: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 539 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 540 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 541 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 542 | case LibFunc_vscanf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 543 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 544 | Changed |= setDoesNotCapture(F, 0); |
| 545 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 546 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 547 | case LibFunc_vsscanf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 548 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 549 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 550 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 551 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 552 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 553 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 554 | case LibFunc_vfscanf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 555 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 556 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 557 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 558 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 559 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 560 | case LibFunc_valloc: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 561 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 562 | Changed |= setRetDoesNotAlias(F); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 563 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 564 | case LibFunc_vprintf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 565 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 566 | Changed |= setDoesNotCapture(F, 0); |
| 567 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 568 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 569 | case LibFunc_vfprintf: |
| 570 | case LibFunc_vsprintf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 571 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 572 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 573 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 574 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 575 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 576 | case LibFunc_vsnprintf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 577 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 578 | Changed |= setDoesNotCapture(F, 0); |
| 579 | Changed |= setDoesNotCapture(F, 2); |
| 580 | Changed |= setOnlyReadsMemory(F, 2); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 581 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 582 | case LibFunc_open: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 583 | // May throw; "open" is a valid pthread cancellation point. |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 584 | Changed |= setDoesNotCapture(F, 0); |
| 585 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 586 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 587 | case LibFunc_opendir: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 588 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 589 | Changed |= setRetDoesNotAlias(F); |
| 590 | Changed |= setDoesNotCapture(F, 0); |
| 591 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 592 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 593 | case LibFunc_tmpfile: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 594 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 595 | Changed |= setRetDoesNotAlias(F); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 596 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 597 | case LibFunc_times: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 598 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 599 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 600 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 601 | case LibFunc_htonl: |
| 602 | case LibFunc_htons: |
| 603 | case LibFunc_ntohl: |
| 604 | case LibFunc_ntohs: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 605 | Changed |= setDoesNotThrow(F); |
| 606 | Changed |= setDoesNotAccessMemory(F); |
| 607 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 608 | case LibFunc_lstat: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 609 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 610 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 611 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 612 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 613 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 614 | case LibFunc_lchown: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 615 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 616 | Changed |= setDoesNotCapture(F, 0); |
| 617 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 618 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 619 | case LibFunc_qsort: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 620 | // May throw; places call through function pointer. |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 621 | Changed |= setDoesNotCapture(F, 3); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 622 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 623 | case LibFunc_dunder_strdup: |
| 624 | case LibFunc_dunder_strndup: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 625 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 626 | Changed |= setRetDoesNotAlias(F); |
| 627 | Changed |= setDoesNotCapture(F, 0); |
| 628 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 629 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 630 | case LibFunc_dunder_strtok_r: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 631 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 632 | Changed |= setDoesNotCapture(F, 1); |
| 633 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 634 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 635 | case LibFunc_under_IO_getc: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 636 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 637 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 638 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 639 | case LibFunc_under_IO_putc: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 640 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 641 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 642 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 643 | case LibFunc_dunder_isoc99_scanf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 644 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 645 | Changed |= setDoesNotCapture(F, 0); |
| 646 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 647 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 648 | case LibFunc_stat64: |
| 649 | case LibFunc_lstat64: |
| 650 | case LibFunc_statvfs64: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 651 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 652 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 653 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 654 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 655 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 656 | case LibFunc_dunder_isoc99_sscanf: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 657 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 658 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 659 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 660 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 661 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 662 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 663 | case LibFunc_fopen64: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 664 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 665 | Changed |= setRetDoesNotAlias(F); |
| 666 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 667 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 668 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 669 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 670 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 671 | case LibFunc_fseeko64: |
| 672 | case LibFunc_ftello64: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 673 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 674 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 675 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 676 | case LibFunc_tmpfile64: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 677 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 678 | Changed |= setRetDoesNotAlias(F); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 679 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 680 | case LibFunc_fstat64: |
| 681 | case LibFunc_fstatvfs64: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 682 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 683 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 684 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 685 | case LibFunc_open64: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 686 | // May throw; "open" is a valid pthread cancellation point. |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 687 | Changed |= setDoesNotCapture(F, 0); |
| 688 | Changed |= setOnlyReadsMemory(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 689 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 690 | case LibFunc_gettimeofday: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 691 | // Currently some platforms have the restrict keyword on the arguments to |
| 692 | // gettimeofday. To be conservative, do not add noalias to gettimeofday's |
| 693 | // arguments. |
| 694 | Changed |= setDoesNotThrow(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 695 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 696 | Changed |= setDoesNotCapture(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 697 | return Changed; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 698 | case LibFunc_Znwj: // new(unsigned int) |
| 699 | case LibFunc_Znwm: // new(unsigned long) |
| 700 | case LibFunc_Znaj: // new[](unsigned int) |
| 701 | case LibFunc_Znam: // new[](unsigned long) |
| 702 | case LibFunc_msvc_new_int: // new(unsigned int) |
| 703 | case LibFunc_msvc_new_longlong: // new(unsigned long long) |
| 704 | case LibFunc_msvc_new_array_int: // new[](unsigned int) |
| 705 | case LibFunc_msvc_new_array_longlong: // new[](unsigned long long) |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 706 | // Operator new always returns a nonnull noalias pointer |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 707 | Changed |= setRetNonNull(F); |
| 708 | Changed |= setRetDoesNotAlias(F); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 709 | return Changed; |
David Bolvansky | 3ea50f9 | 2018-04-25 04:33:36 +0000 | [diff] [blame] | 710 | // TODO: add LibFunc entries for: |
| 711 | // case LibFunc_memset_pattern4: |
| 712 | // case LibFunc_memset_pattern8: |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 713 | case LibFunc_memset_pattern16: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 714 | Changed |= setOnlyAccessesArgMemory(F); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 715 | Changed |= setDoesNotCapture(F, 0); |
Ahmed Bougacha | 44c1987 | 2016-04-27 19:04:43 +0000 | [diff] [blame] | 716 | Changed |= setDoesNotCapture(F, 1); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 717 | Changed |= setOnlyReadsMemory(F, 1); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 718 | return Changed; |
| 719 | // int __nvvm_reflect(const char *) |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 720 | case LibFunc_nvvm_reflect: |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 721 | Changed |= setDoesNotAccessMemory(F); |
| 722 | Changed |= setDoesNotThrow(F); |
| 723 | return Changed; |
| 724 | |
| 725 | default: |
| 726 | // FIXME: It'd be really nice to cover all the library functions we're |
| 727 | // aware of here. |
| 728 | return false; |
| 729 | } |
| 730 | } |
| 731 | |
Dmitry Venikov | e5fbf59 | 2018-01-11 06:33:00 +0000 | [diff] [blame] | 732 | bool llvm::hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty, |
| 733 | LibFunc DoubleFn, LibFunc FloatFn, |
| 734 | LibFunc LongDoubleFn) { |
| 735 | switch (Ty->getTypeID()) { |
| 736 | case Type::FloatTyID: |
| 737 | return TLI->has(FloatFn); |
| 738 | case Type::DoubleTyID: |
| 739 | return TLI->has(DoubleFn); |
| 740 | default: |
| 741 | return TLI->has(LongDoubleFn); |
| 742 | } |
| 743 | } |
| 744 | |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 745 | //- Emit LibCalls ------------------------------------------------------------// |
| 746 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 747 | Value *llvm::castToCStr(Value *V, IRBuilder<> &B) { |
Matt Arsenault | be55888 | 2014-04-23 20:58:57 +0000 | [diff] [blame] | 748 | unsigned AS = V->getType()->getPointerAddressSpace(); |
| 749 | return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr"); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 752 | Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 753 | const TargetLibraryInfo *TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 754 | if (!TLI->has(LibFunc_strlen)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 755 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 756 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 757 | Module *M = B.GetInsertBlock()->getModule(); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 758 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 759 | Constant *StrLen = M->getOrInsertFunction("strlen", DL.getIntPtrType(Context), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 760 | B.getInt8PtrTy()); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 761 | inferLibFuncAttributes(*M->getFunction("strlen"), *TLI); |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 762 | CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), "strlen"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 763 | if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts())) |
| 764 | CI->setCallingConv(F->getCallingConv()); |
| 765 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 766 | return CI; |
| 767 | } |
| 768 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 769 | Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 770 | const TargetLibraryInfo *TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 771 | if (!TLI->has(LibFunc_strchr)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 772 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 773 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 774 | Module *M = B.GetInsertBlock()->getModule(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 775 | Type *I8Ptr = B.getInt8PtrTy(); |
| 776 | Type *I32Ty = B.getInt32Ty(); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 777 | Constant *StrChr = |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 778 | M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 779 | inferLibFuncAttributes(*M->getFunction("strchr"), *TLI); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 780 | CallInst *CI = B.CreateCall( |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 781 | StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 782 | if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts())) |
| 783 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 784 | return CI; |
| 785 | } |
| 786 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 787 | Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 788 | const DataLayout &DL, const TargetLibraryInfo *TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 789 | if (!TLI->has(LibFunc_strncmp)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 790 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 791 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 792 | Module *M = B.GetInsertBlock()->getModule(); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 793 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 794 | Value *StrNCmp = M->getOrInsertFunction("strncmp", B.getInt32Ty(), |
| 795 | B.getInt8PtrTy(), B.getInt8PtrTy(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 796 | DL.getIntPtrType(Context)); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 797 | inferLibFuncAttributes(*M->getFunction("strncmp"), *TLI); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 798 | CallInst *CI = B.CreateCall( |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 799 | StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "strncmp"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 800 | |
| 801 | if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts())) |
| 802 | CI->setCallingConv(F->getCallingConv()); |
| 803 | |
Benjamin Kramer | 1118860 | 2010-06-15 21:34:25 +0000 | [diff] [blame] | 804 | return CI; |
| 805 | } |
| 806 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 807 | Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 808 | const TargetLibraryInfo *TLI, StringRef Name) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 809 | if (!TLI->has(LibFunc_strcpy)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 810 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 811 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 812 | Module *M = B.GetInsertBlock()->getModule(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 813 | Type *I8Ptr = B.getInt8PtrTy(); |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 814 | Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 815 | inferLibFuncAttributes(*M->getFunction(Name), *TLI); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 816 | CallInst *CI = |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 817 | B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 818 | if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts())) |
| 819 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 820 | return CI; |
| 821 | } |
| 822 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 823 | Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 824 | const TargetLibraryInfo *TLI, StringRef Name) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 825 | if (!TLI->has(LibFunc_strncpy)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 826 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 827 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 828 | Module *M = B.GetInsertBlock()->getModule(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 829 | Type *I8Ptr = B.getInt8PtrTy(); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 830 | Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 831 | Len->getType()); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 832 | inferLibFuncAttributes(*M->getFunction(Name), *TLI); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 833 | CallInst *CI = B.CreateCall( |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 834 | StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, "strncpy"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 835 | if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts())) |
| 836 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 43dc11c | 2010-03-11 01:25:07 +0000 | [diff] [blame] | 837 | return CI; |
| 838 | } |
| 839 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 840 | Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 841 | IRBuilder<> &B, const DataLayout &DL, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 842 | const TargetLibraryInfo *TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 843 | if (!TLI->has(LibFunc_memcpy_chk)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 844 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 845 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 846 | Module *M = B.GetInsertBlock()->getModule(); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 847 | AttributeList AS; |
| 848 | AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex, |
| 849 | Attribute::NoUnwind); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 850 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 851 | Value *MemCpy = M->getOrInsertFunction( |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 852 | "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 853 | B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 854 | DL.getIntPtrType(Context)); |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 855 | Dst = castToCStr(Dst, B); |
| 856 | Src = castToCStr(Src, B); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 857 | CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize}); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 858 | if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts())) |
| 859 | CI->setCallingConv(F->getCallingConv()); |
Evan Cheng | d9e8223 | 2010-03-23 15:48:04 +0000 | [diff] [blame] | 860 | return CI; |
| 861 | } |
| 862 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 863 | Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 864 | const DataLayout &DL, const TargetLibraryInfo *TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 865 | if (!TLI->has(LibFunc_memchr)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 866 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 867 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 868 | Module *M = B.GetInsertBlock()->getModule(); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 869 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 870 | Value *MemChr = M->getOrInsertFunction("memchr", B.getInt8PtrTy(), |
| 871 | B.getInt8PtrTy(), B.getInt32Ty(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 872 | DL.getIntPtrType(Context)); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 873 | inferLibFuncAttributes(*M->getFunction("memchr"), *TLI); |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 874 | CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, "memchr"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 875 | |
| 876 | if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts())) |
| 877 | CI->setCallingConv(F->getCallingConv()); |
| 878 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 879 | return CI; |
| 880 | } |
| 881 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 882 | Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 883 | const DataLayout &DL, const TargetLibraryInfo *TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 884 | if (!TLI->has(LibFunc_memcmp)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 885 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 886 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 887 | Module *M = B.GetInsertBlock()->getModule(); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 888 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 889 | Value *MemCmp = M->getOrInsertFunction("memcmp", B.getInt32Ty(), |
| 890 | B.getInt8PtrTy(), B.getInt8PtrTy(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 891 | DL.getIntPtrType(Context)); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 892 | inferLibFuncAttributes(*M->getFunction("memcmp"), *TLI); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 893 | CallInst *CI = B.CreateCall( |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 894 | MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "memcmp"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 895 | |
| 896 | if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts())) |
| 897 | CI->setCallingConv(F->getCallingConv()); |
| 898 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 899 | return CI; |
| 900 | } |
| 901 | |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 902 | /// Append a suffix to the function name according to the type of 'Op'. |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 903 | static void appendTypeSuffix(Value *Op, StringRef &Name, |
Sanjay Patel | b50325e | 2016-01-19 19:17:47 +0000 | [diff] [blame] | 904 | SmallString<20> &NameBuffer) { |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 905 | if (!Op->getType()->isDoubleTy()) { |
| 906 | NameBuffer += Name; |
| 907 | |
| 908 | if (Op->getType()->isFloatTy()) |
| 909 | NameBuffer += 'f'; |
| 910 | else |
| 911 | NameBuffer += 'l'; |
| 912 | |
| 913 | Name = NameBuffer; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame^] | 914 | } |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 917 | Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 918 | const AttributeList &Attrs) { |
Benjamin Kramer | b106bcc | 2011-11-15 19:12:09 +0000 | [diff] [blame] | 919 | SmallString<20> NameBuffer; |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 920 | appendTypeSuffix(Op, Name, NameBuffer); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 921 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 922 | Module *M = B.GetInsertBlock()->getModule(); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 923 | Value *Callee = M->getOrInsertFunction(Name, Op->getType(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 924 | Op->getType()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 925 | CallInst *CI = B.CreateCall(Callee, Op, Name); |
Matt Arsenault | 6a288c1 | 2017-05-03 02:26:10 +0000 | [diff] [blame] | 926 | |
| 927 | // The incoming attribute set may have come from a speculatable intrinsic, but |
| 928 | // is being replaced with a library call which is not allowed to be |
| 929 | // speculatable. |
| 930 | CI->setAttributes(Attrs.removeAttribute(B.getContext(), |
| 931 | AttributeList::FunctionIndex, |
| 932 | Attribute::Speculatable)); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 933 | if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) |
| 934 | CI->setCallingConv(F->getCallingConv()); |
| 935 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 936 | return CI; |
| 937 | } |
| 938 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 939 | Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 940 | IRBuilder<> &B, const AttributeList &Attrs) { |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 941 | SmallString<20> NameBuffer; |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 942 | appendTypeSuffix(Op1, Name, NameBuffer); |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 943 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 944 | Module *M = B.GetInsertBlock()->getModule(); |
Sanjay Patel | b50325e | 2016-01-19 19:17:47 +0000 | [diff] [blame] | 945 | Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 946 | Op2->getType()); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 947 | CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name); |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 948 | CI->setAttributes(Attrs); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 949 | if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) |
| 950 | CI->setCallingConv(F->getCallingConv()); |
| 951 | |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 952 | return CI; |
| 953 | } |
| 954 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 955 | Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 956 | const TargetLibraryInfo *TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 957 | if (!TLI->has(LibFunc_putchar)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 958 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 959 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 960 | Module *M = B.GetInsertBlock()->getModule(); |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 961 | Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(), B.getInt32Ty()); |
Craig Topper | 0f5063c | 2017-03-17 23:48:02 +0000 | [diff] [blame] | 962 | inferLibFuncAttributes(*M->getFunction("putchar"), *TLI); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 963 | CallInst *CI = B.CreateCall(PutChar, |
| 964 | B.CreateIntCast(Char, |
| 965 | B.getInt32Ty(), |
| 966 | /*isSigned*/true, |
| 967 | "chari"), |
| 968 | "putchar"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 969 | |
| 970 | if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts())) |
| 971 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 972 | return CI; |
| 973 | } |
| 974 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 975 | Value *llvm::emitPutS(Value *Str, IRBuilder<> &B, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 976 | const TargetLibraryInfo *TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 977 | if (!TLI->has(LibFunc_puts)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 978 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 979 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 980 | Module *M = B.GetInsertBlock()->getModule(); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 981 | Value *PutS = |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 982 | M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy()); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 983 | inferLibFuncAttributes(*M->getFunction("puts"), *TLI); |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 984 | CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), "puts"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 985 | if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts())) |
| 986 | CI->setCallingConv(F->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 987 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 990 | Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 991 | const TargetLibraryInfo *TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 992 | if (!TLI->has(LibFunc_fputc)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 993 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 994 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 995 | Module *M = B.GetInsertBlock()->getModule(); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 996 | Constant *F = M->getOrInsertFunction("fputc", B.getInt32Ty(), B.getInt32Ty(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 997 | File->getType()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 998 | if (File->getType()->isPointerTy()) |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 999 | inferLibFuncAttributes(*M->getFunction("fputc"), *TLI); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 1000 | Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, |
| 1001 | "chari"); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1002 | CallInst *CI = B.CreateCall(F, {Char, File}, "fputc"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 1003 | |
| 1004 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 1005 | CI->setCallingConv(Fn->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 1006 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 1009 | Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B, |
| 1010 | const TargetLibraryInfo *TLI) { |
| 1011 | if (!TLI->has(LibFunc_fputc_unlocked)) |
| 1012 | return nullptr; |
| 1013 | |
| 1014 | Module *M = B.GetInsertBlock()->getModule(); |
| 1015 | Constant *F = M->getOrInsertFunction("fputc_unlocked", B.getInt32Ty(), |
| 1016 | B.getInt32Ty(), File->getType()); |
| 1017 | if (File->getType()->isPointerTy()) |
| 1018 | inferLibFuncAttributes(*M->getFunction("fputc_unlocked"), *TLI); |
| 1019 | Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari"); |
| 1020 | CallInst *CI = B.CreateCall(F, {Char, File}, "fputc_unlocked"); |
| 1021 | |
| 1022 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 1023 | CI->setCallingConv(Fn->getCallingConv()); |
| 1024 | return CI; |
| 1025 | } |
| 1026 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 1027 | Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1028 | const TargetLibraryInfo *TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 1029 | if (!TLI->has(LibFunc_fputs)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1030 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 1031 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 1032 | Module *M = B.GetInsertBlock()->getModule(); |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 1033 | StringRef FPutsName = TLI->getName(LibFunc_fputs); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 1034 | Constant *F = M->getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 1035 | FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 1036 | if (File->getType()->isPointerTy()) |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 1037 | inferLibFuncAttributes(*M->getFunction(FPutsName), *TLI); |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 1038 | CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 1039 | |
| 1040 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 1041 | CI->setCallingConv(Fn->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 1042 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 1045 | Value *llvm::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B, |
| 1046 | const TargetLibraryInfo *TLI) { |
| 1047 | if (!TLI->has(LibFunc_fputs_unlocked)) |
| 1048 | return nullptr; |
| 1049 | |
| 1050 | Module *M = B.GetInsertBlock()->getModule(); |
| 1051 | StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked); |
| 1052 | Constant *F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(), |
| 1053 | B.getInt8PtrTy(), File->getType()); |
| 1054 | if (File->getType()->isPointerTy()) |
| 1055 | inferLibFuncAttributes(*M->getFunction(FPutsUnlockedName), *TLI); |
| 1056 | CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs_unlocked"); |
| 1057 | |
| 1058 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 1059 | CI->setCallingConv(Fn->getCallingConv()); |
| 1060 | return CI; |
| 1061 | } |
| 1062 | |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 1063 | Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1064 | const DataLayout &DL, const TargetLibraryInfo *TLI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 1065 | if (!TLI->has(LibFunc_fwrite)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1066 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 1067 | |
Sanjay Patel | d4af297 | 2016-01-19 19:58:49 +0000 | [diff] [blame] | 1068 | Module *M = B.GetInsertBlock()->getModule(); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 1069 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 1070 | StringRef FWriteName = TLI->getName(LibFunc_fwrite); |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 1071 | Constant *F = M->getOrInsertFunction( |
| 1072 | FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 1073 | DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); |
| 1074 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 1075 | if (File->getType()->isPointerTy()) |
Ahmed Bougacha | b0624a2 | 2016-04-27 19:04:40 +0000 | [diff] [blame] | 1076 | inferLibFuncAttributes(*M->getFunction(FWriteName), *TLI); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1077 | CallInst *CI = |
Sanjay Patel | d3112a5 | 2016-01-19 19:46:10 +0000 | [diff] [blame] | 1078 | B.CreateCall(F, {castToCStr(Ptr, B), Size, |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1079 | ConstantInt::get(DL.getIntPtrType(Context), 1), File}); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 1080 | |
| 1081 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 1082 | CI->setCallingConv(Fn->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 1083 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 1084 | } |
Sanjay Patel | b2ab3f2 | 2018-04-18 14:21:31 +0000 | [diff] [blame] | 1085 | |
| 1086 | Value *llvm::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL, |
| 1087 | const TargetLibraryInfo *TLI) { |
| 1088 | if (!TLI->has(LibFunc_malloc)) |
| 1089 | return nullptr; |
| 1090 | |
| 1091 | Module *M = B.GetInsertBlock()->getModule(); |
| 1092 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
| 1093 | Value *Malloc = M->getOrInsertFunction("malloc", B.getInt8PtrTy(), |
| 1094 | DL.getIntPtrType(Context)); |
| 1095 | inferLibFuncAttributes(*M->getFunction("malloc"), *TLI); |
| 1096 | CallInst *CI = B.CreateCall(Malloc, Num, "malloc"); |
| 1097 | |
| 1098 | if (const Function *F = dyn_cast<Function>(Malloc->stripPointerCasts())) |
| 1099 | CI->setCallingConv(F->getCallingConv()); |
| 1100 | |
| 1101 | return CI; |
| 1102 | } |
| 1103 | |
| 1104 | Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs, |
| 1105 | IRBuilder<> &B, const TargetLibraryInfo &TLI) { |
| 1106 | if (!TLI.has(LibFunc_calloc)) |
| 1107 | return nullptr; |
| 1108 | |
| 1109 | Module *M = B.GetInsertBlock()->getModule(); |
| 1110 | const DataLayout &DL = M->getDataLayout(); |
| 1111 | IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext())); |
| 1112 | Value *Calloc = M->getOrInsertFunction("calloc", Attrs, B.getInt8PtrTy(), |
| 1113 | PtrType, PtrType); |
| 1114 | inferLibFuncAttributes(*M->getFunction("calloc"), TLI); |
| 1115 | CallInst *CI = B.CreateCall(Calloc, {Num, Size}, "calloc"); |
| 1116 | |
| 1117 | if (const auto *F = dyn_cast<Function>(Calloc->stripPointerCasts())) |
| 1118 | CI->setCallingConv(F->getCallingConv()); |
| 1119 | |
| 1120 | return CI; |
| 1121 | } |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 1122 | |
| 1123 | Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, |
| 1124 | IRBuilder<> &B, const DataLayout &DL, |
| 1125 | const TargetLibraryInfo *TLI) { |
| 1126 | if (!TLI->has(LibFunc_fwrite_unlocked)) |
| 1127 | return nullptr; |
| 1128 | |
| 1129 | Module *M = B.GetInsertBlock()->getModule(); |
| 1130 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
| 1131 | StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked); |
| 1132 | Constant *F = M->getOrInsertFunction( |
| 1133 | FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), |
| 1134 | DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); |
| 1135 | |
| 1136 | if (File->getType()->isPointerTy()) |
| 1137 | inferLibFuncAttributes(*M->getFunction(FWriteUnlockedName), *TLI); |
| 1138 | CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); |
| 1139 | |
| 1140 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 1141 | CI->setCallingConv(Fn->getCallingConv()); |
| 1142 | return CI; |
| 1143 | } |
| 1144 | |
| 1145 | Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B, |
| 1146 | const TargetLibraryInfo *TLI) { |
| 1147 | if (!TLI->has(LibFunc_fgetc_unlocked)) |
| 1148 | return nullptr; |
| 1149 | |
| 1150 | Module *M = B.GetInsertBlock()->getModule(); |
| 1151 | Constant *F = |
| 1152 | M->getOrInsertFunction("fgetc_unlocked", B.getInt32Ty(), File->getType()); |
| 1153 | if (File->getType()->isPointerTy()) |
| 1154 | inferLibFuncAttributes(*M->getFunction("fgetc_unlocked"), *TLI); |
| 1155 | CallInst *CI = B.CreateCall(F, File, "fgetc_unlocked"); |
| 1156 | |
| 1157 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 1158 | CI->setCallingConv(Fn->getCallingConv()); |
| 1159 | return CI; |
| 1160 | } |
| 1161 | |
| 1162 | Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File, |
| 1163 | IRBuilder<> &B, const TargetLibraryInfo *TLI) { |
| 1164 | if (!TLI->has(LibFunc_fgets_unlocked)) |
| 1165 | return nullptr; |
| 1166 | |
| 1167 | Module *M = B.GetInsertBlock()->getModule(); |
| 1168 | Constant *F = |
Benjamin Kramer | 8ac15bf | 2018-05-16 21:45:39 +0000 | [diff] [blame] | 1169 | M->getOrInsertFunction("fgets_unlocked", B.getInt8PtrTy(), |
| 1170 | B.getInt8PtrTy(), B.getInt32Ty(), File->getType()); |
David Bolvansky | ca22d42 | 2018-05-16 11:39:52 +0000 | [diff] [blame] | 1171 | inferLibFuncAttributes(*M->getFunction("fgets_unlocked"), *TLI); |
| 1172 | CallInst *CI = |
| 1173 | B.CreateCall(F, {castToCStr(Str, B), Size, File}, "fgets_unlocked"); |
| 1174 | |
| 1175 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 1176 | CI->setCallingConv(Fn->getCallingConv()); |
| 1177 | return CI; |
| 1178 | } |
| 1179 | |
| 1180 | Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, |
| 1181 | IRBuilder<> &B, const DataLayout &DL, |
| 1182 | const TargetLibraryInfo *TLI) { |
| 1183 | if (!TLI->has(LibFunc_fread_unlocked)) |
| 1184 | return nullptr; |
| 1185 | |
| 1186 | Module *M = B.GetInsertBlock()->getModule(); |
| 1187 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
| 1188 | StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked); |
| 1189 | Constant *F = M->getOrInsertFunction( |
| 1190 | FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), |
| 1191 | DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); |
| 1192 | |
| 1193 | if (File->getType()->isPointerTy()) |
| 1194 | inferLibFuncAttributes(*M->getFunction(FReadUnlockedName), *TLI); |
| 1195 | CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); |
| 1196 | |
| 1197 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 1198 | CI->setCallingConv(Fn->getCallingConv()); |
| 1199 | return CI; |
| 1200 | } |