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