Custom BR instruction emitted in wrong order


To play around with, I duplicated the branch instruction implementation in LLVM and Clang under the opcode ‘fbr’ as opposed to regular ‘br’. I accept LLVM to accept the new opcode and have Clang produce ‘fbr’ branches instead of the normal branches. But the place of ‘if.then' and ‘if.else' in the instruction is switch. Instead of if.then occurring first, if.else is occurring first. For example:

The normal conditional branch instructions would be: "br i1 %cmp, label %if.then, label %if.else”
But if I force Clang to produce ’fbr’ instruction, it becomes: “fbr i1 %cmp, label %if.else, label %if.then” (the condition doesn’t inverse either)

This is causing the output to the wrong since the right and wrong block are switched. I traced it down to the generation of the custom branch instruction to Instructions.cpp (it’s the same as BranchstInst class but just the opcode changed) and dumping all the information once it’s being created. Everything is in order until it returns back from FBranchInst::Create (in IRBuilder.h), which is where the if.then and if.else are being switch. I have no idea what’s causing it to flip, and what makes it weird is that if I pass the .ll file to opt (or back it back into Clang with -emit-llvm option), the new output file has the block order flipped back (so if.then is first and following that is if.else). They flip to the wrong order if I pass it back through opt (or Clang) again.

Could I have missed to duplicate something from BranchInst that might be causing this? I’d appreciate any help I could get with this.

Kaarthik A.