[llvm] r339654 - [RISCV] Fix incorrect use of MCInstBuilder

Roger Ferrer Ibanez via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 14 01:30:42 PDT 2018


Author: rogfer01
Date: Tue Aug 14 01:30:42 2018
New Revision: 339654

URL: http://llvm.org/viewvc/llvm-project?rev=339654&view=rev
Log:
[RISCV] Fix incorrect use of MCInstBuilder

This is a fix for r339314.

MCInstBuilder uses the named parameter idiom and an 'operator MCInst&' to ease
the creation of MCInsts. As the object of MCInstBuilder owns the MCInst is
manipulating, the lifetime of the MCInst is bound to that of MCInstBuilder.

In r339314 I bound a reference to the MCInst in an initializer. The
temporary of MCInstBuilder (and also its MCInst) is destroyed at the end of
the declaration leading to a dangling reference.

Fix this by using MCInstBuilder inside an argument of a function call.
Temporaries in function calls are destroyed in the enclosing full expression,
so the the reference to MCInst is still valid when emitToStreamer executes.


Modified:
    llvm/trunk/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp

Modified: llvm/trunk/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp?rev=339654&r1=339653&r2=339654&view=diff
==============================================================================
--- llvm/trunk/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp (original)
+++ llvm/trunk/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp Tue Aug 14 01:30:42 2018
@@ -1227,19 +1227,17 @@ void RISCVAsmParser::emitLoadLocalAddres
   const RISCVMCExpr *Symbol = RISCVMCExpr::create(
       Inst.getOperand(1).getExpr(), RISCVMCExpr::VK_RISCV_PCREL_HI, Ctx);
 
-  MCInst &AUIPC =
-      MCInstBuilder(RISCV::AUIPC).addOperand(DestReg).addExpr(Symbol);
-  emitToStreamer(Out, AUIPC);
+  emitToStreamer(
+      Out, MCInstBuilder(RISCV::AUIPC).addOperand(DestReg).addExpr(Symbol));
 
   const MCExpr *RefToLinkTmpLabel =
       RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx),
                           RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
 
-  MCInst &ADDI = MCInstBuilder(RISCV::ADDI)
-                     .addOperand(DestReg)
-                     .addOperand(DestReg)
-                     .addExpr(RefToLinkTmpLabel);
-  emitToStreamer(Out, ADDI);
+  emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
+                          .addOperand(DestReg)
+                          .addOperand(DestReg)
+                          .addExpr(RefToLinkTmpLabel));
 }
 
 bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,




More information about the llvm-commits mailing list