Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1 | //===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===// |
| 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 | // |
Jeroen Ketema | 9640ae7 | 2016-04-08 09:19:02 +0000 | [diff] [blame] | 10 | // This file implements the --echo command in llvm-c-test. |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 11 | // |
| 12 | // This command uses the C API to read a module and output an exact copy of it |
| 13 | // as output. It is used to check that the resulting module matches the input |
| 14 | // to validate that the C API can read and write modules properly. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "llvm-c-test.h" |
Robert Widmann | 56f7ea2 | 2018-09-28 15:35:18 +0000 | [diff] [blame] | 19 | #include "llvm-c/DebugInfo.h" |
Amaury Sechet | b1fb18e | 2016-02-16 05:11:24 +0000 | [diff] [blame] | 20 | #include "llvm-c/Target.h" |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 23 | |
Duncan P. N. Exon Smith | 2707ee3 | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | // Provide DenseMapInfo for C API opaque types. |
| 30 | template<typename T> |
| 31 | struct CAPIDenseMap {}; |
| 32 | |
| 33 | // The default DenseMapInfo require to know about pointer alignement. |
| 34 | // Because the C API uses opaques pointer types, their alignement is unknown. |
| 35 | // As a result, we need to roll out our own implementation. |
| 36 | template<typename T> |
| 37 | struct CAPIDenseMap<T*> { |
| 38 | struct CAPIDenseMapInfo { |
| 39 | static inline T* getEmptyKey() { |
| 40 | uintptr_t Val = static_cast<uintptr_t>(-1); |
| 41 | return reinterpret_cast<T*>(Val); |
| 42 | } |
| 43 | static inline T* getTombstoneKey() { |
| 44 | uintptr_t Val = static_cast<uintptr_t>(-2); |
| 45 | return reinterpret_cast<T*>(Val); |
| 46 | } |
| 47 | static unsigned getHashValue(const T *PtrVal) { |
| 48 | return hash_value(PtrVal); |
| 49 | } |
| 50 | static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; } |
| 51 | }; |
| 52 | |
| 53 | typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map; |
| 54 | }; |
| 55 | |
| 56 | typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap; |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 57 | typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap; |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 58 | |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 59 | struct TypeCloner { |
| 60 | LLVMModuleRef M; |
| 61 | LLVMContextRef Ctx; |
| 62 | |
| 63 | TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {} |
| 64 | |
Amaury Sechet | 1ca39a4 | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 65 | LLVMTypeRef Clone(LLVMValueRef Src) { |
| 66 | return Clone(LLVMTypeOf(Src)); |
| 67 | } |
| 68 | |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 69 | LLVMTypeRef Clone(LLVMTypeRef Src) { |
| 70 | LLVMTypeKind Kind = LLVMGetTypeKind(Src); |
| 71 | switch (Kind) { |
| 72 | case LLVMVoidTypeKind: |
| 73 | return LLVMVoidTypeInContext(Ctx); |
| 74 | case LLVMHalfTypeKind: |
| 75 | return LLVMHalfTypeInContext(Ctx); |
| 76 | case LLVMFloatTypeKind: |
| 77 | return LLVMFloatTypeInContext(Ctx); |
| 78 | case LLVMDoubleTypeKind: |
| 79 | return LLVMDoubleTypeInContext(Ctx); |
| 80 | case LLVMX86_FP80TypeKind: |
| 81 | return LLVMX86FP80TypeInContext(Ctx); |
| 82 | case LLVMFP128TypeKind: |
| 83 | return LLVMFP128TypeInContext(Ctx); |
| 84 | case LLVMPPC_FP128TypeKind: |
| 85 | return LLVMPPCFP128TypeInContext(Ctx); |
| 86 | case LLVMLabelTypeKind: |
| 87 | return LLVMLabelTypeInContext(Ctx); |
| 88 | case LLVMIntegerTypeKind: |
| 89 | return LLVMIntTypeInContext(Ctx, LLVMGetIntTypeWidth(Src)); |
| 90 | case LLVMFunctionTypeKind: { |
| 91 | unsigned ParamCount = LLVMCountParamTypes(Src); |
| 92 | LLVMTypeRef* Params = nullptr; |
| 93 | if (ParamCount > 0) { |
Serge Pavlov | 06c71d8 | 2018-02-20 05:41:26 +0000 | [diff] [blame] | 94 | Params = static_cast<LLVMTypeRef*>( |
| 95 | safe_malloc(ParamCount * sizeof(LLVMTypeRef))); |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 96 | LLVMGetParamTypes(Src, Params); |
| 97 | for (unsigned i = 0; i < ParamCount; i++) |
| 98 | Params[i] = Clone(Params[i]); |
| 99 | } |
| 100 | |
| 101 | LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)), |
| 102 | Params, ParamCount, |
| 103 | LLVMIsFunctionVarArg(Src)); |
| 104 | if (ParamCount > 0) |
| 105 | free(Params); |
| 106 | return FunTy; |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 107 | } |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 108 | case LLVMStructTypeKind: { |
| 109 | LLVMTypeRef S = nullptr; |
| 110 | const char *Name = LLVMGetStructName(Src); |
| 111 | if (Name) { |
| 112 | S = LLVMGetTypeByName(M, Name); |
| 113 | if (S) |
| 114 | return S; |
| 115 | S = LLVMStructCreateNamed(Ctx, Name); |
| 116 | if (LLVMIsOpaqueStruct(Src)) |
| 117 | return S; |
| 118 | } |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 119 | |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 120 | unsigned EltCount = LLVMCountStructElementTypes(Src); |
| 121 | SmallVector<LLVMTypeRef, 8> Elts; |
| 122 | for (unsigned i = 0; i < EltCount; i++) |
| 123 | Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i))); |
| 124 | if (Name) |
| 125 | LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src)); |
| 126 | else |
| 127 | S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount, |
| 128 | LLVMIsPackedStruct(Src)); |
| 129 | return S; |
| 130 | } |
| 131 | case LLVMArrayTypeKind: |
| 132 | return LLVMArrayType( |
| 133 | Clone(LLVMGetElementType(Src)), |
| 134 | LLVMGetArrayLength(Src) |
| 135 | ); |
| 136 | case LLVMPointerTypeKind: |
| 137 | return LLVMPointerType( |
| 138 | Clone(LLVMGetElementType(Src)), |
| 139 | LLVMGetPointerAddressSpace(Src) |
| 140 | ); |
| 141 | case LLVMVectorTypeKind: |
| 142 | return LLVMVectorType( |
| 143 | Clone(LLVMGetElementType(Src)), |
| 144 | LLVMGetVectorSize(Src) |
| 145 | ); |
| 146 | case LLVMMetadataTypeKind: |
whitequark | a129dc8 | 2017-10-27 11:51:40 +0000 | [diff] [blame] | 147 | return LLVMMetadataTypeInContext(Ctx); |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 148 | case LLVMX86_MMXTypeKind: |
| 149 | return LLVMX86MMXTypeInContext(Ctx); |
Robert Widmann | fea7778 | 2018-03-30 17:49:53 +0000 | [diff] [blame] | 150 | case LLVMTokenTypeKind: |
| 151 | return LLVMTokenTypeInContext(Ctx); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 152 | } |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 153 | |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 154 | fprintf(stderr, "%d is not a supported typekind\n", Kind); |
| 155 | exit(-1); |
| 156 | } |
| 157 | }; |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 158 | |
Duncan P. N. Exon Smith | 2707ee3 | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 159 | static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) { |
Amaury Sechet | 6e0e354 | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 160 | unsigned Count = LLVMCountParams(Src); |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 161 | if (Count != LLVMCountParams(Dst)) |
| 162 | report_fatal_error("Parameter count mismatch"); |
Amaury Sechet | 6e0e354 | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 163 | |
| 164 | ValueMap VMap; |
| 165 | if (Count == 0) |
| 166 | return VMap; |
| 167 | |
| 168 | LLVMValueRef SrcFirst = LLVMGetFirstParam(Src); |
| 169 | LLVMValueRef DstFirst = LLVMGetFirstParam(Dst); |
| 170 | LLVMValueRef SrcLast = LLVMGetLastParam(Src); |
| 171 | LLVMValueRef DstLast = LLVMGetLastParam(Dst); |
| 172 | |
| 173 | LLVMValueRef SrcCur = SrcFirst; |
| 174 | LLVMValueRef DstCur = DstFirst; |
| 175 | LLVMValueRef SrcNext = nullptr; |
| 176 | LLVMValueRef DstNext = nullptr; |
| 177 | while (true) { |
Robert Widmann | 12e7467 | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 178 | size_t NameLen; |
| 179 | const char *Name = LLVMGetValueName2(SrcCur, &NameLen); |
| 180 | LLVMSetValueName2(DstCur, Name, NameLen); |
Amaury Sechet | 6e0e354 | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 181 | |
| 182 | VMap[SrcCur] = DstCur; |
| 183 | |
| 184 | Count--; |
| 185 | SrcNext = LLVMGetNextParam(SrcCur); |
| 186 | DstNext = LLVMGetNextParam(DstCur); |
| 187 | if (SrcNext == nullptr && DstNext == nullptr) { |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 188 | if (SrcCur != SrcLast) |
| 189 | report_fatal_error("SrcLast param does not match End"); |
| 190 | if (DstCur != DstLast) |
| 191 | report_fatal_error("DstLast param does not match End"); |
Amaury Sechet | 6e0e354 | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 192 | break; |
| 193 | } |
| 194 | |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 195 | if (SrcNext == nullptr) |
| 196 | report_fatal_error("SrcNext was unexpectedly null"); |
| 197 | if (DstNext == nullptr) |
| 198 | report_fatal_error("DstNext was unexpectedly null"); |
Amaury Sechet | 6e0e354 | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 199 | |
| 200 | LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext); |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 201 | if (SrcPrev != SrcCur) |
| 202 | report_fatal_error("SrcNext.Previous param is not Current"); |
Amaury Sechet | 6e0e354 | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 203 | |
| 204 | LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext); |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 205 | if (DstPrev != DstCur) |
| 206 | report_fatal_error("DstNext.Previous param is not Current"); |
Amaury Sechet | 6e0e354 | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 207 | |
| 208 | SrcCur = SrcNext; |
| 209 | DstCur = DstNext; |
| 210 | } |
| 211 | |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 212 | if (Count != 0) |
| 213 | report_fatal_error("Parameter count does not match iteration"); |
Amaury Sechet | 6e0e354 | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 214 | |
| 215 | return VMap; |
| 216 | } |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 217 | |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 218 | static void check_value_kind(LLVMValueRef V, LLVMValueKind K) { |
| 219 | if (LLVMGetValueKind(V) != K) |
| 220 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
| 221 | } |
| 222 | |
| 223 | static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M); |
| 224 | |
| 225 | static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) { |
| 226 | LLVMValueRef Ret = clone_constant_impl(Cst, M); |
| 227 | check_value_kind(Ret, LLVMGetValueKind(Cst)); |
| 228 | return Ret; |
| 229 | } |
| 230 | |
| 231 | static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) { |
Amaury Sechet | 1ca39a4 | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 232 | if (!LLVMIsAConstant(Cst)) |
| 233 | report_fatal_error("Expected a constant"); |
| 234 | |
| 235 | // Maybe it is a symbol |
| 236 | if (LLVMIsAGlobalValue(Cst)) { |
Robert Widmann | 12e7467 | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 237 | size_t NameLen; |
| 238 | const char *Name = LLVMGetValueName2(Cst, &NameLen); |
Amaury Sechet | 1ca39a4 | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 239 | |
| 240 | // Try function |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 241 | if (LLVMIsAFunction(Cst)) { |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 242 | check_value_kind(Cst, LLVMFunctionValueKind); |
Robert Widmann | 4fefaae | 2018-11-06 01:38:14 +0000 | [diff] [blame] | 243 | |
| 244 | LLVMValueRef Dst = nullptr; |
| 245 | // Try an intrinsic |
| 246 | unsigned ID = LLVMGetIntrinsicID(Cst); |
| 247 | if (ID > 0 && !LLVMIntrinsicIsOverloaded(ID)) { |
| 248 | Dst = LLVMGetIntrinsicDeclaration(M, ID, nullptr, 0); |
| 249 | } else { |
| 250 | // Try a normal function |
| 251 | Dst = LLVMGetNamedFunction(M, Name); |
| 252 | } |
| 253 | |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 254 | if (Dst) |
| 255 | return Dst; |
| 256 | report_fatal_error("Could not find function"); |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 257 | } |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 258 | |
| 259 | // Try global variable |
| 260 | if (LLVMIsAGlobalVariable(Cst)) { |
| 261 | check_value_kind(Cst, LLVMGlobalVariableValueKind); |
Robert Widmann | 1181c40 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 262 | LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name); |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 263 | if (Dst) |
| 264 | return Dst; |
Robert Widmann | 1181c40 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 265 | report_fatal_error("Could not find variable"); |
| 266 | } |
| 267 | |
| 268 | // Try global alias |
| 269 | if (LLVMIsAGlobalAlias(Cst)) { |
| 270 | check_value_kind(Cst, LLVMGlobalAliasValueKind); |
| 271 | LLVMValueRef Dst = LLVMGetNamedGlobalAlias(M, Name, NameLen); |
| 272 | if (Dst) |
| 273 | return Dst; |
| 274 | report_fatal_error("Could not find alias"); |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | fprintf(stderr, "Could not find @%s\n", Name); |
| 278 | exit(-1); |
| 279 | } |
| 280 | |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 281 | // Try integer literal |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 282 | if (LLVMIsAConstantInt(Cst)) { |
| 283 | check_value_kind(Cst, LLVMConstantIntValueKind); |
| 284 | return LLVMConstInt(TypeCloner(M).Clone(Cst), |
| 285 | LLVMConstIntGetZExtValue(Cst), false); |
| 286 | } |
| 287 | |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 288 | // Try zeroinitializer |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 289 | if (LLVMIsAConstantAggregateZero(Cst)) { |
| 290 | check_value_kind(Cst, LLVMConstantAggregateZeroValueKind); |
| 291 | return LLVMConstNull(TypeCloner(M).Clone(Cst)); |
| 292 | } |
| 293 | |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 294 | // Try constant array |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 295 | if (LLVMIsAConstantArray(Cst)) { |
| 296 | check_value_kind(Cst, LLVMConstantArrayValueKind); |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 297 | LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); |
| 298 | unsigned EltCount = LLVMGetArrayLength(Ty); |
| 299 | SmallVector<LLVMValueRef, 8> Elts; |
| 300 | for (unsigned i = 0; i < EltCount; i++) |
| 301 | Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M)); |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 302 | return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount); |
| 303 | } |
| 304 | |
Amaury Sechet | fdae897 | 2016-03-13 00:58:25 +0000 | [diff] [blame] | 305 | // Try contant data array |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 306 | if (LLVMIsAConstantDataArray(Cst)) { |
| 307 | check_value_kind(Cst, LLVMConstantDataArrayValueKind); |
Amaury Sechet | fdae897 | 2016-03-13 00:58:25 +0000 | [diff] [blame] | 308 | LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); |
| 309 | unsigned EltCount = LLVMGetArrayLength(Ty); |
| 310 | SmallVector<LLVMValueRef, 8> Elts; |
| 311 | for (unsigned i = 0; i < EltCount; i++) |
| 312 | Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M)); |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 313 | return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount); |
| 314 | } |
| 315 | |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 316 | // Try constant struct |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 317 | if (LLVMIsAConstantStruct(Cst)) { |
| 318 | check_value_kind(Cst, LLVMConstantStructValueKind); |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 319 | LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); |
| 320 | unsigned EltCount = LLVMCountStructElementTypes(Ty); |
| 321 | SmallVector<LLVMValueRef, 8> Elts; |
| 322 | for (unsigned i = 0; i < EltCount; i++) |
| 323 | Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M)); |
| 324 | if (LLVMGetStructName(Ty)) |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 325 | return LLVMConstNamedStruct(Ty, Elts.data(), EltCount); |
| 326 | return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(), |
| 327 | EltCount, LLVMIsPackedStruct(Ty)); |
| 328 | } |
| 329 | |
Amaury Sechet | 1ca39a4 | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 330 | // Try undef |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 331 | if (LLVMIsUndef(Cst)) { |
| 332 | check_value_kind(Cst, LLVMUndefValueValueKind); |
| 333 | return LLVMGetUndef(TypeCloner(M).Clone(Cst)); |
| 334 | } |
| 335 | |
Robert Widmann | fea7778 | 2018-03-30 17:49:53 +0000 | [diff] [blame] | 336 | // Try null |
| 337 | if (LLVMIsNull(Cst)) { |
| 338 | check_value_kind(Cst, LLVMConstantTokenNoneValueKind); |
| 339 | LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); |
| 340 | return LLVMConstNull(Ty); |
| 341 | } |
| 342 | |
Amaury Sechet | a032454 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 343 | // Try float literal |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 344 | if (LLVMIsAConstantFP(Cst)) { |
| 345 | check_value_kind(Cst, LLVMConstantFPValueKind); |
Amaury Sechet | a032454 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 346 | report_fatal_error("ConstantFP is not supported"); |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Amaury Sechet | f1671f6 | 2016-02-16 07:33:23 +0000 | [diff] [blame] | 349 | // This kind of constant is not supported |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 350 | if (!LLVMIsAConstantExpr(Cst)) |
Amaury Sechet | f1671f6 | 2016-02-16 07:33:23 +0000 | [diff] [blame] | 351 | report_fatal_error("Expected a constant expression"); |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 352 | |
| 353 | // At this point, it must be a constant expression |
| 354 | check_value_kind(Cst, LLVMConstantExprValueKind); |
| 355 | |
| 356 | LLVMOpcode Op = LLVMGetConstOpcode(Cst); |
| 357 | switch(Op) { |
| 358 | case LLVMBitCast: |
| 359 | return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M), |
| 360 | TypeCloner(M).Clone(Cst)); |
| 361 | default: |
| 362 | fprintf(stderr, "%d is not a supported opcode\n", Op); |
| 363 | exit(-1); |
Amaury Sechet | a032454 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 364 | } |
Amaury Sechet | 1ca39a4 | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 367 | struct FunCloner { |
| 368 | LLVMValueRef Fun; |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 369 | LLVMModuleRef M; |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 370 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 371 | ValueMap VMap; |
| 372 | BasicBlockMap BBMap; |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 373 | |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 374 | FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst), |
| 375 | M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {} |
| 376 | |
| 377 | LLVMTypeRef CloneType(LLVMTypeRef Src) { |
| 378 | return TypeCloner(M).Clone(Src); |
| 379 | } |
| 380 | |
| 381 | LLVMTypeRef CloneType(LLVMValueRef Src) { |
Amaury Sechet | 1ca39a4 | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 382 | return TypeCloner(M).Clone(Src); |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 383 | } |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 384 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 385 | // Try to clone everything in the llvm::Value hierarchy. |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 386 | LLVMValueRef CloneValue(LLVMValueRef Src) { |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 387 | // First, the value may be constant. |
Amaury Sechet | 1ca39a4 | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 388 | if (LLVMIsAConstant(Src)) |
| 389 | return clone_constant(Src, M); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 390 | |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 391 | // Function argument should always be in the map already. |
Amaury Sechet | c2e77df | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 392 | auto i = VMap.find(Src); |
| 393 | if (i != VMap.end()) |
| 394 | return i->second; |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 395 | |
Amaury Sechet | c2e77df | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 396 | if (!LLVMIsAInstruction(Src)) |
| 397 | report_fatal_error("Expected an instruction"); |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 398 | |
Amaury Sechet | c2e77df | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 399 | auto Ctx = LLVMGetModuleContext(M); |
| 400 | auto Builder = LLVMCreateBuilderInContext(Ctx); |
| 401 | auto BB = DeclareBB(LLVMGetInstructionParent(Src)); |
| 402 | LLVMPositionBuilderAtEnd(Builder, BB); |
| 403 | auto Dst = CloneInstruction(Src, Builder); |
| 404 | LLVMDisposeBuilder(Builder); |
| 405 | return Dst; |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Amaury Sechet | 4e14c83 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 408 | void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) { |
| 409 | auto Ctx = LLVMGetModuleContext(M); |
| 410 | int ArgCount = LLVMGetNumArgOperands(Src); |
| 411 | for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) { |
| 412 | for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { |
| 413 | if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) { |
| 414 | auto Val = LLVMGetEnumAttributeValue(SrcA); |
| 415 | auto A = LLVMCreateEnumAttribute(Ctx, k, Val); |
| 416 | LLVMAddCallSiteAttribute(Dst, i, A); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 422 | LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) { |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 423 | check_value_kind(Src, LLVMInstructionValueKind); |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 424 | if (!LLVMIsAInstruction(Src)) |
| 425 | report_fatal_error("Expected an instruction"); |
| 426 | |
Robert Widmann | 12e7467 | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 427 | size_t NameLen; |
| 428 | const char *Name = LLVMGetValueName2(Src, &NameLen); |
Peter Zotov | 2a41371 | 2016-04-06 22:21:29 +0000 | [diff] [blame] | 429 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 430 | // Check if this is something we already computed. |
| 431 | { |
| 432 | auto i = VMap.find(Src); |
Amaury Sechet | c2e77df | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 433 | if (i != VMap.end()) { |
| 434 | // If we have a hit, it means we already generated the instruction |
| 435 | // as a dependancy to somethign else. We need to make sure |
| 436 | // it is ordered properly. |
| 437 | auto I = i->second; |
| 438 | LLVMInstructionRemoveFromParent(I); |
| 439 | LLVMInsertIntoBuilderWithName(Builder, I, Name); |
| 440 | return I; |
| 441 | } |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // We tried everything, it must be an instruction |
| 445 | // that hasn't been generated already. |
| 446 | LLVMValueRef Dst = nullptr; |
| 447 | |
| 448 | LLVMOpcode Op = LLVMGetInstructionOpcode(Src); |
| 449 | switch(Op) { |
| 450 | case LLVMRet: { |
| 451 | int OpCount = LLVMGetNumOperands(Src); |
| 452 | if (OpCount == 0) |
| 453 | Dst = LLVMBuildRetVoid(Builder); |
| 454 | else |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 455 | Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0))); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 456 | break; |
| 457 | } |
| 458 | case LLVMBr: { |
Amaury Sechet | ef35fd8 | 2016-02-09 23:15:02 +0000 | [diff] [blame] | 459 | if (!LLVMIsConditional(Src)) { |
| 460 | LLVMValueRef SrcOp = LLVMGetOperand(Src, 0); |
| 461 | LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp); |
| 462 | Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB)); |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | LLVMValueRef Cond = LLVMGetCondition(Src); |
| 467 | LLVMValueRef Else = LLVMGetOperand(Src, 1); |
| 468 | LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else)); |
| 469 | LLVMValueRef Then = LLVMGetOperand(Src, 2); |
| 470 | LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then)); |
Rafael Espindola | 9540e47 | 2018-02-21 19:55:11 +0000 | [diff] [blame] | 471 | Dst = LLVMBuildCondBr(Builder, CloneValue(Cond), ThenBB, ElseBB); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 472 | break; |
| 473 | } |
| 474 | case LLVMSwitch: |
| 475 | case LLVMIndirectBr: |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 476 | break; |
Amaury Sechet | 10ae0ad5 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 477 | case LLVMInvoke: { |
| 478 | SmallVector<LLVMValueRef, 8> Args; |
| 479 | int ArgCount = LLVMGetNumArgOperands(Src); |
| 480 | for (int i = 0; i < ArgCount; i++) |
| 481 | Args.push_back(CloneValue(LLVMGetOperand(Src, i))); |
| 482 | LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); |
| 483 | LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src)); |
| 484 | LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src)); |
| 485 | Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount, |
| 486 | Then, Unwind, Name); |
Amaury Sechet | 4e14c83 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 487 | CloneAttrs(Src, Dst); |
Amaury Sechet | 10ae0ad5 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 488 | break; |
| 489 | } |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 490 | case LLVMUnreachable: |
| 491 | Dst = LLVMBuildUnreachable(Builder); |
| 492 | break; |
| 493 | case LLVMAdd: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 494 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 495 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 496 | Dst = LLVMBuildAdd(Builder, LHS, RHS, Name); |
| 497 | break; |
| 498 | } |
| 499 | case LLVMSub: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 500 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 501 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 502 | Dst = LLVMBuildSub(Builder, LHS, RHS, Name); |
| 503 | break; |
| 504 | } |
| 505 | case LLVMMul: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 506 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 507 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 508 | Dst = LLVMBuildMul(Builder, LHS, RHS, Name); |
| 509 | break; |
| 510 | } |
| 511 | case LLVMUDiv: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 512 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 513 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 514 | Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name); |
| 515 | break; |
| 516 | } |
| 517 | case LLVMSDiv: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 518 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 519 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 520 | Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name); |
| 521 | break; |
| 522 | } |
| 523 | case LLVMURem: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 524 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 525 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 526 | Dst = LLVMBuildURem(Builder, LHS, RHS, Name); |
| 527 | break; |
| 528 | } |
| 529 | case LLVMSRem: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 530 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 531 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 532 | Dst = LLVMBuildSRem(Builder, LHS, RHS, Name); |
| 533 | break; |
| 534 | } |
| 535 | case LLVMShl: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 536 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 537 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 538 | Dst = LLVMBuildShl(Builder, LHS, RHS, Name); |
| 539 | break; |
| 540 | } |
| 541 | case LLVMLShr: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 542 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 543 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 544 | Dst = LLVMBuildLShr(Builder, LHS, RHS, Name); |
| 545 | break; |
| 546 | } |
| 547 | case LLVMAShr: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 548 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 549 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 550 | Dst = LLVMBuildAShr(Builder, LHS, RHS, Name); |
| 551 | break; |
| 552 | } |
| 553 | case LLVMAnd: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 554 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 555 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 556 | Dst = LLVMBuildAnd(Builder, LHS, RHS, Name); |
| 557 | break; |
| 558 | } |
| 559 | case LLVMOr: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 560 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 561 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 562 | Dst = LLVMBuildOr(Builder, LHS, RHS, Name); |
| 563 | break; |
| 564 | } |
| 565 | case LLVMXor: { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 566 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 567 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 568 | Dst = LLVMBuildXor(Builder, LHS, RHS, Name); |
| 569 | break; |
| 570 | } |
| 571 | case LLVMAlloca: { |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 572 | LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 573 | Dst = LLVMBuildAlloca(Builder, Ty, Name); |
| 574 | break; |
| 575 | } |
Amaury Sechet | 185aa0e | 2016-02-17 22:51:03 +0000 | [diff] [blame] | 576 | case LLVMLoad: { |
| 577 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); |
| 578 | Dst = LLVMBuildLoad(Builder, Ptr, Name); |
| 579 | LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); |
| 580 | break; |
| 581 | } |
| 582 | case LLVMStore: { |
| 583 | LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0)); |
| 584 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1)); |
| 585 | Dst = LLVMBuildStore(Builder, Val, Ptr); |
| 586 | LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); |
| 587 | break; |
| 588 | } |
| 589 | case LLVMGetElementPtr: { |
| 590 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); |
| 591 | SmallVector<LLVMValueRef, 8> Idx; |
| 592 | int NumIdx = LLVMGetNumIndices(Src); |
| 593 | for (int i = 1; i <= NumIdx; i++) |
| 594 | Idx.push_back(CloneValue(LLVMGetOperand(Src, i))); |
| 595 | if (LLVMIsInBounds(Src)) |
| 596 | Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name); |
| 597 | else |
| 598 | Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name); |
| 599 | break; |
| 600 | } |
Mehdi Amini | e6cda0e | 2016-03-19 21:28:28 +0000 | [diff] [blame] | 601 | case LLVMAtomicCmpXchg: { |
| 602 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); |
| 603 | LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1)); |
| 604 | LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2)); |
| 605 | LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src); |
| 606 | LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src); |
| 607 | LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src); |
| 608 | |
| 609 | Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail, |
| 610 | SingleThread); |
| 611 | } break; |
Amaury Sechet | a032454 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 612 | case LLVMBitCast: { |
| 613 | LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0)); |
| 614 | Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name); |
| 615 | break; |
| 616 | } |
Amaury Sechet | ef35fd8 | 2016-02-09 23:15:02 +0000 | [diff] [blame] | 617 | case LLVMICmp: { |
| 618 | LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src); |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 619 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 620 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | ef35fd8 | 2016-02-09 23:15:02 +0000 | [diff] [blame] | 621 | Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name); |
| 622 | break; |
| 623 | } |
Amaury Sechet | c2e77df | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 624 | case LLVMPHI: { |
Simon Pilgrim | 428e693 | 2017-03-30 12:59:53 +0000 | [diff] [blame] | 625 | // We need to aggressively set things here because of loops. |
Amaury Sechet | c2e77df | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 626 | VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name); |
| 627 | |
| 628 | SmallVector<LLVMValueRef, 8> Values; |
| 629 | SmallVector<LLVMBasicBlockRef, 8> Blocks; |
| 630 | |
| 631 | unsigned IncomingCount = LLVMCountIncoming(Src); |
| 632 | for (unsigned i = 0; i < IncomingCount; ++i) { |
| 633 | Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i))); |
| 634 | Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i))); |
| 635 | } |
| 636 | |
| 637 | LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount); |
| 638 | return Dst; |
| 639 | } |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 640 | case LLVMCall: { |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 641 | SmallVector<LLVMValueRef, 8> Args; |
Amaury Sechet | 6f6b354 | 2016-02-10 00:09:37 +0000 | [diff] [blame] | 642 | int ArgCount = LLVMGetNumArgOperands(Src); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 643 | for (int i = 0; i < ArgCount; i++) |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 644 | Args.push_back(CloneValue(LLVMGetOperand(Src, i))); |
Amaury Sechet | 6f6b354 | 2016-02-10 00:09:37 +0000 | [diff] [blame] | 645 | LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 646 | Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name); |
Amaury Sechet | 10ae0ad5 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 647 | LLVMSetTailCall(Dst, LLVMIsTailCall(Src)); |
Amaury Sechet | 4e14c83 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 648 | CloneAttrs(Src, Dst); |
Amaury Sechet | 10ae0ad5 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | case LLVMResume: { |
| 652 | Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0))); |
| 653 | break; |
| 654 | } |
| 655 | case LLVMLandingPad: { |
| 656 | // The landing pad API is a bit screwed up for historical reasons. |
| 657 | Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name); |
| 658 | unsigned NumClauses = LLVMGetNumClauses(Src); |
| 659 | for (unsigned i = 0; i < NumClauses; ++i) |
| 660 | LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i))); |
| 661 | LLVMSetCleanup(Dst, LLVMIsCleanup(Src)); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 662 | break; |
| 663 | } |
Robert Widmann | fea7778 | 2018-03-30 17:49:53 +0000 | [diff] [blame] | 664 | case LLVMCleanupRet: { |
| 665 | LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0)); |
| 666 | LLVMBasicBlockRef Unwind = nullptr; |
| 667 | if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) |
| 668 | Unwind = DeclareBB(UDest); |
| 669 | Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind); |
| 670 | break; |
| 671 | } |
| 672 | case LLVMCatchRet: { |
| 673 | LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0)); |
| 674 | LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0)); |
| 675 | Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB); |
| 676 | break; |
| 677 | } |
| 678 | case LLVMCatchPad: { |
| 679 | LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src)); |
| 680 | SmallVector<LLVMValueRef, 8> Args; |
| 681 | int ArgCount = LLVMGetNumArgOperands(Src); |
| 682 | for (int i = 0; i < ArgCount; i++) |
| 683 | Args.push_back(CloneValue(LLVMGetOperand(Src, i))); |
| 684 | Dst = LLVMBuildCatchPad(Builder, ParentPad, |
| 685 | Args.data(), ArgCount, Name); |
| 686 | break; |
| 687 | } |
| 688 | case LLVMCleanupPad: { |
| 689 | LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0)); |
| 690 | SmallVector<LLVMValueRef, 8> Args; |
| 691 | int ArgCount = LLVMGetNumArgOperands(Src); |
| 692 | for (int i = 0; i < ArgCount; i++) |
| 693 | Args.push_back(CloneValue(LLVMGetArgOperand(Src, i))); |
| 694 | Dst = LLVMBuildCleanupPad(Builder, ParentPad, |
| 695 | Args.data(), ArgCount, Name); |
| 696 | break; |
| 697 | } |
| 698 | case LLVMCatchSwitch: { |
| 699 | LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0)); |
| 700 | LLVMBasicBlockRef UnwindBB = nullptr; |
| 701 | if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) { |
| 702 | UnwindBB = DeclareBB(UDest); |
| 703 | } |
| 704 | unsigned NumHandlers = LLVMGetNumHandlers(Src); |
| 705 | Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name); |
| 706 | if (NumHandlers > 0) { |
| 707 | LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>( |
| 708 | safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef))); |
| 709 | LLVMGetHandlers(Src, Handlers); |
| 710 | for (unsigned i = 0; i < NumHandlers; i++) |
| 711 | LLVMAddHandler(Dst, DeclareBB(Handlers[i])); |
| 712 | free(Handlers); |
| 713 | } |
| 714 | break; |
| 715 | } |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 716 | case LLVMExtractValue: { |
| 717 | LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); |
| 718 | if (LLVMGetNumIndices(Src) != 1) |
| 719 | report_fatal_error("Expected only one indice"); |
| 720 | auto I = LLVMGetIndices(Src)[0]; |
| 721 | Dst = LLVMBuildExtractValue(Builder, Agg, I, Name); |
| 722 | break; |
| 723 | } |
| 724 | case LLVMInsertValue: { |
| 725 | LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); |
| 726 | LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1)); |
| 727 | if (LLVMGetNumIndices(Src) != 1) |
| 728 | report_fatal_error("Expected only one indice"); |
| 729 | auto I = LLVMGetIndices(Src)[0]; |
| 730 | Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name); |
| 731 | break; |
| 732 | } |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 733 | default: |
| 734 | break; |
| 735 | } |
| 736 | |
| 737 | if (Dst == nullptr) { |
| 738 | fprintf(stderr, "%d is not a supported opcode\n", Op); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 739 | exit(-1); |
| 740 | } |
| 741 | |
Robert Widmann | 56f7ea2 | 2018-09-28 15:35:18 +0000 | [diff] [blame] | 742 | auto Ctx = LLVMGetModuleContext(M); |
| 743 | size_t NumMetadataEntries; |
| 744 | auto *AllMetadata = |
| 745 | LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src, |
| 746 | &NumMetadataEntries); |
| 747 | for (unsigned i = 0; i < NumMetadataEntries; ++i) { |
| 748 | unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i); |
| 749 | LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i); |
| 750 | LLVMSetMetadata(Dst, Kind, LLVMMetadataAsValue(Ctx, MD)); |
| 751 | } |
| 752 | LLVMDisposeValueMetadataEntries(AllMetadata); |
| 753 | LLVMSetInstDebugLocation(Builder, Dst); |
| 754 | |
Amaury Sechet | e31b144 | 2016-04-07 05:56:20 +0000 | [diff] [blame] | 755 | check_value_kind(Dst, LLVMInstructionValueKind); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 756 | return VMap[Src] = Dst; |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 759 | LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) { |
| 760 | // Check if this is something we already computed. |
| 761 | { |
| 762 | auto i = BBMap.find(Src); |
| 763 | if (i != BBMap.end()) { |
| 764 | return i->second; |
| 765 | } |
| 766 | } |
| 767 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 768 | LLVMValueRef V = LLVMBasicBlockAsValue(Src); |
| 769 | if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src) |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 770 | report_fatal_error("Basic block is not a basic block"); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 771 | |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 772 | const char *Name = LLVMGetBasicBlockName(Src); |
Robert Widmann | 12e7467 | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 773 | size_t NameLen; |
| 774 | const char *VName = LLVMGetValueName2(V, &NameLen); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 775 | if (Name != VName) |
| 776 | report_fatal_error("Basic block name mismatch"); |
| 777 | |
| 778 | LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name); |
| 779 | return BBMap[Src] = BB; |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 782 | LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) { |
| 783 | LLVMBasicBlockRef BB = DeclareBB(Src); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 784 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 785 | // Make sure ordering is correct. |
| 786 | LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src); |
| 787 | if (Prev) |
| 788 | LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev)); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 789 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 790 | LLVMValueRef First = LLVMGetFirstInstruction(Src); |
| 791 | LLVMValueRef Last = LLVMGetLastInstruction(Src); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 792 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 793 | if (First == nullptr) { |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 794 | if (Last != nullptr) |
| 795 | report_fatal_error("Has no first instruction, but last one"); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 796 | return BB; |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Amaury Sechet | b711faf | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 799 | auto Ctx = LLVMGetModuleContext(M); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 800 | LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx); |
| 801 | LLVMPositionBuilderAtEnd(Builder, BB); |
| 802 | |
| 803 | LLVMValueRef Cur = First; |
| 804 | LLVMValueRef Next = nullptr; |
| 805 | while(true) { |
Amaury Sechet | 7dc881f | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 806 | CloneInstruction(Cur, Builder); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 807 | Next = LLVMGetNextInstruction(Cur); |
| 808 | if (Next == nullptr) { |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 809 | if (Cur != Last) |
| 810 | report_fatal_error("Final instruction does not match Last"); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 811 | break; |
| 812 | } |
| 813 | |
| 814 | LLVMValueRef Prev = LLVMGetPreviousInstruction(Next); |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 815 | if (Prev != Cur) |
| 816 | report_fatal_error("Next.Previous instruction is not Current"); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 817 | |
| 818 | Cur = Next; |
| 819 | } |
| 820 | |
| 821 | LLVMDisposeBuilder(Builder); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 822 | return BB; |
| 823 | } |
| 824 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 825 | void CloneBBs(LLVMValueRef Src) { |
| 826 | unsigned Count = LLVMCountBasicBlocks(Src); |
| 827 | if (Count == 0) |
| 828 | return; |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 829 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 830 | LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src); |
| 831 | LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src); |
| 832 | |
| 833 | LLVMBasicBlockRef Cur = First; |
| 834 | LLVMBasicBlockRef Next = nullptr; |
| 835 | while(true) { |
| 836 | CloneBB(Cur); |
| 837 | Count--; |
| 838 | Next = LLVMGetNextBasicBlock(Cur); |
| 839 | if (Next == nullptr) { |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 840 | if (Cur != Last) |
| 841 | report_fatal_error("Final basic block does not match Last"); |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 842 | break; |
| 843 | } |
| 844 | |
| 845 | LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next); |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 846 | if (Prev != Cur) |
| 847 | report_fatal_error("Next.Previous basic bloc is not Current"); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 848 | |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 849 | Cur = Next; |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 852 | if (Count != 0) |
| 853 | report_fatal_error("Basic block count does not match iterration"); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 854 | } |
Amaury Sechet | 0999758 | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 855 | }; |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 856 | |
Duncan P. N. Exon Smith | 2707ee3 | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 857 | static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) { |
Robert Widmann | 1181c40 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 858 | auto Ctx = LLVMGetModuleContext(M); |
| 859 | |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 860 | LLVMValueRef Begin = LLVMGetFirstGlobal(Src); |
| 861 | LLVMValueRef End = LLVMGetLastGlobal(Src); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 862 | |
| 863 | LLVMValueRef Cur = Begin; |
| 864 | LLVMValueRef Next = nullptr; |
Amaury Sechet | cd12c6c | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 865 | if (!Begin) { |
| 866 | if (End != nullptr) |
Hiroshi Inoue | ff281e5 | 2017-07-13 06:48:39 +0000 | [diff] [blame] | 867 | report_fatal_error("Range has an end but no beginning"); |
Amaury Sechet | cd12c6c | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 868 | goto FunDecl; |
| 869 | } |
| 870 | |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 871 | while (true) { |
Robert Widmann | 12e7467 | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 872 | size_t NameLen; |
| 873 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 874 | if (LLVMGetNamedGlobal(M, Name)) |
| 875 | report_fatal_error("GlobalVariable already cloned"); |
| 876 | LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name); |
| 877 | |
| 878 | Next = LLVMGetNextGlobal(Cur); |
| 879 | if (Next == nullptr) { |
| 880 | if (Cur != End) |
| 881 | report_fatal_error(""); |
| 882 | break; |
| 883 | } |
| 884 | |
| 885 | LLVMValueRef Prev = LLVMGetPreviousGlobal(Next); |
| 886 | if (Prev != Cur) |
| 887 | report_fatal_error("Next.Previous global is not Current"); |
| 888 | |
| 889 | Cur = Next; |
| 890 | } |
| 891 | |
Amaury Sechet | cd12c6c | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 892 | FunDecl: |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 893 | Begin = LLVMGetFirstFunction(Src); |
| 894 | End = LLVMGetLastFunction(Src); |
| 895 | if (!Begin) { |
| 896 | if (End != nullptr) |
Hiroshi Inoue | ff281e5 | 2017-07-13 06:48:39 +0000 | [diff] [blame] | 897 | report_fatal_error("Range has an end but no beginning"); |
Robert Widmann | 1181c40 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 898 | goto AliasDecl; |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | Cur = Begin; |
| 902 | Next = nullptr; |
| 903 | while (true) { |
Robert Widmann | 12e7467 | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 904 | size_t NameLen; |
| 905 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 906 | if (LLVMGetNamedFunction(M, Name)) |
| 907 | report_fatal_error("Function already cloned"); |
Amaury Sechet | ca66910 | 2016-06-12 06:17:24 +0000 | [diff] [blame] | 908 | auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur)); |
| 909 | auto F = LLVMAddFunction(M, Name, Ty); |
| 910 | |
| 911 | // Copy attributes |
| 912 | for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F); |
| 913 | i <= c; ++i) { |
| 914 | for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) { |
| 915 | if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) { |
| 916 | auto Val = LLVMGetEnumAttributeValue(SrcA); |
| 917 | auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val); |
| 918 | LLVMAddAttributeAtIndex(F, i, DstA); |
| 919 | } |
| 920 | } |
| 921 | } |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 922 | |
Amaury Sechet | f5261f5 | 2016-02-16 07:08:49 +0000 | [diff] [blame] | 923 | Next = LLVMGetNextFunction(Cur); |
| 924 | if (Next == nullptr) { |
| 925 | if (Cur != End) |
| 926 | report_fatal_error("Last function does not match End"); |
| 927 | break; |
| 928 | } |
| 929 | |
| 930 | LLVMValueRef Prev = LLVMGetPreviousFunction(Next); |
| 931 | if (Prev != Cur) |
| 932 | report_fatal_error("Next.Previous function is not Current"); |
| 933 | |
| 934 | Cur = Next; |
| 935 | } |
Robert Widmann | 1181c40 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 936 | |
| 937 | AliasDecl: |
| 938 | Begin = LLVMGetFirstGlobalAlias(Src); |
| 939 | End = LLVMGetLastGlobalAlias(Src); |
| 940 | if (!Begin) { |
| 941 | if (End != nullptr) |
| 942 | report_fatal_error("Range has an end but no beginning"); |
Robert Widmann | 8f32507 | 2018-08-30 17:09:43 +0000 | [diff] [blame] | 943 | goto NamedMDDecl; |
Robert Widmann | 1181c40 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | Cur = Begin; |
| 947 | Next = nullptr; |
| 948 | while (true) { |
| 949 | size_t NameLen; |
| 950 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
| 951 | if (LLVMGetNamedGlobalAlias(M, Name, NameLen)) |
| 952 | report_fatal_error("Global alias already cloned"); |
| 953 | LLVMTypeRef CurType = TypeCloner(M).Clone(Cur); |
| 954 | // FIXME: Allow NULL aliasee. |
| 955 | LLVMAddAlias(M, CurType, LLVMGetUndef(CurType), Name); |
| 956 | |
| 957 | Next = LLVMGetNextGlobalAlias(Cur); |
| 958 | if (Next == nullptr) { |
| 959 | if (Cur != End) |
| 960 | report_fatal_error(""); |
| 961 | break; |
| 962 | } |
| 963 | |
| 964 | LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next); |
| 965 | if (Prev != Cur) |
| 966 | report_fatal_error("Next.Previous global is not Current"); |
| 967 | |
| 968 | Cur = Next; |
| 969 | } |
Robert Widmann | 8f32507 | 2018-08-30 17:09:43 +0000 | [diff] [blame] | 970 | |
| 971 | NamedMDDecl: |
| 972 | LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src); |
| 973 | LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src); |
| 974 | if (!BeginMD) { |
| 975 | if (EndMD != nullptr) |
| 976 | report_fatal_error("Range has an end but no beginning"); |
| 977 | return; |
| 978 | } |
| 979 | |
| 980 | LLVMNamedMDNodeRef CurMD = BeginMD; |
| 981 | LLVMNamedMDNodeRef NextMD = nullptr; |
| 982 | while (true) { |
| 983 | size_t NameLen; |
| 984 | const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen); |
| 985 | if (LLVMGetNamedMetadata(M, Name, NameLen)) |
| 986 | report_fatal_error("Named Metadata Node already cloned"); |
| 987 | LLVMGetOrInsertNamedMetadata(M, Name, NameLen); |
| 988 | |
| 989 | NextMD = LLVMGetNextNamedMetadata(CurMD); |
| 990 | if (NextMD == nullptr) { |
| 991 | if (CurMD != EndMD) |
| 992 | report_fatal_error(""); |
| 993 | break; |
| 994 | } |
| 995 | |
| 996 | LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD); |
| 997 | if (PrevMD != CurMD) |
| 998 | report_fatal_error("Next.Previous global is not Current"); |
| 999 | |
| 1000 | CurMD = NextMD; |
| 1001 | } |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1002 | } |
Amaury Sechet | f5261f5 | 2016-02-16 07:08:49 +0000 | [diff] [blame] | 1003 | |
Duncan P. N. Exon Smith | 2707ee3 | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1004 | static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) { |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1005 | LLVMValueRef Begin = LLVMGetFirstGlobal(Src); |
| 1006 | LLVMValueRef End = LLVMGetLastGlobal(Src); |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1007 | |
| 1008 | LLVMValueRef Cur = Begin; |
| 1009 | LLVMValueRef Next = nullptr; |
Amaury Sechet | cd12c6c | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 1010 | if (!Begin) { |
| 1011 | if (End != nullptr) |
Hiroshi Inoue | ff281e5 | 2017-07-13 06:48:39 +0000 | [diff] [blame] | 1012 | report_fatal_error("Range has an end but no beginning"); |
Amaury Sechet | cd12c6c | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 1013 | goto FunClone; |
| 1014 | } |
| 1015 | |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1016 | while (true) { |
Robert Widmann | 12e7467 | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 1017 | size_t NameLen; |
| 1018 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1019 | LLVMValueRef G = LLVMGetNamedGlobal(M, Name); |
| 1020 | if (!G) |
| 1021 | report_fatal_error("GlobalVariable must have been declared already"); |
| 1022 | |
| 1023 | if (auto I = LLVMGetInitializer(Cur)) |
| 1024 | LLVMSetInitializer(G, clone_constant(I, M)); |
| 1025 | |
Robert Widmann | 56f7ea2 | 2018-09-28 15:35:18 +0000 | [diff] [blame] | 1026 | size_t NumMetadataEntries; |
| 1027 | auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries); |
| 1028 | for (unsigned i = 0; i < NumMetadataEntries; ++i) { |
| 1029 | unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i); |
| 1030 | LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i); |
| 1031 | LLVMGlobalSetMetadata(G, Kind, MD); |
| 1032 | } |
| 1033 | LLVMDisposeValueMetadataEntries(AllMetadata); |
| 1034 | |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1035 | LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur)); |
| 1036 | LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur)); |
| 1037 | LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur)); |
| 1038 | LLVMSetLinkage(G, LLVMGetLinkage(Cur)); |
| 1039 | LLVMSetSection(G, LLVMGetSection(Cur)); |
| 1040 | LLVMSetVisibility(G, LLVMGetVisibility(Cur)); |
Robert Widmann | df7c2a2 | 2018-03-14 06:45:51 +0000 | [diff] [blame] | 1041 | LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur)); |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1042 | LLVMSetAlignment(G, LLVMGetAlignment(Cur)); |
| 1043 | |
| 1044 | Next = LLVMGetNextGlobal(Cur); |
| 1045 | if (Next == nullptr) { |
| 1046 | if (Cur != End) |
| 1047 | report_fatal_error(""); |
| 1048 | break; |
| 1049 | } |
| 1050 | |
| 1051 | LLVMValueRef Prev = LLVMGetPreviousGlobal(Next); |
| 1052 | if (Prev != Cur) |
| 1053 | report_fatal_error("Next.Previous global is not Current"); |
| 1054 | |
| 1055 | Cur = Next; |
| 1056 | } |
| 1057 | |
Amaury Sechet | cd12c6c | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 1058 | FunClone: |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1059 | Begin = LLVMGetFirstFunction(Src); |
| 1060 | End = LLVMGetLastFunction(Src); |
| 1061 | if (!Begin) { |
| 1062 | if (End != nullptr) |
Hiroshi Inoue | ff281e5 | 2017-07-13 06:48:39 +0000 | [diff] [blame] | 1063 | report_fatal_error("Range has an end but no beginning"); |
Robert Widmann | 1181c40 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 1064 | goto AliasClone; |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Amaury Sechet | f5261f5 | 2016-02-16 07:08:49 +0000 | [diff] [blame] | 1067 | Cur = Begin; |
| 1068 | Next = nullptr; |
| 1069 | while (true) { |
Robert Widmann | 12e7467 | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 1070 | size_t NameLen; |
| 1071 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1072 | LLVMValueRef Fun = LLVMGetNamedFunction(M, Name); |
| 1073 | if (!Fun) |
| 1074 | report_fatal_error("Function must have been declared already"); |
Amaury Sechet | 10ae0ad5 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 1075 | |
| 1076 | if (LLVMHasPersonalityFn(Cur)) { |
Robert Widmann | 12e7467 | 2018-05-19 15:08:36 +0000 | [diff] [blame] | 1077 | size_t FNameLen; |
| 1078 | const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur), |
| 1079 | &FNameLen); |
Amaury Sechet | 10ae0ad5 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 1080 | LLVMValueRef P = LLVMGetNamedFunction(M, FName); |
| 1081 | if (!P) |
| 1082 | report_fatal_error("Could not find personality function"); |
| 1083 | LLVMSetPersonalityFn(Fun, P); |
| 1084 | } |
| 1085 | |
Robert Widmann | 56f7ea2 | 2018-09-28 15:35:18 +0000 | [diff] [blame] | 1086 | size_t NumMetadataEntries; |
| 1087 | auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries); |
| 1088 | for (unsigned i = 0; i < NumMetadataEntries; ++i) { |
| 1089 | unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i); |
| 1090 | LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i); |
| 1091 | LLVMGlobalSetMetadata(Fun, Kind, MD); |
| 1092 | } |
| 1093 | LLVMDisposeValueMetadataEntries(AllMetadata); |
| 1094 | |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1095 | FunCloner FC(Cur, Fun); |
| 1096 | FC.CloneBBs(Cur); |
| 1097 | |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1098 | Next = LLVMGetNextFunction(Cur); |
| 1099 | if (Next == nullptr) { |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 1100 | if (Cur != End) |
| 1101 | report_fatal_error("Last function does not match End"); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1102 | break; |
| 1103 | } |
| 1104 | |
| 1105 | LLVMValueRef Prev = LLVMGetPreviousFunction(Next); |
Amaury Sechet | 6a26a01 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 1106 | if (Prev != Cur) |
| 1107 | report_fatal_error("Next.Previous function is not Current"); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1108 | |
| 1109 | Cur = Next; |
| 1110 | } |
Robert Widmann | 1181c40 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 1111 | |
| 1112 | AliasClone: |
| 1113 | Begin = LLVMGetFirstGlobalAlias(Src); |
| 1114 | End = LLVMGetLastGlobalAlias(Src); |
| 1115 | if (!Begin) { |
| 1116 | if (End != nullptr) |
| 1117 | report_fatal_error("Range has an end but no beginning"); |
Robert Widmann | 8f32507 | 2018-08-30 17:09:43 +0000 | [diff] [blame] | 1118 | goto NamedMDClone; |
Robert Widmann | 1181c40 | 2018-05-20 23:49:08 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | Cur = Begin; |
| 1122 | Next = nullptr; |
| 1123 | while (true) { |
| 1124 | size_t NameLen; |
| 1125 | const char *Name = LLVMGetValueName2(Cur, &NameLen); |
| 1126 | LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen); |
| 1127 | if (!Alias) |
| 1128 | report_fatal_error("Global alias must have been declared already"); |
| 1129 | |
| 1130 | if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Cur)) { |
| 1131 | LLVMAliasSetAliasee(Alias, clone_constant(Aliasee, M)); |
| 1132 | } |
| 1133 | |
| 1134 | LLVMSetLinkage(Alias, LLVMGetLinkage(Cur)); |
| 1135 | LLVMSetUnnamedAddress(Alias, LLVMGetUnnamedAddress(Cur)); |
| 1136 | |
| 1137 | Next = LLVMGetNextGlobalAlias(Cur); |
| 1138 | if (Next == nullptr) { |
| 1139 | if (Cur != End) |
| 1140 | report_fatal_error("Last global alias does not match End"); |
| 1141 | break; |
| 1142 | } |
| 1143 | |
| 1144 | LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next); |
| 1145 | if (Prev != Cur) |
| 1146 | report_fatal_error("Next.Previous global alias is not Current"); |
| 1147 | |
| 1148 | Cur = Next; |
| 1149 | } |
Robert Widmann | 8f32507 | 2018-08-30 17:09:43 +0000 | [diff] [blame] | 1150 | |
| 1151 | NamedMDClone: |
| 1152 | LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src); |
| 1153 | LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src); |
| 1154 | if (!BeginMD) { |
| 1155 | if (EndMD != nullptr) |
| 1156 | report_fatal_error("Range has an end but no beginning"); |
| 1157 | return; |
| 1158 | } |
| 1159 | |
| 1160 | LLVMNamedMDNodeRef CurMD = BeginMD; |
| 1161 | LLVMNamedMDNodeRef NextMD = nullptr; |
| 1162 | while (true) { |
| 1163 | size_t NameLen; |
| 1164 | const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen); |
| 1165 | LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen); |
| 1166 | if (!NamedMD) |
| 1167 | report_fatal_error("Named MD Node must have been declared already"); |
| 1168 | |
| 1169 | unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name); |
| 1170 | LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>( |
| 1171 | safe_malloc(OperandCount * sizeof(LLVMValueRef))); |
| 1172 | LLVMGetNamedMetadataOperands(Src, Name, OperandBuf); |
| 1173 | for (unsigned i = 0, e = OperandCount; i != e; ++i) { |
| 1174 | LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]); |
| 1175 | } |
| 1176 | free(OperandBuf); |
| 1177 | |
| 1178 | NextMD = LLVMGetNextNamedMetadata(CurMD); |
| 1179 | if (NextMD == nullptr) { |
| 1180 | if (CurMD != EndMD) |
| 1181 | report_fatal_error("Last Named MD Node does not match End"); |
| 1182 | break; |
| 1183 | } |
| 1184 | |
| 1185 | LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD); |
| 1186 | if (PrevMD != CurMD) |
| 1187 | report_fatal_error("Next.Previous Named MD Node is not Current"); |
| 1188 | |
| 1189 | CurMD = NextMD; |
| 1190 | } |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Benjamin Kramer | 2e13053 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 1193 | int llvm_echo(void) { |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1194 | LLVMEnablePrettyStackTrace(); |
| 1195 | |
Benjamin Kramer | 2e13053 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 1196 | LLVMModuleRef Src = llvm_load_module(false, true); |
Robert Widmann | a92b6bb | 2018-01-30 21:34:29 +0000 | [diff] [blame] | 1197 | size_t SourceFileLen; |
| 1198 | const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen); |
| 1199 | size_t ModuleIdentLen; |
| 1200 | const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1201 | LLVMContextRef Ctx = LLVMContextCreate(); |
Peter Zotov | 2b8aed7 | 2016-04-05 13:56:59 +0000 | [diff] [blame] | 1202 | LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx); |
| 1203 | |
Robert Widmann | a92b6bb | 2018-01-30 21:34:29 +0000 | [diff] [blame] | 1204 | LLVMSetSourceFileName(M, SourceFileName, SourceFileLen); |
| 1205 | LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1206 | |
Amaury Sechet | b1fb18e | 2016-02-16 05:11:24 +0000 | [diff] [blame] | 1207 | LLVMSetTarget(M, LLVMGetTarget(Src)); |
| 1208 | LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src)); |
| 1209 | if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src))) |
| 1210 | report_fatal_error("Inconsistent DataLayout string representation"); |
| 1211 | |
Robert Widmann | e563ac3 | 2018-04-06 02:31:29 +0000 | [diff] [blame] | 1212 | size_t ModuleInlineAsmLen; |
| 1213 | const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen); |
| 1214 | LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen); |
| 1215 | |
Amaury Sechet | eef9379 | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 1216 | declare_symbols(Src, M); |
| 1217 | clone_symbols(Src, M); |
Amaury Sechet | 1ca39a4 | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 1218 | char *Str = LLVMPrintModuleToString(M); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1219 | fputs(Str, stdout); |
| 1220 | |
| 1221 | LLVMDisposeMessage(Str); |
Rafael Espindola | 9540e47 | 2018-02-21 19:55:11 +0000 | [diff] [blame] | 1222 | LLVMDisposeModule(Src); |
Amaury Sechet | 1ca39a4 | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 1223 | LLVMDisposeModule(M); |
Amaury Sechet | 8824280 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 1224 | LLVMContextDispose(Ctx); |
| 1225 | |
| 1226 | return 0; |
| 1227 | } |