|
20 | 20 |
|
21 | 21 | #include "absl/container/flat_hash_map.h"
|
22 | 22 | #include "absl/container/flat_hash_set.h"
|
| 23 | +#include "labm8/cpp/logging.h" |
23 | 24 | #include "labm8/cpp/status_macros.h"
|
24 | 25 | #include "labm8/cpp/string.h"
|
25 | 26 | #include "llvm/IR/BasicBlock.h"
|
@@ -357,29 +358,125 @@ Node* ProgramGraphBuilder::AddLlvmInstruction(
|
357 | 358 | Node* ProgramGraphBuilder::AddLlvmVariable(const ::llvm::Instruction* operand,
|
358 | 359 | const programl::Function* function) {
|
359 | 360 | const LlvmTextComponents text = textEncoder_.Encode(operand);
|
360 |
| - Node* node = AddVariable(text.lhs_type, function); |
| 361 | + Node* node = AddVariable("var", function); |
361 | 362 | node->set_block(blockCount_);
|
362 | 363 | graph::AddScalarFeature(node, "llvm_string", AddString(text.lhs));
|
363 | 364 |
|
| 365 | + compositeTypeParts_.clear(); // Reset after previous call. |
| 366 | + Node* type = GetOrCreateType(operand->getType()); |
| 367 | + CHECK(AddTypeEdge(/*position=*/0, type, node).ok()); |
| 368 | + |
364 | 369 | return node;
|
365 | 370 | }
|
366 | 371 |
|
367 | 372 | Node* ProgramGraphBuilder::AddLlvmVariable(const ::llvm::Argument* argument,
|
368 | 373 | const programl::Function* function) {
|
369 | 374 | const LlvmTextComponents text = textEncoder_.Encode(argument);
|
370 |
| - Node* node = AddVariable(text.lhs_type, function); |
| 375 | + Node* node = AddVariable("var", function); |
371 | 376 | node->set_block(blockCount_);
|
372 | 377 | graph::AddScalarFeature(node, "llvm_string", AddString(text.lhs));
|
373 | 378 |
|
| 379 | + compositeTypeParts_.clear(); // Reset after previous call. |
| 380 | + Node* type = GetOrCreateType(argument->getType()); |
| 381 | + CHECK(AddTypeEdge(/*position=*/0, type, node).ok()); |
| 382 | + |
374 | 383 | return node;
|
375 | 384 | }
|
376 | 385 |
|
377 | 386 | Node* ProgramGraphBuilder::AddLlvmConstant(const ::llvm::Constant* constant) {
|
378 | 387 | const LlvmTextComponents text = textEncoder_.Encode(constant);
|
379 |
| - Node* node = AddConstant(text.lhs_type); |
| 388 | + Node* node = AddConstant("val"); |
380 | 389 | node->set_block(blockCount_);
|
381 | 390 | graph::AddScalarFeature(node, "llvm_string", AddString(text.text));
|
382 | 391 |
|
| 392 | + compositeTypeParts_.clear(); // Reset after previous call. |
| 393 | + Node* type = GetOrCreateType(constant->getType()); |
| 394 | + CHECK(AddTypeEdge(/*position=*/0, type, node).ok()); |
| 395 | + |
| 396 | + return node; |
| 397 | +} |
| 398 | + |
| 399 | +Node* ProgramGraphBuilder::AddLlvmType(const ::llvm::Type* type) { |
| 400 | + // Dispatch to the type-specific handlers. |
| 401 | + if (::llvm::dyn_cast<::llvm::StructType>(type)) { |
| 402 | + return AddLlvmType(::llvm::dyn_cast<::llvm::StructType>(type)); |
| 403 | + } else if (::llvm::dyn_cast<::llvm::PointerType>(type)) { |
| 404 | + return AddLlvmType(::llvm::dyn_cast<::llvm::PointerType>(type)); |
| 405 | + } else if (::llvm::dyn_cast<::llvm::FunctionType>(type)) { |
| 406 | + return AddLlvmType(::llvm::dyn_cast<::llvm::FunctionType>(type)); |
| 407 | + } else if (::llvm::dyn_cast<::llvm::ArrayType>(type)) { |
| 408 | + return AddLlvmType(::llvm::dyn_cast<::llvm::ArrayType>(type)); |
| 409 | + } else if (::llvm::dyn_cast<::llvm::VectorType>(type)) { |
| 410 | + return AddLlvmType(::llvm::dyn_cast<::llvm::VectorType>(type)); |
| 411 | + } else { |
| 412 | + const LlvmTextComponents text = textEncoder_.Encode(type); |
| 413 | + Node *node = AddType(text.text); |
| 414 | + graph::AddScalarFeature(node, "llvm_string", AddString(text.text)); |
| 415 | + return node; |
| 416 | + } |
| 417 | +} |
| 418 | + |
| 419 | +Node* ProgramGraphBuilder::AddLlvmType(const ::llvm::StructType* type) { |
| 420 | + Node* node = AddType("struct"); |
| 421 | + compositeTypeParts_[type] = node; |
| 422 | + graph::AddScalarFeature(node, "llvm_string", |
| 423 | + AddString(textEncoder_.Encode(type).text)); |
| 424 | + |
| 425 | + // Add types for the struct elements, and add type edges. |
| 426 | + for (int i = 0; i < type->getNumElements(); ++i) { |
| 427 | + const auto& member = type->elements()[i]; |
| 428 | + // Re-use the type if it already exists to prevent duplication of member |
| 429 | + // types. |
| 430 | + auto memberNode = GetOrCreateType(member); |
| 431 | + CHECK(AddTypeEdge(/*position=*/i, memberNode, node).ok()); |
| 432 | + } |
| 433 | + |
| 434 | + return node; |
| 435 | +} |
| 436 | + |
| 437 | +Node* ProgramGraphBuilder::AddLlvmType(const ::llvm::PointerType* type) { |
| 438 | + Node* node = AddType("*"); |
| 439 | + graph::AddScalarFeature(node, "llvm_string", |
| 440 | + AddString(textEncoder_.Encode(type).text)); |
| 441 | + |
| 442 | + auto elementType = type->getElementType(); |
| 443 | + auto parent = compositeTypeParts_.find(elementType); |
| 444 | + if (parent == compositeTypeParts_.end()) { |
| 445 | + // Re-use the type if it already exists to prevent duplication. |
| 446 | + auto elementNode = GetOrCreateType(type->getElementType()); |
| 447 | + CHECK(AddTypeEdge(/*position=*/0, elementNode, node).ok()); |
| 448 | + } else { |
| 449 | + // Bottom-out for self-referencing types. |
| 450 | + CHECK(AddTypeEdge(/*position=*/0, node, parent->second).ok()); |
| 451 | + } |
| 452 | + |
| 453 | + return node; |
| 454 | +} |
| 455 | + |
| 456 | +Node* ProgramGraphBuilder::AddLlvmType(const ::llvm::FunctionType* type) { |
| 457 | + Node* node = AddType("fn"); |
| 458 | + graph::AddScalarFeature(node, "llvm_string", |
| 459 | + AddString(textEncoder_.Encode(type).text)); |
| 460 | + return node; |
| 461 | +} |
| 462 | + |
| 463 | +Node* ProgramGraphBuilder::AddLlvmType(const ::llvm::ArrayType* type) { |
| 464 | + Node* node = AddType("[]"); |
| 465 | + graph::AddScalarFeature(node, "llvm_string", |
| 466 | + AddString(textEncoder_.Encode(type).text)); |
| 467 | + // Re-use the type if it already exists to prevent duplication. |
| 468 | + auto elementType = GetOrCreateType(type->getElementType()); |
| 469 | + CHECK(AddTypeEdge(/*position=*/0, elementType, node).ok()); |
| 470 | + return node; |
| 471 | +} |
| 472 | + |
| 473 | +Node* ProgramGraphBuilder::AddLlvmType(const ::llvm::VectorType* type) { |
| 474 | + Node* node = AddType("vector"); |
| 475 | + graph::AddScalarFeature(node, "llvm_string", |
| 476 | + AddString(textEncoder_.Encode(type).text)); |
| 477 | + // Re-use the type if it already exists to prevent duplication. |
| 478 | + auto elementType = GetOrCreateType(type->getElementType()); |
| 479 | + CHECK(AddTypeEdge(/*position=*/0, elementType, node).ok()); |
383 | 480 | return node;
|
384 | 481 | }
|
385 | 482 |
|
|
0 commit comments