5353#include " llvm/IR/Instruction.h"
5454#include " llvm/IR/Instructions.h"
5555#include " llvm/IR/IntrinsicInst.h"
56+ #include " llvm/IR/Intrinsics.h"
5657#include " llvm/IR/LLVMContext.h"
5758#include " llvm/IR/Metadata.h"
5859#include " llvm/IR/Module.h"
@@ -2831,6 +2832,7 @@ class AssemblyWriter {
28312832 SetVector<const Comdat *> Comdats;
28322833 bool IsForDebug;
28332834 bool ShouldPreserveUseListOrder;
2835+ bool PrettyPrintIntrinsicArgs;
28342836 UseListOrderMap UseListOrders;
28352837 SmallVector<StringRef, 8 > MDNames;
28362838 // / Synchronization scope names registered with LLVMContext.
@@ -2841,7 +2843,8 @@ class AssemblyWriter {
28412843 // / Construct an AssemblyWriter with an external SlotTracker
28422844 AssemblyWriter (formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
28432845 AssemblyAnnotationWriter *AAW, bool IsForDebug,
2844- bool ShouldPreserveUseListOrder = false );
2846+ bool ShouldPreserveUseListOrder = false ,
2847+ bool PrettyPrintIntrinsicArgs = false );
28452848
28462849 AssemblyWriter (formatted_raw_ostream &o, SlotTracker &Mac,
28472850 const ModuleSummaryIndex *Index, bool IsForDebug);
@@ -2930,10 +2933,12 @@ class AssemblyWriter {
29302933
29312934AssemblyWriter::AssemblyWriter (formatted_raw_ostream &o, SlotTracker &Mac,
29322935 const Module *M, AssemblyAnnotationWriter *AAW,
2933- bool IsForDebug, bool ShouldPreserveUseListOrder)
2936+ bool IsForDebug, bool ShouldPreserveUseListOrder,
2937+ bool PrettyPrintIntrinsicArgs)
29342938 : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
29352939 IsForDebug(IsForDebug),
2936- ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
2940+ ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
2941+ PrettyPrintIntrinsicArgs(PrettyPrintIntrinsicArgs) {
29372942 if (!TheModule)
29382943 return ;
29392944 for (const GlobalObject &GO : TheModule->global_objects ())
@@ -2944,7 +2949,8 @@ AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
29442949AssemblyWriter::AssemblyWriter (formatted_raw_ostream &o, SlotTracker &Mac,
29452950 const ModuleSummaryIndex *Index, bool IsForDebug)
29462951 : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/* Module=*/ nullptr ),
2947- IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false ) {}
2952+ IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false ),
2953+ PrettyPrintIntrinsicArgs(false ) {}
29482954
29492955void AssemblyWriter::writeOperand (const Value *Operand, bool PrintType) {
29502956 if (!Operand) {
@@ -4561,12 +4567,40 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
45614567 Out << ' ' ;
45624568 writeOperand (Operand, false );
45634569 Out << ' (' ;
4570+ bool HasPrettyPrintedArgs =
4571+ PrettyPrintIntrinsicArgs && isa<IntrinsicInst>(CI) &&
4572+ Intrinsic::hasPrettyPrintedArgs (CI->getIntrinsicID ());
4573+
45644574 ListSeparator LS;
4565- for (unsigned op = 0 , Eop = CI->arg_size (); op < Eop; ++op) {
4566- Out << LS;
4567- writeParamOperand (CI->getArgOperand (op), PAL.getParamAttrs (op));
4568- }
4575+ if (HasPrettyPrintedArgs) {
4576+ Function *CalledFunc = CI->getCalledFunction ();
4577+ auto PrintArgComment = [&](unsigned ArgNo) {
4578+ if (!CalledFunc->hasParamAttribute (ArgNo, Attribute::ImmArg))
4579+ return ;
4580+ const Constant *ConstArg = dyn_cast<Constant>(CI->getArgOperand (ArgNo));
4581+ if (!ConstArg)
4582+ return ;
4583+ std::string ArgComment;
4584+ raw_string_ostream ArgCommentStream (ArgComment);
4585+ Intrinsic::ID IID = CalledFunc->getIntrinsicID ();
4586+ Intrinsic::printImmArg (IID, ArgNo, ArgCommentStream, ConstArg);
4587+ if (ArgComment.empty ())
4588+ return ;
4589+ Out << " /* " << ArgComment << " */ " ;
4590+ };
45694591
4592+ for (unsigned ArgNo = 0 , NumArgs = CI->arg_size (); ArgNo < NumArgs;
4593+ ++ArgNo) {
4594+ Out << LS;
4595+ PrintArgComment (ArgNo);
4596+ writeParamOperand (CI->getArgOperand (ArgNo), PAL.getParamAttrs (ArgNo));
4597+ }
4598+ } else {
4599+ for (unsigned op = 0 , Eop = CI->arg_size (); op < Eop; ++op) {
4600+ Out << LS;
4601+ writeParamOperand (CI->getArgOperand (op), PAL.getParamAttrs (op));
4602+ }
4603+ }
45704604 // Emit an ellipsis if this is a musttail call in a vararg function. This
45714605 // is only to aid readability, musttail calls forward varargs by default.
45724606 if (CI->isMustTailCall () && CI->getParent () &&
@@ -4990,13 +5024,12 @@ void AssemblyWriter::printUseLists(const Function *F) {
49905024// ===----------------------------------------------------------------------===//
49915025
49925026void Function::print (raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
4993- bool ShouldPreserveUseListOrder,
4994- bool IsForDebug ) const {
5027+ bool ShouldPreserveUseListOrder, bool IsForDebug,
5028+ bool PrettyPrintIntrinsicArgs ) const {
49955029 SlotTracker SlotTable (this ->getParent ());
49965030 formatted_raw_ostream OS (ROS);
4997- AssemblyWriter W (OS, SlotTable, this ->getParent (), AAW,
4998- IsForDebug,
4999- ShouldPreserveUseListOrder);
5031+ AssemblyWriter W (OS, SlotTable, this ->getParent (), AAW, IsForDebug,
5032+ ShouldPreserveUseListOrder, PrettyPrintIntrinsicArgs);
50005033 W.printFunction (this );
50015034}
50025035
@@ -5012,11 +5045,12 @@ void BasicBlock::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
50125045}
50135046
50145047void Module::print (raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
5015- bool ShouldPreserveUseListOrder, bool IsForDebug) const {
5048+ bool ShouldPreserveUseListOrder, bool IsForDebug,
5049+ bool PrettyPrintIntrinsicArgs) const {
50165050 SlotTracker SlotTable (this );
50175051 formatted_raw_ostream OS (ROS);
50185052 AssemblyWriter W (OS, SlotTable, this , AAW, IsForDebug,
5019- ShouldPreserveUseListOrder);
5053+ ShouldPreserveUseListOrder, PrettyPrintIntrinsicArgs );
50205054 W.printModule (this );
50215055}
50225056
0 commit comments