MIR Serialization: Serialize unnamed local IR values in memory operands.
authorAlex Lorenz <arphaman@gmail.com>
Wed, 19 Aug 2015 23:31:05 +0000 (23:31 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Wed, 19 Aug 2015 23:31:05 +0000 (23:31 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@245521 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/MIRParser/MILexer.cpp
lib/CodeGen/MIRParser/MILexer.h
lib/CodeGen/MIRParser/MIParser.cpp
lib/CodeGen/MIRPrinter.cpp
test/CodeGen/MIR/X86/memory-operands.mir

index 94f38e48281217efc43c45d89401ad439b2bb730..6094ca996bf6fb7db10ba51bdf3d133f320170d1 100644 (file)
@@ -342,6 +342,8 @@ static Cursor maybeLexIRValue(
   const StringRef Rule = "%ir.";
   if (!C.remaining().startswith(Rule))
     return None;
   const StringRef Rule = "%ir.";
   if (!C.remaining().startswith(Rule))
     return None;
+  if (isdigit(C.peek(Rule.size())))
+    return maybeLexIndex(C, Token, Rule, MIToken::IRValue);
   return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
 }
 
   return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
 }
 
index 55479b8c54d72edaf0c6d47c1f17ad1743daccb8..17ddc4d5c1adfc59ce93b86d9d1935fe12b15ba2 100644 (file)
@@ -114,6 +114,7 @@ struct MIToken {
     NamedIRBlock,
     IRBlock,
     NamedIRValue,
     NamedIRBlock,
     IRBlock,
     NamedIRValue,
+    IRValue
   };
 
 private:
   };
 
 private:
@@ -175,7 +176,7 @@ public:
            Kind == MachineBasicBlockLabel || Kind == StackObject ||
            Kind == FixedStackObject || Kind == GlobalValue ||
            Kind == VirtualRegister || Kind == ConstantPoolItem ||
            Kind == MachineBasicBlockLabel || Kind == StackObject ||
            Kind == FixedStackObject || Kind == GlobalValue ||
            Kind == VirtualRegister || Kind == ConstantPoolItem ||
-           Kind == JumpTableIndex || Kind == IRBlock;
+           Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
   }
 };
 
   }
 };
 
index 4dfa793e7307b13a36d6f25b4bf18695b6897bf4..80f6a654b97316df43bed18633104a24a162ba04 100644 (file)
@@ -73,6 +73,8 @@ class MIParser {
   StringMap<unsigned> Names2SubRegIndices;
   /// Maps from slot numbers to function's unnamed basic blocks.
   DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
   StringMap<unsigned> Names2SubRegIndices;
   /// Maps from slot numbers to function's unnamed basic blocks.
   DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
+  /// Maps from slot numbers to function's unnamed values.
+  DenseMap<unsigned, const Value *> Slots2Values;
   /// Maps from target index names to target indices.
   StringMap<int> Names2TargetIndices;
   /// Maps from direct target flag names to the direct target flag values.
   /// Maps from target index names to target indices.
   StringMap<int> Names2TargetIndices;
   /// Maps from direct target flag names to the direct target flag values.
@@ -212,6 +214,8 @@ private:
   const BasicBlock *getIRBlock(unsigned Slot);
   const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
 
   const BasicBlock *getIRBlock(unsigned Slot);
   const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
 
+  const Value *getIRValue(unsigned Slot);
+
   void initNames2TargetIndices();
 
   /// Try to convert a name of target index to the corresponding target index.
   void initNames2TargetIndices();
 
   /// Try to convert a name of target index to the corresponding target index.
@@ -1530,14 +1534,20 @@ bool MIParser::parseIRValue(const Value *&V) {
     if (!V)
       V = MF.getFunction()->getParent()->getValueSymbolTable().lookup(
           Token.stringValue());
     if (!V)
       V = MF.getFunction()->getParent()->getValueSymbolTable().lookup(
           Token.stringValue());
-    if (!V)
-      return error(Twine("use of undefined IR value '") + Token.range() + "'");
     break;
   }
     break;
   }
-  // TODO: Parse unnamed IR value references.
+  case MIToken::IRValue: {
+    unsigned SlotNumber = 0;
+    if (getUnsigned(SlotNumber))
+      return true;
+    V = getIRValue(SlotNumber);
+    break;
+  }
   default:
     llvm_unreachable("The current token should be an IR block reference");
   }
   default:
     llvm_unreachable("The current token should be an IR block reference");
   }
+  if (!V)
+    return error(Twine("use of undefined IR value '") + Token.range() + "'");
   return false;
 }
 
   return false;
 }
 
@@ -1629,7 +1639,7 @@ bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
     Dest = MachinePointerInfo(PSV, Offset);
     return false;
   }
     Dest = MachinePointerInfo(PSV, Offset);
     return false;
   }
