Amaury Sechet | e8ea7d8 | 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 | // |
| 10 | // This file implements the --echo commands in llvm-c-test. |
| 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" |
Amaury Sechet | 5590967 | 2016-02-16 05:11:24 +0000 | [diff] [blame] | 19 | #include "llvm-c/Target.h" |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 22 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | // Provide DenseMapInfo for C API opaque types. |
| 29 | template<typename T> |
| 30 | struct CAPIDenseMap {}; |
| 31 | |
| 32 | // The default DenseMapInfo require to know about pointer alignement. |
| 33 | // Because the C API uses opaques pointer types, their alignement is unknown. |
| 34 | // As a result, we need to roll out our own implementation. |
| 35 | template<typename T> |
| 36 | struct CAPIDenseMap<T*> { |
| 37 | struct CAPIDenseMapInfo { |
| 38 | static inline T* getEmptyKey() { |
| 39 | uintptr_t Val = static_cast<uintptr_t>(-1); |
| 40 | return reinterpret_cast<T*>(Val); |
| 41 | } |
| 42 | static inline T* getTombstoneKey() { |
| 43 | uintptr_t Val = static_cast<uintptr_t>(-2); |
| 44 | return reinterpret_cast<T*>(Val); |
| 45 | } |
| 46 | static unsigned getHashValue(const T *PtrVal) { |
| 47 | return hash_value(PtrVal); |
| 48 | } |
| 49 | static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; } |
| 50 | }; |
| 51 | |
| 52 | typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map; |
| 53 | }; |
| 54 | |
| 55 | typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap; |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 56 | typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 57 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 58 | struct TypeCloner { |
| 59 | LLVMModuleRef M; |
| 60 | LLVMContextRef Ctx; |
| 61 | |
| 62 | TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {} |
| 63 | |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 64 | LLVMTypeRef Clone(LLVMValueRef Src) { |
| 65 | return Clone(LLVMTypeOf(Src)); |
| 66 | } |
| 67 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 68 | LLVMTypeRef Clone(LLVMTypeRef Src) { |
| 69 | LLVMTypeKind Kind = LLVMGetTypeKind(Src); |
| 70 | switch (Kind) { |
| 71 | case LLVMVoidTypeKind: |
| 72 | return LLVMVoidTypeInContext(Ctx); |
| 73 | case LLVMHalfTypeKind: |
| 74 | return LLVMHalfTypeInContext(Ctx); |
| 75 | case LLVMFloatTypeKind: |
| 76 | return LLVMFloatTypeInContext(Ctx); |
| 77 | case LLVMDoubleTypeKind: |
| 78 | return LLVMDoubleTypeInContext(Ctx); |
| 79 | case LLVMX86_FP80TypeKind: |
| 80 | return LLVMX86FP80TypeInContext(Ctx); |
| 81 | case LLVMFP128TypeKind: |
| 82 | return LLVMFP128TypeInContext(Ctx); |
| 83 | case LLVMPPC_FP128TypeKind: |
| 84 | return LLVMPPCFP128TypeInContext(Ctx); |
| 85 | case LLVMLabelTypeKind: |
| 86 | return LLVMLabelTypeInContext(Ctx); |
| 87 | case LLVMIntegerTypeKind: |
| 88 | return LLVMIntTypeInContext(Ctx, LLVMGetIntTypeWidth(Src)); |
| 89 | case LLVMFunctionTypeKind: { |
| 90 | unsigned ParamCount = LLVMCountParamTypes(Src); |
| 91 | LLVMTypeRef* Params = nullptr; |
| 92 | if (ParamCount > 0) { |
| 93 | Params = (LLVMTypeRef*) malloc(ParamCount * sizeof(LLVMTypeRef)); |
| 94 | LLVMGetParamTypes(Src, Params); |
| 95 | for (unsigned i = 0; i < ParamCount; i++) |
| 96 | Params[i] = Clone(Params[i]); |
| 97 | } |
| 98 | |
| 99 | LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)), |
| 100 | Params, ParamCount, |
| 101 | LLVMIsFunctionVarArg(Src)); |
| 102 | if (ParamCount > 0) |
| 103 | free(Params); |
| 104 | return FunTy; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 105 | } |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 106 | case LLVMStructTypeKind: { |
| 107 | LLVMTypeRef S = nullptr; |
| 108 | const char *Name = LLVMGetStructName(Src); |
| 109 | if (Name) { |
| 110 | S = LLVMGetTypeByName(M, Name); |
| 111 | if (S) |
| 112 | return S; |
| 113 | S = LLVMStructCreateNamed(Ctx, Name); |
| 114 | if (LLVMIsOpaqueStruct(Src)) |
| 115 | return S; |
| 116 | } |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 117 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 118 | unsigned EltCount = LLVMCountStructElementTypes(Src); |
| 119 | SmallVector<LLVMTypeRef, 8> Elts; |
| 120 | for (unsigned i = 0; i < EltCount; i++) |
| 121 | Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i))); |
| 122 | if (Name) |
| 123 | LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src)); |
| 124 | else |
| 125 | S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount, |
| 126 | LLVMIsPackedStruct(Src)); |
| 127 | return S; |
| 128 | } |
| 129 | case LLVMArrayTypeKind: |
| 130 | return LLVMArrayType( |
| 131 | Clone(LLVMGetElementType(Src)), |
| 132 | LLVMGetArrayLength(Src) |
| 133 | ); |
| 134 | case LLVMPointerTypeKind: |
| 135 | return LLVMPointerType( |
| 136 | Clone(LLVMGetElementType(Src)), |
| 137 | LLVMGetPointerAddressSpace(Src) |
| 138 | ); |
| 139 | case LLVMVectorTypeKind: |
| 140 | return LLVMVectorType( |
| 141 | Clone(LLVMGetElementType(Src)), |
| 142 | LLVMGetVectorSize(Src) |
| 143 | ); |
| 144 | case LLVMMetadataTypeKind: |
| 145 | break; |
| 146 | case LLVMX86_MMXTypeKind: |
| 147 | return LLVMX86MMXTypeInContext(Ctx); |
| 148 | default: |
| 149 | break; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 150 | } |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 151 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 152 | fprintf(stderr, "%d is not a supported typekind\n", Kind); |
| 153 | exit(-1); |
| 154 | } |
| 155 | }; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 156 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 157 | static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) { |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 158 | unsigned Count = LLVMCountParams(Src); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 159 | if (Count != LLVMCountParams(Dst)) |
| 160 | report_fatal_error("Parameter count mismatch"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 161 | |
| 162 | ValueMap VMap; |
| 163 | if (Count == 0) |
| 164 | return VMap; |
| 165 | |
| 166 | LLVMValueRef SrcFirst = LLVMGetFirstParam(Src); |
| 167 | LLVMValueRef DstFirst = LLVMGetFirstParam(Dst); |
| 168 | LLVMValueRef SrcLast = LLVMGetLastParam(Src); |
| 169 | LLVMValueRef DstLast = LLVMGetLastParam(Dst); |
| 170 | |
| 171 | LLVMValueRef SrcCur = SrcFirst; |
| 172 | LLVMValueRef DstCur = DstFirst; |
| 173 | LLVMValueRef SrcNext = nullptr; |
| 174 | LLVMValueRef DstNext = nullptr; |
| 175 | while (true) { |
| 176 | const char *Name = LLVMGetValueName(SrcCur); |
| 177 | LLVMSetValueName(DstCur, Name); |
| 178 | |
| 179 | VMap[SrcCur] = DstCur; |
| 180 | |
| 181 | Count--; |
| 182 | SrcNext = LLVMGetNextParam(SrcCur); |
| 183 | DstNext = LLVMGetNextParam(DstCur); |
| 184 | if (SrcNext == nullptr && DstNext == nullptr) { |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 185 | if (SrcCur != SrcLast) |
| 186 | report_fatal_error("SrcLast param does not match End"); |
| 187 | if (DstCur != DstLast) |
| 188 | report_fatal_error("DstLast param does not match End"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 192 | if (SrcNext == nullptr) |
| 193 | report_fatal_error("SrcNext was unexpectedly null"); |
| 194 | if (DstNext == nullptr) |
| 195 | report_fatal_error("DstNext was unexpectedly null"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 196 | |
| 197 | LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 198 | if (SrcPrev != SrcCur) |
| 199 | report_fatal_error("SrcNext.Previous param is not Current"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 200 | |
| 201 | LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 202 | if (DstPrev != DstCur) |
| 203 | report_fatal_error("DstNext.Previous param is not Current"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 204 | |
| 205 | SrcCur = SrcNext; |
| 206 | DstCur = DstNext; |
| 207 | } |
| 208 | |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 209 | if (Count != 0) |
| 210 | report_fatal_error("Parameter count does not match iteration"); |
Amaury Sechet | ecda4ea | 2016-02-14 09:14:30 +0000 | [diff] [blame] | 211 | |
| 212 | return VMap; |
| 213 | } |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 214 | |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 215 | LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) { |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 216 | LLVMValueRef Ret; |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 217 | if (!LLVMIsAConstant(Cst)) |
| 218 | report_fatal_error("Expected a constant"); |
| 219 | |
| 220 | // Maybe it is a symbol |
| 221 | if (LLVMIsAGlobalValue(Cst)) { |
| 222 | const char *Name = LLVMGetValueName(Cst); |
| 223 | |
| 224 | // Try function |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 225 | if (LLVMIsAFunction(Cst)) { |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 226 | if (LLVMGetValueKind(Cst) != LLVMFunctionValueKind) |
| 227 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
| 228 | Ret = LLVMGetNamedFunction(M, Name); |
| 229 | if (!Ret) |
| 230 | report_fatal_error("Could not find function"); |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 231 | // Try global variable |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 232 | } else if (LLVMIsAGlobalVariable(Cst)) { |
| 233 | if (LLVMGetValueKind(Cst) != LLVMGlobalVariableValueKind) |
| 234 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
| 235 | Ret = LLVMGetNamedGlobal(M, Name); |
| 236 | if (!Ret) |
| 237 | report_fatal_error("Could not find function"); |
| 238 | } else { |
| 239 | fprintf(stderr, "Could not find @%s\n", Name); |
| 240 | exit(-1); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 241 | } |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 242 | // Try integer literal |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 243 | } else if (LLVMIsAConstantInt(Cst)) { |
| 244 | if (LLVMGetValueKind(Cst) != LLVMConstantIntValueKind) |
| 245 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
| 246 | Ret = LLVMConstInt(TypeCloner(M).Clone(Cst), LLVMConstIntGetZExtValue(Cst), |
| 247 | false); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 248 | // Try zeroinitializer |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 249 | } else if (LLVMIsAConstantAggregateZero(Cst)) { |
| 250 | if (LLVMGetValueKind(Cst) != LLVMConstantAggregateZeroValueKind) |
| 251 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
| 252 | Ret = LLVMConstNull(TypeCloner(M).Clone(Cst)); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 253 | // Try constant array |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 254 | } else if (LLVMIsAConstantArray(Cst)) { |
| 255 | if (LLVMGetValueKind(Cst) != LLVMConstantArrayValueKind) |
| 256 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 257 | LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); |
| 258 | unsigned EltCount = LLVMGetArrayLength(Ty); |
| 259 | SmallVector<LLVMValueRef, 8> Elts; |
| 260 | for (unsigned i = 0; i < EltCount; i++) |
| 261 | Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M)); |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 262 | Ret = LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount); |
Amaury Sechet | b325686 | 2016-03-13 00:58:25 +0000 | [diff] [blame] | 263 | // Try contant data array |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 264 | } else if (LLVMIsAConstantDataArray(Cst)) { |
| 265 | if (LLVMGetValueKind(Cst) != LLVMConstantDataArrayValueKind) |
| 266 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
Amaury Sechet | b325686 | 2016-03-13 00:58:25 +0000 | [diff] [blame] | 267 | LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); |
| 268 | unsigned EltCount = LLVMGetArrayLength(Ty); |
| 269 | SmallVector<LLVMValueRef, 8> Elts; |
| 270 | for (unsigned i = 0; i < EltCount; i++) |
| 271 | Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M)); |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 272 | Ret = LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 273 | // Try constant struct |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 274 | } else if (LLVMIsAConstantStruct(Cst)) { |
| 275 | if (LLVMGetValueKind(Cst) != LLVMConstantStructValueKind) |
| 276 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 277 | LLVMTypeRef Ty = TypeCloner(M).Clone(Cst); |
| 278 | unsigned EltCount = LLVMCountStructElementTypes(Ty); |
| 279 | SmallVector<LLVMValueRef, 8> Elts; |
| 280 | for (unsigned i = 0; i < EltCount; i++) |
| 281 | Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M)); |
| 282 | if (LLVMGetStructName(Ty)) |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 283 | Ret = LLVMConstNamedStruct(Ty, Elts.data(), EltCount); |
| 284 | else |
| 285 | Ret = LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(), |
| 286 | EltCount, LLVMIsPackedStruct(Ty)); |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 287 | // Try undef |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 288 | } else if (LLVMIsUndef(Cst)) { |
| 289 | if (LLVMGetValueKind(Cst) != LLVMUndefValueValueKind) |
| 290 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
| 291 | Ret = LLVMGetUndef(TypeCloner(M).Clone(Cst)); |
Amaury Sechet | 40bbe51 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 292 | // Try float literal |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 293 | } else if (LLVMIsAConstantFP(Cst)) { |
| 294 | if (LLVMGetValueKind(Cst) != LLVMConstantFPValueKind) |
| 295 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
Amaury Sechet | 40bbe51 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 296 | report_fatal_error("ConstantFP is not supported"); |
Amaury Sechet | 6b16c23 | 2016-02-16 07:33:23 +0000 | [diff] [blame] | 297 | // This kind of constant is not supported |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 298 | } else if (!LLVMIsAConstantExpr(Cst)) { |
Amaury Sechet | 6b16c23 | 2016-02-16 07:33:23 +0000 | [diff] [blame] | 299 | report_fatal_error("Expected a constant expression"); |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 300 | // At this point, it must be a constant expression, and thus, an opcode |
| 301 | } else { |
| 302 | LLVMOpcode Op = LLVMGetConstOpcode(Cst); |
| 303 | switch(Op) { |
| 304 | case LLVMBitCast: |
| 305 | return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M), |
| 306 | TypeCloner(M).Clone(Cst)); |
| 307 | default: |
| 308 | fprintf(stderr, "%d is not a supported opcode\n", Op); |
| 309 | exit(-1); |
| 310 | } |
Amaury Sechet | 40bbe51 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 311 | } |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 312 | if (LLVMGetValueKind(Ret) != LLVMGetValueKind(Cst)) { |
| 313 | report_fatal_error( |
| 314 | "The ValueKind of Ret is not equal to the ValueKind of Cst"); |
| 315 | } |
| 316 | return Ret; |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 319 | struct FunCloner { |
| 320 | LLVMValueRef Fun; |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 321 | LLVMModuleRef M; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 322 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 323 | ValueMap VMap; |
| 324 | BasicBlockMap BBMap; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 325 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 326 | FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst), |
| 327 | M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {} |
| 328 | |
| 329 | LLVMTypeRef CloneType(LLVMTypeRef Src) { |
| 330 | return TypeCloner(M).Clone(Src); |
| 331 | } |
| 332 | |
| 333 | LLVMTypeRef CloneType(LLVMValueRef Src) { |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 334 | return TypeCloner(M).Clone(Src); |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 335 | } |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 336 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 337 | // Try to clone everything in the llvm::Value hierarchy. |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 338 | LLVMValueRef CloneValue(LLVMValueRef Src) { |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 339 | // First, the value may be constant. |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 340 | if (LLVMIsAConstant(Src)) |
| 341 | return clone_constant(Src, M); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 342 | |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 343 | // Function argument should always be in the map already. |
Amaury Sechet | 2f43208 | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 344 | auto i = VMap.find(Src); |
| 345 | if (i != VMap.end()) |
| 346 | return i->second; |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 347 | |
Amaury Sechet | 2f43208 | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 348 | if (!LLVMIsAInstruction(Src)) |
| 349 | report_fatal_error("Expected an instruction"); |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 350 | |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 351 | if (LLVMGetValueKind(Src) != LLVMInstructionValueKind) |
| 352 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
| 353 | |
Amaury Sechet | 2f43208 | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 354 | auto Ctx = LLVMGetModuleContext(M); |
| 355 | auto Builder = LLVMCreateBuilderInContext(Ctx); |
| 356 | auto BB = DeclareBB(LLVMGetInstructionParent(Src)); |
| 357 | LLVMPositionBuilderAtEnd(Builder, BB); |
| 358 | auto Dst = CloneInstruction(Src, Builder); |
| 359 | LLVMDisposeBuilder(Builder); |
| 360 | return Dst; |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) { |
| 364 | const char *Name = LLVMGetValueName(Src); |
| 365 | if (!LLVMIsAInstruction(Src)) |
| 366 | report_fatal_error("Expected an instruction"); |
| 367 | |
Peter Zotov | 3e4561c | 2016-04-06 22:21:29 +0000 | [diff] [blame^] | 368 | if (LLVMGetValueKind(Src) != LLVMInstructionValueKind) |
| 369 | report_fatal_error("LLVMGetValueKind returned incorrect type"); |
| 370 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 371 | // Check if this is something we already computed. |
| 372 | { |
| 373 | auto i = VMap.find(Src); |
Amaury Sechet | 2f43208 | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 374 | if (i != VMap.end()) { |
| 375 | // If we have a hit, it means we already generated the instruction |
| 376 | // as a dependancy to somethign else. We need to make sure |
| 377 | // it is ordered properly. |
| 378 | auto I = i->second; |
| 379 | LLVMInstructionRemoveFromParent(I); |
| 380 | LLVMInsertIntoBuilderWithName(Builder, I, Name); |
| 381 | return I; |
| 382 | } |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | // We tried everything, it must be an instruction |
| 386 | // that hasn't been generated already. |
| 387 | LLVMValueRef Dst = nullptr; |
| 388 | |
| 389 | LLVMOpcode Op = LLVMGetInstructionOpcode(Src); |
| 390 | switch(Op) { |
| 391 | case LLVMRet: { |
| 392 | int OpCount = LLVMGetNumOperands(Src); |
| 393 | if (OpCount == 0) |
| 394 | Dst = LLVMBuildRetVoid(Builder); |
| 395 | else |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 396 | Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0))); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | case LLVMBr: { |
Amaury Sechet | e7e6217 | 2016-02-09 23:15:02 +0000 | [diff] [blame] | 400 | if (!LLVMIsConditional(Src)) { |
| 401 | LLVMValueRef SrcOp = LLVMGetOperand(Src, 0); |
| 402 | LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp); |
| 403 | Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB)); |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | LLVMValueRef Cond = LLVMGetCondition(Src); |
| 408 | LLVMValueRef Else = LLVMGetOperand(Src, 1); |
| 409 | LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else)); |
| 410 | LLVMValueRef Then = LLVMGetOperand(Src, 2); |
| 411 | LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then)); |
| 412 | Dst = LLVMBuildCondBr(Builder, Cond, ThenBB, ElseBB); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | case LLVMSwitch: |
| 416 | case LLVMIndirectBr: |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 417 | break; |
Amaury Sechet | e39e853 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 418 | case LLVMInvoke: { |
| 419 | SmallVector<LLVMValueRef, 8> Args; |
| 420 | int ArgCount = LLVMGetNumArgOperands(Src); |
| 421 | for (int i = 0; i < ArgCount; i++) |
| 422 | Args.push_back(CloneValue(LLVMGetOperand(Src, i))); |
| 423 | LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); |
| 424 | LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src)); |
| 425 | LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src)); |
| 426 | Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount, |
| 427 | Then, Unwind, Name); |
| 428 | break; |
| 429 | } |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 430 | case LLVMUnreachable: |
| 431 | Dst = LLVMBuildUnreachable(Builder); |
| 432 | break; |
| 433 | case LLVMAdd: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 434 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 435 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 436 | Dst = LLVMBuildAdd(Builder, LHS, RHS, Name); |
| 437 | break; |
| 438 | } |
| 439 | case LLVMSub: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 440 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 441 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 442 | Dst = LLVMBuildSub(Builder, LHS, RHS, Name); |
| 443 | break; |
| 444 | } |
| 445 | case LLVMMul: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 446 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 447 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 448 | Dst = LLVMBuildMul(Builder, LHS, RHS, Name); |
| 449 | break; |
| 450 | } |
| 451 | case LLVMUDiv: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 452 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 453 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 454 | Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name); |
| 455 | break; |
| 456 | } |
| 457 | case LLVMSDiv: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 458 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 459 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 460 | Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name); |
| 461 | break; |
| 462 | } |
| 463 | case LLVMURem: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 464 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 465 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 466 | Dst = LLVMBuildURem(Builder, LHS, RHS, Name); |
| 467 | break; |
| 468 | } |
| 469 | case LLVMSRem: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 470 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 471 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 472 | Dst = LLVMBuildSRem(Builder, LHS, RHS, Name); |
| 473 | break; |
| 474 | } |
| 475 | case LLVMShl: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 476 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 477 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 478 | Dst = LLVMBuildShl(Builder, LHS, RHS, Name); |
| 479 | break; |
| 480 | } |
| 481 | case LLVMLShr: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 482 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 483 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 484 | Dst = LLVMBuildLShr(Builder, LHS, RHS, Name); |
| 485 | break; |
| 486 | } |
| 487 | case LLVMAShr: { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 488 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 489 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 490 | Dst = LLVMBuildAShr(Builder, LHS, RHS, Name); |
| 491 | break; |
| 492 | } |
| 493 | case LLVMAnd: { |
Amaury Sechet | 1e5d7e2 | 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 | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 496 | Dst = LLVMBuildAnd(Builder, LHS, RHS, Name); |
| 497 | break; |
| 498 | } |
| 499 | case LLVMOr: { |
Amaury Sechet | 1e5d7e2 | 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 | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 502 | Dst = LLVMBuildOr(Builder, LHS, RHS, Name); |
| 503 | break; |
| 504 | } |
| 505 | case LLVMXor: { |
Amaury Sechet | 1e5d7e2 | 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 | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 508 | Dst = LLVMBuildXor(Builder, LHS, RHS, Name); |
| 509 | break; |
| 510 | } |
| 511 | case LLVMAlloca: { |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 512 | LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 513 | Dst = LLVMBuildAlloca(Builder, Ty, Name); |
| 514 | break; |
| 515 | } |
Amaury Sechet | 053ac45 | 2016-02-17 22:51:03 +0000 | [diff] [blame] | 516 | case LLVMLoad: { |
| 517 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); |
| 518 | Dst = LLVMBuildLoad(Builder, Ptr, Name); |
| 519 | LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); |
| 520 | break; |
| 521 | } |
| 522 | case LLVMStore: { |
| 523 | LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0)); |
| 524 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1)); |
| 525 | Dst = LLVMBuildStore(Builder, Val, Ptr); |
| 526 | LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); |
| 527 | break; |
| 528 | } |
| 529 | case LLVMGetElementPtr: { |
| 530 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); |
| 531 | SmallVector<LLVMValueRef, 8> Idx; |
| 532 | int NumIdx = LLVMGetNumIndices(Src); |
| 533 | for (int i = 1; i <= NumIdx; i++) |
| 534 | Idx.push_back(CloneValue(LLVMGetOperand(Src, i))); |
| 535 | if (LLVMIsInBounds(Src)) |
| 536 | Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name); |
| 537 | else |
| 538 | Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name); |
| 539 | break; |
| 540 | } |
Mehdi Amini | 43165d9 | 2016-03-19 21:28:28 +0000 | [diff] [blame] | 541 | case LLVMAtomicCmpXchg: { |
| 542 | LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); |
| 543 | LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1)); |
| 544 | LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2)); |
| 545 | LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src); |
| 546 | LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src); |
| 547 | LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src); |
| 548 | |
| 549 | Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail, |
| 550 | SingleThread); |
| 551 | } break; |
Amaury Sechet | 40bbe51 | 2016-02-17 23:55:59 +0000 | [diff] [blame] | 552 | case LLVMBitCast: { |
| 553 | LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0)); |
| 554 | Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name); |
| 555 | break; |
| 556 | } |
Amaury Sechet | e7e6217 | 2016-02-09 23:15:02 +0000 | [diff] [blame] | 557 | case LLVMICmp: { |
| 558 | LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src); |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 559 | LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0)); |
| 560 | LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1)); |
Amaury Sechet | e7e6217 | 2016-02-09 23:15:02 +0000 | [diff] [blame] | 561 | Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name); |
| 562 | break; |
| 563 | } |
Amaury Sechet | 2f43208 | 2016-02-11 21:37:54 +0000 | [diff] [blame] | 564 | case LLVMPHI: { |
| 565 | // We need to agressively set things here because of loops. |
| 566 | VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name); |
| 567 | |
| 568 | SmallVector<LLVMValueRef, 8> Values; |
| 569 | SmallVector<LLVMBasicBlockRef, 8> Blocks; |
| 570 | |
| 571 | unsigned IncomingCount = LLVMCountIncoming(Src); |
| 572 | for (unsigned i = 0; i < IncomingCount; ++i) { |
| 573 | Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i))); |
| 574 | Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i))); |
| 575 | } |
| 576 | |
| 577 | LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount); |
| 578 | return Dst; |
| 579 | } |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 580 | case LLVMCall: { |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 581 | SmallVector<LLVMValueRef, 8> Args; |
Amaury Sechet | 5c7b3af | 2016-02-10 00:09:37 +0000 | [diff] [blame] | 582 | int ArgCount = LLVMGetNumArgOperands(Src); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 583 | for (int i = 0; i < ArgCount; i++) |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 584 | Args.push_back(CloneValue(LLVMGetOperand(Src, i))); |
Amaury Sechet | 5c7b3af | 2016-02-10 00:09:37 +0000 | [diff] [blame] | 585 | LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 586 | Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name); |
Amaury Sechet | e39e853 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 587 | LLVMSetTailCall(Dst, LLVMIsTailCall(Src)); |
| 588 | break; |
| 589 | } |
| 590 | case LLVMResume: { |
| 591 | Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0))); |
| 592 | break; |
| 593 | } |
| 594 | case LLVMLandingPad: { |
| 595 | // The landing pad API is a bit screwed up for historical reasons. |
| 596 | Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name); |
| 597 | unsigned NumClauses = LLVMGetNumClauses(Src); |
| 598 | for (unsigned i = 0; i < NumClauses; ++i) |
| 599 | LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i))); |
| 600 | LLVMSetCleanup(Dst, LLVMIsCleanup(Src)); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 601 | break; |
| 602 | } |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 603 | case LLVMExtractValue: { |
| 604 | LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); |
| 605 | if (LLVMGetNumIndices(Src) != 1) |
| 606 | report_fatal_error("Expected only one indice"); |
| 607 | auto I = LLVMGetIndices(Src)[0]; |
| 608 | Dst = LLVMBuildExtractValue(Builder, Agg, I, Name); |
| 609 | break; |
| 610 | } |
| 611 | case LLVMInsertValue: { |
| 612 | LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0)); |
| 613 | LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1)); |
| 614 | if (LLVMGetNumIndices(Src) != 1) |
| 615 | report_fatal_error("Expected only one indice"); |
| 616 | auto I = LLVMGetIndices(Src)[0]; |
| 617 | Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name); |
| 618 | break; |
| 619 | } |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 620 | default: |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | if (Dst == nullptr) { |
| 625 | fprintf(stderr, "%d is not a supported opcode\n", Op); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 626 | exit(-1); |
| 627 | } |
| 628 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 629 | return VMap[Src] = Dst; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 632 | LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) { |
| 633 | // Check if this is something we already computed. |
| 634 | { |
| 635 | auto i = BBMap.find(Src); |
| 636 | if (i != BBMap.end()) { |
| 637 | return i->second; |
| 638 | } |
| 639 | } |
| 640 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 641 | LLVMValueRef V = LLVMBasicBlockAsValue(Src); |
| 642 | if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src) |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 643 | report_fatal_error("Basic block is not a basic block"); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 644 | |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 645 | const char *Name = LLVMGetBasicBlockName(Src); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 646 | const char *VName = LLVMGetValueName(V); |
| 647 | if (Name != VName) |
| 648 | report_fatal_error("Basic block name mismatch"); |
| 649 | |
| 650 | LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name); |
| 651 | return BBMap[Src] = BB; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 654 | LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) { |
| 655 | LLVMBasicBlockRef BB = DeclareBB(Src); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 656 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 657 | // Make sure ordering is correct. |
| 658 | LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src); |
| 659 | if (Prev) |
| 660 | LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev)); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 661 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 662 | LLVMValueRef First = LLVMGetFirstInstruction(Src); |
| 663 | LLVMValueRef Last = LLVMGetLastInstruction(Src); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 664 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 665 | if (First == nullptr) { |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 666 | if (Last != nullptr) |
| 667 | report_fatal_error("Has no first instruction, but last one"); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 668 | return BB; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Amaury Sechet | aad9353 | 2016-02-10 00:38:50 +0000 | [diff] [blame] | 671 | auto Ctx = LLVMGetModuleContext(M); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 672 | LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx); |
| 673 | LLVMPositionBuilderAtEnd(Builder, BB); |
| 674 | |
| 675 | LLVMValueRef Cur = First; |
| 676 | LLVMValueRef Next = nullptr; |
| 677 | while(true) { |
Amaury Sechet | 1e5d7e2 | 2016-02-09 23:41:20 +0000 | [diff] [blame] | 678 | CloneInstruction(Cur, Builder); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 679 | Next = LLVMGetNextInstruction(Cur); |
| 680 | if (Next == nullptr) { |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 681 | if (Cur != Last) |
| 682 | report_fatal_error("Final instruction does not match Last"); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 683 | break; |
| 684 | } |
| 685 | |
| 686 | LLVMValueRef Prev = LLVMGetPreviousInstruction(Next); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 687 | if (Prev != Cur) |
| 688 | report_fatal_error("Next.Previous instruction is not Current"); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 689 | |
| 690 | Cur = Next; |
| 691 | } |
| 692 | |
| 693 | LLVMDisposeBuilder(Builder); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 694 | return BB; |
| 695 | } |
| 696 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 697 | void CloneBBs(LLVMValueRef Src) { |
| 698 | unsigned Count = LLVMCountBasicBlocks(Src); |
| 699 | if (Count == 0) |
| 700 | return; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 701 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 702 | LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src); |
| 703 | LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src); |
| 704 | |
| 705 | LLVMBasicBlockRef Cur = First; |
| 706 | LLVMBasicBlockRef Next = nullptr; |
| 707 | while(true) { |
| 708 | CloneBB(Cur); |
| 709 | Count--; |
| 710 | Next = LLVMGetNextBasicBlock(Cur); |
| 711 | if (Next == nullptr) { |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 712 | if (Cur != Last) |
| 713 | report_fatal_error("Final basic block does not match Last"); |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 714 | break; |
| 715 | } |
| 716 | |
| 717 | LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 718 | if (Prev != Cur) |
| 719 | report_fatal_error("Next.Previous basic bloc is not Current"); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 720 | |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 721 | Cur = Next; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 724 | if (Count != 0) |
| 725 | report_fatal_error("Basic block count does not match iterration"); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 726 | } |
Amaury Sechet | a82042e | 2016-02-09 22:36:41 +0000 | [diff] [blame] | 727 | }; |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 728 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 729 | static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) { |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 730 | LLVMValueRef Begin = LLVMGetFirstGlobal(Src); |
| 731 | LLVMValueRef End = LLVMGetLastGlobal(Src); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 732 | |
| 733 | LLVMValueRef Cur = Begin; |
| 734 | LLVMValueRef Next = nullptr; |
Amaury Sechet | 58946a9 | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 735 | if (!Begin) { |
| 736 | if (End != nullptr) |
| 737 | report_fatal_error("Range has an end but no begining"); |
| 738 | goto FunDecl; |
| 739 | } |
| 740 | |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 741 | while (true) { |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 742 | const char *Name = LLVMGetValueName(Cur); |
| 743 | if (LLVMGetNamedGlobal(M, Name)) |
| 744 | report_fatal_error("GlobalVariable already cloned"); |
| 745 | LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name); |
| 746 | |
| 747 | Next = LLVMGetNextGlobal(Cur); |
| 748 | if (Next == nullptr) { |
| 749 | if (Cur != End) |
| 750 | report_fatal_error(""); |
| 751 | break; |
| 752 | } |
| 753 | |
| 754 | LLVMValueRef Prev = LLVMGetPreviousGlobal(Next); |
| 755 | if (Prev != Cur) |
| 756 | report_fatal_error("Next.Previous global is not Current"); |
| 757 | |
| 758 | Cur = Next; |
| 759 | } |
| 760 | |
Amaury Sechet | 58946a9 | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 761 | FunDecl: |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 762 | Begin = LLVMGetFirstFunction(Src); |
| 763 | End = LLVMGetLastFunction(Src); |
| 764 | if (!Begin) { |
| 765 | if (End != nullptr) |
| 766 | report_fatal_error("Range has an end but no begining"); |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | Cur = Begin; |
| 771 | Next = nullptr; |
| 772 | while (true) { |
| 773 | const char *Name = LLVMGetValueName(Cur); |
| 774 | if (LLVMGetNamedFunction(M, Name)) |
| 775 | report_fatal_error("Function already cloned"); |
| 776 | LLVMAddFunction(M, Name, LLVMGetElementType(TypeCloner(M).Clone(Cur))); |
| 777 | |
Amaury Sechet | f64e834 | 2016-02-16 07:08:49 +0000 | [diff] [blame] | 778 | Next = LLVMGetNextFunction(Cur); |
| 779 | if (Next == nullptr) { |
| 780 | if (Cur != End) |
| 781 | report_fatal_error("Last function does not match End"); |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | LLVMValueRef Prev = LLVMGetPreviousFunction(Next); |
| 786 | if (Prev != Cur) |
| 787 | report_fatal_error("Next.Previous function is not Current"); |
| 788 | |
| 789 | Cur = Next; |
| 790 | } |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 791 | } |
Amaury Sechet | f64e834 | 2016-02-16 07:08:49 +0000 | [diff] [blame] | 792 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 793 | static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) { |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 794 | LLVMValueRef Begin = LLVMGetFirstGlobal(Src); |
| 795 | LLVMValueRef End = LLVMGetLastGlobal(Src); |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 796 | |
| 797 | LLVMValueRef Cur = Begin; |
| 798 | LLVMValueRef Next = nullptr; |
Amaury Sechet | 58946a9 | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 799 | if (!Begin) { |
| 800 | if (End != nullptr) |
| 801 | report_fatal_error("Range has an end but no begining"); |
| 802 | goto FunClone; |
| 803 | } |
| 804 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 805 | while (true) { |
| 806 | const char *Name = LLVMGetValueName(Cur); |
| 807 | LLVMValueRef G = LLVMGetNamedGlobal(M, Name); |
| 808 | if (!G) |
| 809 | report_fatal_error("GlobalVariable must have been declared already"); |
| 810 | |
| 811 | if (auto I = LLVMGetInitializer(Cur)) |
| 812 | LLVMSetInitializer(G, clone_constant(I, M)); |
| 813 | |
| 814 | LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur)); |
| 815 | LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur)); |
| 816 | LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur)); |
| 817 | LLVMSetLinkage(G, LLVMGetLinkage(Cur)); |
| 818 | LLVMSetSection(G, LLVMGetSection(Cur)); |
| 819 | LLVMSetVisibility(G, LLVMGetVisibility(Cur)); |
| 820 | LLVMSetUnnamedAddr(G, LLVMHasUnnamedAddr(Cur)); |
| 821 | LLVMSetAlignment(G, LLVMGetAlignment(Cur)); |
| 822 | |
| 823 | Next = LLVMGetNextGlobal(Cur); |
| 824 | if (Next == nullptr) { |
| 825 | if (Cur != End) |
| 826 | report_fatal_error(""); |
| 827 | break; |
| 828 | } |
| 829 | |
| 830 | LLVMValueRef Prev = LLVMGetPreviousGlobal(Next); |
| 831 | if (Prev != Cur) |
| 832 | report_fatal_error("Next.Previous global is not Current"); |
| 833 | |
| 834 | Cur = Next; |
| 835 | } |
| 836 | |
Amaury Sechet | 58946a9 | 2016-02-17 22:30:05 +0000 | [diff] [blame] | 837 | FunClone: |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 838 | Begin = LLVMGetFirstFunction(Src); |
| 839 | End = LLVMGetLastFunction(Src); |
| 840 | if (!Begin) { |
| 841 | if (End != nullptr) |
| 842 | report_fatal_error("Range has an end but no begining"); |
| 843 | return; |
| 844 | } |
| 845 | |
Amaury Sechet | f64e834 | 2016-02-16 07:08:49 +0000 | [diff] [blame] | 846 | Cur = Begin; |
| 847 | Next = nullptr; |
| 848 | while (true) { |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 849 | const char *Name = LLVMGetValueName(Cur); |
| 850 | LLVMValueRef Fun = LLVMGetNamedFunction(M, Name); |
| 851 | if (!Fun) |
| 852 | report_fatal_error("Function must have been declared already"); |
Amaury Sechet | e39e853 | 2016-02-18 20:38:32 +0000 | [diff] [blame] | 853 | |
| 854 | if (LLVMHasPersonalityFn(Cur)) { |
| 855 | const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur)); |
| 856 | LLVMValueRef P = LLVMGetNamedFunction(M, FName); |
| 857 | if (!P) |
| 858 | report_fatal_error("Could not find personality function"); |
| 859 | LLVMSetPersonalityFn(Fun, P); |
| 860 | } |
| 861 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 862 | FunCloner FC(Cur, Fun); |
| 863 | FC.CloneBBs(Cur); |
| 864 | |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 865 | Next = LLVMGetNextFunction(Cur); |
| 866 | if (Next == nullptr) { |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 867 | if (Cur != End) |
| 868 | report_fatal_error("Last function does not match End"); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 869 | break; |
| 870 | } |
| 871 | |
| 872 | LLVMValueRef Prev = LLVMGetPreviousFunction(Next); |
Amaury Sechet | d01c861 | 2016-02-14 10:06:34 +0000 | [diff] [blame] | 873 | if (Prev != Cur) |
| 874 | report_fatal_error("Next.Previous function is not Current"); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 875 | |
| 876 | Cur = Next; |
| 877 | } |
| 878 | } |
| 879 | |
Benjamin Kramer | 9a3bd23 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 880 | int llvm_echo(void) { |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 881 | LLVMEnablePrettyStackTrace(); |
| 882 | |
Benjamin Kramer | 9a3bd23 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 883 | LLVMModuleRef Src = llvm_load_module(false, true); |
Peter Zotov | 0a2fa0a | 2016-04-05 13:56:59 +0000 | [diff] [blame] | 884 | size_t Len; |
| 885 | const char *ModuleName = LLVMGetModuleIdentifier(Src, &Len); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 886 | LLVMContextRef Ctx = LLVMContextCreate(); |
Peter Zotov | 0a2fa0a | 2016-04-05 13:56:59 +0000 | [diff] [blame] | 887 | LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx); |
| 888 | |
| 889 | // This whole switcharound is done because the C API has no way to |
| 890 | // set the source_filename |
| 891 | LLVMSetModuleIdentifier(M, "", 0); |
| 892 | LLVMGetModuleIdentifier(M, &Len); |
| 893 | if (Len != 0) |
| 894 | report_fatal_error("LLVM{Set,Get}ModuleIdentifier failed"); |
| 895 | LLVMSetModuleIdentifier(M, ModuleName, strlen(ModuleName)); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 896 | |
Amaury Sechet | 5590967 | 2016-02-16 05:11:24 +0000 | [diff] [blame] | 897 | LLVMSetTarget(M, LLVMGetTarget(Src)); |
| 898 | LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src)); |
| 899 | if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src))) |
| 900 | report_fatal_error("Inconsistent DataLayout string representation"); |
| 901 | |
Amaury Sechet | e8ba2bf | 2016-02-17 22:13:33 +0000 | [diff] [blame] | 902 | declare_symbols(Src, M); |
| 903 | clone_symbols(Src, M); |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 904 | char *Str = LLVMPrintModuleToString(M); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 905 | fputs(Str, stdout); |
| 906 | |
| 907 | LLVMDisposeMessage(Str); |
Amaury Sechet | c679dbd | 2016-02-14 09:30:42 +0000 | [diff] [blame] | 908 | LLVMDisposeModule(M); |
Amaury Sechet | e8ea7d8 | 2016-02-04 23:26:19 +0000 | [diff] [blame] | 909 | LLVMContextDispose(Ctx); |
| 910 | |
| 911 | return 0; |
| 912 | } |