1 //===- llvm/unittest/IR/AsmWriter.cpp - AsmWriter tests -------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 #include "llvm/BinaryFormat/Dwarf.h" 9 #include "llvm/IR/DebugInfoMetadata.h" 10 #include "llvm/IR/Function.h" 11 #include "llvm/IR/IRBuilder.h" 12 #include "llvm/IR/LLVMContext.h" 13 #include "llvm/IR/MDBuilder.h" 14 #include "llvm/IR/Module.h" 15 #include "gtest/gtest.h" 16 17 using namespace llvm; 18 19 namespace { 20 21 TEST(AsmWriterTest, DebugPrintDetachedInstruction) { 22 23 // PR24852: Ensure that an instruction can be printed even when it 24 // has metadata attached but no parent. 25 LLVMContext Ctx; 26 auto Ty = Type::getInt32Ty(Ctx); 27 auto Poison = PoisonValue::get(Ty); 28 std::unique_ptr<BinaryOperator> Add(BinaryOperator::CreateAdd(Poison, Poison)); 29 Add->setMetadata( 30 "", MDNode::get(Ctx, {ConstantAsMetadata::get(ConstantInt::get(Ty, 1))})); 31 std::string S; 32 raw_string_ostream OS(S); 33 Add->print(OS); 34 std::size_t r = OS.str().find("<badref> = add i32 poison, poison, !<empty"); 35 EXPECT_TRUE(r != std::string::npos); 36 } 37 38 TEST(AsmWriterTest, DebugPrintDetachedArgument) { 39 LLVMContext Ctx; 40 auto Ty = Type::getInt32Ty(Ctx); 41 auto Arg = new Argument(Ty); 42 43 std::string S; 44 raw_string_ostream OS(S); 45 Arg->print(OS); 46 EXPECT_EQ(S, "i32 <badref>"); 47 delete Arg; 48 } 49 50 TEST(AsmWriterTest, DumpDIExpression) { 51 LLVMContext Ctx; 52 uint64_t Ops[] = { 53 dwarf::DW_OP_constu, 4, 54 dwarf::DW_OP_minus, 55 dwarf::DW_OP_deref, 56 }; 57 DIExpression *Expr = DIExpression::get(Ctx, Ops); 58 std::string S; 59 raw_string_ostream OS(S); 60 Expr->print(OS); 61 EXPECT_EQ("!DIExpression(DW_OP_constu, 4, DW_OP_minus, DW_OP_deref)", 62 OS.str()); 63 } 64 65 TEST(AsmWriterTest, PrintAddrspaceWithNullOperand) { 66 LLVMContext Ctx; 67 Module M("test module", Ctx); 68 SmallVector<Type *, 3> FArgTypes; 69 FArgTypes.push_back(Type::getInt64Ty(Ctx)); 70 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false); 71 Function *F = Function::Create(FTy, Function::ExternalLinkage, "", &M); 72 Argument *Arg0 = F->getArg(0); 73 Value *Args[] = {Arg0}; 74 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args)); 75 // This will make Call's operand null. 76 Call->dropAllReferences(); 77 78 std::string S; 79 raw_string_ostream OS(S); 80 Call->print(OS); 81 std::size_t r = OS.str().find("<cannot get addrspace!>"); 82 EXPECT_TRUE(r != std::string::npos); 83 } 84 85 TEST(AsmWriterTest, PrintNullOperandBundle) { 86 LLVMContext C; 87 Type *Int32Ty = Type::getInt32Ty(C); 88 FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false); 89 Value *Callee = Constant::getNullValue(FnTy->getPointerTo()); 90 Value *Args[] = {ConstantInt::get(Int32Ty, 42)}; 91 std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C)); 92 std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C)); 93 OperandBundleDef Bundle("bundle", UndefValue::get(Int32Ty)); 94 std::unique_ptr<InvokeInst> Invoke( 95 InvokeInst::Create(FnTy, Callee, NormalDest.get(), UnwindDest.get(), Args, 96 Bundle, "result")); 97 // Makes the operand bundle null. 98 Invoke->dropAllReferences(); 99 Invoke->setNormalDest(NormalDest.get()); 100 Invoke->setUnwindDest(UnwindDest.get()); 101 102 std::string S; 103 raw_string_ostream OS(S); 104 Invoke->print(OS); 105 EXPECT_TRUE(OS.str().find("<null operand bundle!>") != std::string::npos); 106 } 107 } 108