-  if (Token.isNot(MIToken::NamedIRValue))
+  if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue))
     return error("expected an IR value reference");
   const Value *V = nullptr;
   if (parseIRValue(V))
     return error("expected an IR value reference");
   const Value *V = nullptr;
   if (parseIRValue(V))
@@ -1837,6 +1847,37 @@ const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
   return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
 }
 
   return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
 }
 
+static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
+                           DenseMap<unsigned, const Value *> &Slots2Values) {
+  int Slot = MST.getLocalSlot(V);
+  if (Slot == -1)
+    return;
+  Slots2Values.insert(std::make_pair(unsigned(Slot), V));
+}
+
+/// Creates the mapping from slot numbers to function's unnamed IR values.
+static void initSlots2Values(const Function &F,
+                             DenseMap<unsigned, const Value *> &Slots2Values) {
+  ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
+  MST.incorporateFunction(F);
+  for (const auto &Arg : F.args())
+    mapValueToSlot(&Arg, MST, Slots2Values);
+  for (const auto &BB : F) {
+    mapValueToSlot(&BB, MST, Slots2Values);
+    for (const auto &I : BB)
+      mapValueToSlot(&I, MST, Slots2Values);
+  }
+}
+
+const Value *MIParser::getIRValue(unsigned Slot) {
+  if (Slots2Values.empty())
+    initSlots2Values(*MF.getFunction(), Slots2Values);
+  auto ValueInfo = Slots2Values.find(Slot);
+  if (ValueInfo == Slots2Values.end())
+    return nullptr;
+  return ValueInfo->second;
+}
+
 void MIParser::initNames2TargetIndices() {
   if (!Names2TargetIndices.empty())
     return;
 void MIParser::initNames2TargetIndices() {
   if (!Names2TargetIndices.empty())
     return;
index df2f441a031c5dc18042d89d535610abbd716501..e9ecba404b68ef6e762487ddf552e93dea0cf270 100644 (file)
@@ -608,13 +608,13 @@ void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
 }
 
 void MIPrinter::printIRValueReference(const Value &V) {
 }
 
 void MIPrinter::printIRValueReference(const Value &V) {
+  // TODO: Global values should use the '@' syntax.
   OS << "%ir.";
   if (V.hasName()) {
     printLLVMNameWithoutPrefix(OS, V.getName());
     return;
   }
   OS << "%ir.";
   if (V.hasName()) {
     printLLVMNameWithoutPrefix(OS, V.getName());
     return;
   }
-  // TODO: Serialize the unnamed IR value references.
-  OS << "<unserializable ir value>";
+  printIRSlotNumber(OS, MST.getLocalSlot(&V));
 }
 
 void MIPrinter::printStackObjectReference(int FrameIndex) {
 }
 
 void MIPrinter::printStackObjectReference(int FrameIndex) {
index fabea5ebcbfa237d1937b83a00eeadb1aad36439..ba5bcec820e1307fad2f495b6af864051eabf37e 100644 (file)
     ret void
   }
 
     ret void
   }
 
+  define void @test3(i32*) {
+  entry3:
+    %1 = alloca i32
+    %b = load i32, i32* %0
+    %c = add i32 %b, 1
+    store i32 %c, i32* %1
+    ret void
+  }
+
   define i32 @volatile_inc(i32* %x) {
   entry:
     %0 = load volatile i32, i32* %x
   define i32 @volatile_inc(i32* %x) {
   entry:
     %0 = load volatile i32, i32* %x
@@ -185,6 +194,27 @@ body: |
     RETQ
 ...
 ---
     RETQ
 ...
 ---
+name:            test3
+tracksRegLiveness: true
+liveins:
+  - { reg: '%rdi' }
+frameInfo:
+  maxAlignment:    4
+stack:
+  - { id: 0, offset: -12, size: 4, alignment: 4 }
+body: |
+  bb.0.entry3:
+    liveins: %rdi
+  ; Verify that the unnamed local values can be serialized.
+  ; CHECK-LABEL: name: test3
+  ; CHECK: %eax = MOV32rm killed %rdi, 1, _, 0, _ :: (load 4 from %ir.0)
+  ; CHECK: MOV32mr %rsp, 1, _, -4, _, killed %eax :: (store 4 into %ir.1)
+    %eax = MOV32rm killed %rdi, 1, _, 0, _ :: (load 4 from %ir.0)
+    %eax = INC32r killed %eax, implicit-def dead %eflags
+    MOV32mr %rsp, 1, _, -4, _, killed %eax :: (store 4 into %ir.1)
+    RETQ
+...
+---
 name:            volatile_inc
 tracksRegLiveness: true
 liveins:
 name:            volatile_inc
 tracksRegLiveness: true
 liveins: