llvm.org GIT mirror llvm / 2a41371
[llvm-c] Add LLVMGetValueKind. Patch by Nicole Mazzuca <npmazzuca@gmail.com>. Differential Revision: http://reviews.llvm.org/D18729 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265608 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Zotov 4 years ago
3 changed file(s) with 120 addition(s) and 55 deletion(s). Raw diff Collapse all Expand all
245245 LLVMX86StdcallCallConv = 64,
246246 LLVMX86FastcallCallConv = 65
247247 } LLVMCallConv;
248
249 typedef enum {
250 LLVMArgumentValueKind,
251 LLVMBasicBlockValueKind,
252 LLVMMemoryUseValueKind,
253 LLVMMemoryDefValueKind,
254 LLVMMemoryPhiValueKind,
255
256 LLVMFunctionValueKind,
257 LLVMGlobalAliasValueKind,
258 LLVMGlobalVariableValueKind,
259 LLVMBlockAddressValueKind,
260 LLVMConstantExprValueKind,
261 LLVMConstantArrayValueKind,
262 LLVMConstantStructValueKind,
263 LLVMConstantVectorValueKind,
264
265 LLVMUndefValueValueKind,
266 LLVMConstantAggregateZeroValueKind,
267 LLVMConstantDataArrayValueKind,
268 LLVMConstantDataVectorValueKind,
269 LLVMConstantIntValueKind,
270 LLVMConstantFPValueKind,
271 LLVMConstantPointerNullValueKind,
272 LLVMConstantTokenNoneValueKind,
273
274 LLVMMetadataAsValueValueKind,
275 LLVMInlineAsmValueKind,
276
277 LLVMInstructionValueKind,
278 } LLVMValueKind;
248279
249280 typedef enum {
250281 LLVMIntEQ = 32, /**< equal */
11881219 * @see llvm::Value::getType()
11891220 */
11901221 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val);
1222
1223 /**
1224 * Obtain the enumerated type of a Value instance.
1225 *
1226 * @see llvm::Value::getValueID()
1227 */
1228 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val);
11911229
11921230 /**
11931231 * Obtain the string name of a value.
547547
548548 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
549549 return wrap(unwrap(Val)->getType());
550 }
551
552 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
553 switch(unwrap(Val)->getValueID()) {
554 #define HANDLE_VALUE(Name) \
555 case Value::Name##Val: \
556 return LLVM##Name##ValueKind;
557 #include "llvm/IR/Value.def"
558 default:
559 return LLVMInstructionValueKind;
560 }
550561 }
551562
552563 const char *LLVMGetValueName(LLVMValueRef Val) {
212212 }
213213
214214 LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
215 LLVMValueRef Ret;
215216 if (!LLVMIsAConstant(Cst))
216217 report_fatal_error("Expected a constant");
217218
221222
222223 // Try function
223224 if (LLVMIsAFunction(Cst)) {
224 LLVMValueRef Dst = LLVMGetNamedFunction(M, Name);
225 if (Dst)
226 return Dst;
227 report_fatal_error("Could not find function");
228 }
229
225 if (LLVMGetValueKind(Cst) != LLVMFunctionValueKind)
226 report_fatal_error("LLVMGetValueKind returned incorrect type");
227 Ret = LLVMGetNamedFunction(M, Name);
228 if (!Ret)
229 report_fatal_error("Could not find function");
230230 // Try global variable
231 if (LLVMIsAGlobalVariable(Cst)) {
232 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
233 if (Dst)
234 return Dst;
235 report_fatal_error("Could not find function");
236 }
237
238 fprintf(stderr, "Could not find @%s\n", Name);
239 exit(-1);
240 }
241
231 } else if (LLVMIsAGlobalVariable(Cst)) {
232 if (LLVMGetValueKind(Cst) != LLVMGlobalVariableValueKind)
233 report_fatal_error("LLVMGetValueKind returned incorrect type");
234 Ret = LLVMGetNamedGlobal(M, Name);
235 if (!Ret)
236 report_fatal_error("Could not find function");
237 } else {
238 fprintf(stderr, "Could not find @%s\n", Name);
239 exit(-1);
240 }
242241 // Try integer literal
243 if (LLVMIsAConstantInt(Cst))
244 return LLVMConstInt(TypeCloner(M).Clone(Cst),
245 LLVMConstIntGetZExtValue(Cst), false);
246
242 } else if (LLVMIsAConstantInt(Cst)) {
243 if (LLVMGetValueKind(Cst) != LLVMConstantIntValueKind)
244 report_fatal_error("LLVMGetValueKind returned incorrect type");
245 Ret = LLVMConstInt(TypeCloner(M).Clone(Cst), LLVMConstIntGetZExtValue(Cst),
246 false);
247247 // Try zeroinitializer
248 if (LLVMIsAConstantAggregateZero(Cst))
249 return LLVMConstNull(TypeCloner(M).Clone(Cst));
250
248 } else if (LLVMIsAConstantAggregateZero(Cst)) {
249 if (LLVMGetValueKind(Cst) != LLVMConstantAggregateZeroValueKind)
250 report_fatal_error("LLVMGetValueKind returned incorrect type");
251 Ret = LLVMConstNull(TypeCloner(M).Clone(Cst));
251252 // Try constant array
252 if (LLVMIsAConstantArray(Cst)) {
253 } else if (LLVMIsAConstantArray(Cst)) {
254 if (LLVMGetValueKind(Cst) != LLVMConstantArrayValueKind)
255 report_fatal_error("LLVMGetValueKind returned incorrect type");
253256 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
254257 unsigned EltCount = LLVMGetArrayLength(Ty);
255258 SmallVector Elts;
256259 for (unsigned i = 0; i < EltCount; i++)
257260 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
258 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
259 }
260
261 Ret = LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
261262 // Try contant data array
262 if (LLVMIsAConstantDataArray(Cst)) {
263 } else if (LLVMIsAConstantDataArray(Cst)) {
264 if (LLVMGetValueKind(Cst) != LLVMConstantDataArrayValueKind)
265 report_fatal_error("LLVMGetValueKind returned incorrect type");
263266 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
264267 unsigned EltCount = LLVMGetArrayLength(Ty);
265268 SmallVector Elts;
266269 for (unsigned i = 0; i < EltCount; i++)
267270 Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
268 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
269 }
270
271 Ret = LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
271272 // Try constant struct
272 if (LLVMIsAConstantStruct(Cst)) {
273 } else if (LLVMIsAConstantStruct(Cst)) {
274 if (LLVMGetValueKind(Cst) != LLVMConstantStructValueKind)
275 report_fatal_error("LLVMGetValueKind returned incorrect type");
273276 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
274277 unsigned EltCount = LLVMCountStructElementTypes(Ty);
275278 SmallVector Elts;
276279 for (unsigned i = 0; i < EltCount; i++)
277280 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
278281 if (LLVMGetStructName(Ty))
279 return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
280 return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
281 EltCount, LLVMIsPackedStruct(Ty));
282 }
283
282 Ret = LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
283 else
284 Ret = LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
285 EltCount, LLVMIsPackedStruct(Ty));
284286 // Try undef
285 if (LLVMIsUndef(Cst))
286 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
287
287 } else if (LLVMIsUndef(Cst)) {
288 if (LLVMGetValueKind(Cst) != LLVMUndefValueValueKind)
289 report_fatal_error("LLVMGetValueKind returned incorrect type");
290 Ret = LLVMGetUndef(TypeCloner(M).Clone(Cst));
288291 // Try float literal
289 if (LLVMIsAConstantFP(Cst))
292 } else if (LLVMIsAConstantFP(Cst)) {
293 if (LLVMGetValueKind(Cst) != LLVMConstantFPValueKind)
294 report_fatal_error("LLVMGetValueKind returned incorrect type");
290295 report_fatal_error("ConstantFP is not supported");
291
292296 // This kind of constant is not supported
293 if (!LLVMIsAConstantExpr(Cst))
297 } else if (!LLVMIsAConstantExpr(Cst)) {
294298 report_fatal_error("Expected a constant expression");
295
296 // At this point, it must be a constant expression
297 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
298 switch(Op) {
299 case LLVMBitCast:
300 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
301 TypeCloner(M).Clone(Cst));
302 default:
303 fprintf(stderr, "%d is not a supported opcode\n", Op);
304 exit(-1);
305 }
299 // At this point, it must be a constant expression, and thus, an opcode
300 } else {
301 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
302 switch(Op) {
303 case LLVMBitCast:
304 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
305 TypeCloner(M).Clone(Cst));
306 default:
307 fprintf(stderr, "%d is not a supported opcode\n", Op);
308 exit(-1);
309 }
310 }
311 if (LLVMGetValueKind(Ret) != LLVMGetValueKind(Cst)) {
312 report_fatal_error(
313 "The ValueKind of Ret is not equal to the ValueKind of Cst");
314 }
315 return Ret;
306316 }
307317
308318 struct FunCloner {
336346
337347 if (!LLVMIsAInstruction(Src))
338348 report_fatal_error("Expected an instruction");
349
350 if (LLVMGetValueKind(Src) != LLVMInstructionValueKind)
351 report_fatal_error("LLVMGetValueKind returned incorrect type");
339352
340353 auto Ctx = LLVMGetModuleContext(M);
341354 auto Builder = LLVMCreateBuilderInContext(Ctx);
350363 const char *Name = LLVMGetValueName(Src);
351364 if (!LLVMIsAInstruction(Src))
352365 report_fatal_error("Expected an instruction");
366
367 if (LLVMGetValueKind(Src) != LLVMInstructionValueKind)
368 report_fatal_error("LLVMGetValueKind returned incorrect type");
353369
354370 // Check if this is something we already computed.
355371 {