Skip to content

Instantly share code, notes, and snippets.

@nickdesaulniers
Created September 5, 2019 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickdesaulniers/c44e2244a04e8700041b1d38af6a94ce to your computer and use it in GitHub Desktop.
Save nickdesaulniers/c44e2244a04e8700041b1d38af6a94ce to your computer and use it in GitHub Desktop.
diff --git a/llvm/unittests/IR/InstructionsTest.cpp b/llvm/unittests/IR/InstructionsTest.cpp
index 9b9efe33cfed..159c63a5c5c4 100644
--- a/llvm/unittests/IR/InstructionsTest.cpp
+++ b/llvm/unittests/IR/InstructionsTest.cpp
@@ -1061,5 +1061,55 @@ TEST(InstructionsTest, FNegInstruction) {
FNeg->deleteValue();
}
+TEST(InstructionsTest, CallBrInstruction) {
+ LLVMContext Context;
+ std::unique_ptr<Module> M = parseIR(Context, R"(
+define void @foo() {
+entry:
+ callbr void asm sideeffect "// XXX: ${0:l}", "X"(i8* blockaddress(@foo, %branch_test.exit))
+ to label %land.rhs.i [label %branch_test.exit]
+
+land.rhs.i:
+ br label %branch_test.exit
+
+branch_test.exit:
+ %0 = phi i1 [ true, %entry ], [ false, %land.rhs.i ]
+ br i1 %0, label %if.end, label %if.then
+
+if.then:
+ ret void
+
+if.end:
+ ret void
+}
+)");
+ Function *Foo = M->getFunction("foo");
+ auto BBs = Foo->getBasicBlockList().begin();
+ CallBrInst &CBI = cast<CallBrInst>(BBs->front());
+ ++BBs;
+ ++BBs;
+ BasicBlock &BranchTestExit = *BBs;
+ ++BBs;
+ BasicBlock &IfThen = *BBs;
+
+ // Test that setting the first indirect destination of callbr updates the dest
+ EXPECT_EQ(&BranchTestExit, CBI.getIndirectDest(0));
+ CBI.setIndirectDest(0, &IfThen);
+ EXPECT_EQ(&IfThen, CBI.getIndirectDest(0));
+
+ // Further, test that changing the indirect destination updates the arg
+ // operand to use the block address of the new indirect destination basic
+ // block. This is a critical invariant of CallBrInst.
+ BlockAddress *IndirectBA = BlockAddress::get(CBI.getIndirectDest(0));
+ BlockAddress *ArgBA = cast<BlockAddress>(CBI.getArgOperand(0));
+ EXPECT_EQ(IndirectBA, ArgBA) << "After setting the indirect destination, "
+ "callbr had an indirect destination of '" <<
+ CBI.getIndirectDest(0)->getName() << "', but a argument of '" <<
+ ArgBA->getBasicBlock()->getName() << "'. These should always match:\n" <<
+ CBI;
+ EXPECT_EQ(IndirectBA->getBasicBlock(), &IfThen);
+ EXPECT_EQ(ArgBA->getBasicBlock(), &IfThen);
+}
+
} // end anonymous namespace
} // end namespace llvm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment